forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollapse.jsx
131 lines (126 loc) · 3.57 KB
/
Collapse.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import BaseMixin from '../../_util/BaseMixin';
import { hasProp, getPropsData, isEmptyElement, initDefaultProps } from '../../_util/props-util';
import { cloneElement } from '../../_util/vnode';
import openAnimationFactory from './openAnimationFactory';
import { collapseProps } from './commonProps';
function _toArray(activeKey) {
let currentActiveKey = activeKey;
if (!Array.isArray(currentActiveKey)) {
currentActiveKey = currentActiveKey ? [currentActiveKey] : [];
}
return currentActiveKey;
}
export default {
name: 'Collapse',
mixins: [BaseMixin],
model: {
prop: 'activeKey',
event: 'change',
},
props: initDefaultProps(collapseProps, {
prefixCls: 'rc-collapse',
accordion: false,
destroyInactivePanel: false,
}),
data() {
const { activeKey, defaultActiveKey, openAnimation, prefixCls } = this.$props;
let currentActiveKey = defaultActiveKey;
if (hasProp(this, 'activeKey')) {
currentActiveKey = activeKey;
}
const currentOpenAnimations = openAnimation || openAnimationFactory(prefixCls);
return {
currentOpenAnimations,
stateActiveKey: _toArray(currentActiveKey),
};
},
watch: {
activeKey(val) {
this.setState({
stateActiveKey: _toArray(val),
});
},
openAnimation(val) {
this.setState({
currentOpenAnimations: val,
});
},
},
methods: {
onClickItem(key) {
let activeKey = this.stateActiveKey;
if (this.accordion) {
activeKey = activeKey[0] === key ? [] : [key];
} else {
activeKey = [...activeKey];
const index = activeKey.indexOf(key);
const isActive = index > -1;
if (isActive) {
// remove active state
activeKey.splice(index, 1);
} else {
activeKey.push(key);
}
}
this.setActiveKey(activeKey);
},
getItems() {
const activeKey = this.stateActiveKey;
const { prefixCls, accordion, destroyInactivePanel, expandIcon } = this.$props;
const newChildren = [];
if (this.$slots.default) {
this.$slots.default.forEach((child, index) => {
if (isEmptyElement(child)) return;
const { header, headerClass, disabled } = getPropsData(child);
let isActive = false;
const key = child.key || String(index);
if (accordion) {
isActive = activeKey[0] === key;
} else {
isActive = activeKey.indexOf(key) > -1;
}
let panelEvents = {};
if (!disabled && disabled !== '') {
panelEvents = {
itemClick: () => {
this.onClickItem(key);
},
};
}
const props = {
props: {
header,
headerClass,
isActive,
prefixCls,
destroyInactivePanel,
openAnimation: this.currentOpenAnimations,
accordion,
expandIcon,
},
on: {
...panelEvents,
},
};
newChildren.push(cloneElement(child, props));
});
}
return newChildren;
},
setActiveKey(activeKey) {
this.setState({ stateActiveKey: activeKey });
this.$emit('change', this.accordion ? activeKey[0] : activeKey);
},
},
render() {
const { prefixCls, accordion } = this.$props;
const collapseClassName = {
[prefixCls]: true,
};
return (
<div class={collapseClassName} role={accordion ? 'tablist' : null}>
{this.getItems()}
</div>
);
},
};