-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathCollapse.tsx
202 lines (187 loc) · 6.18 KB
/
Collapse.tsx
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import {
isEmptyElement,
initDefaultProps,
flattenChildren,
isValidElement,
} from '../_util/props-util';
import { cloneElement } from '../_util/vnode';
import type { CollapsibleType } from './commonProps';
import { collapseProps } from './commonProps';
import { getDataAndAriaProps } from '../_util/util';
import type { CSSProperties, ExtractPropTypes } from 'vue';
import { computed, defineComponent, ref, watch } from 'vue';
import RightOutlined from '@ant-design/icons-vue/RightOutlined';
import firstNotUndefined from '../_util/firstNotUndefined';
import classNames from '../_util/classNames';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import type { CollapsePanelProps } from './CollapsePanel';
import collapseMotion from '../_util/collapseMotion';
import type { CustomSlotsType } from '../_util/type';
// CSSINJS
import useStyle from './style';
type Key = number | string;
function getActiveKeysArray(activeKey: Key | Key[]) {
let currentActiveKey = activeKey;
if (!Array.isArray(currentActiveKey)) {
const activeKeyType = typeof currentActiveKey;
currentActiveKey =
activeKeyType === 'number' || activeKeyType === 'string' ? [currentActiveKey] : [];
}
return currentActiveKey.map(key => String(key));
}
export { collapseProps };
export type CollapseProps = Partial<ExtractPropTypes<ReturnType<typeof collapseProps>>>;
export default defineComponent({
name: 'ACollapse',
inheritAttrs: false,
props: initDefaultProps(collapseProps(), {
accordion: false,
destroyInactivePanel: false,
bordered: true,
expandIconPosition: 'start',
}),
slots: Object as CustomSlotsType<{
default?: any;
expandIcon?: CollapsePanelProps;
}>,
setup(props, { attrs, slots, emit }) {
const stateActiveKey = ref<Key[]>(
getActiveKeysArray(firstNotUndefined([props.activeKey, props.defaultActiveKey])),
);
watch(
() => props.activeKey,
() => {
stateActiveKey.value = getActiveKeysArray(props.activeKey);
},
{ deep: true },
);
const { prefixCls, direction, rootPrefixCls } = useConfigInject('collapse', props);
// style
const [wrapSSR, hashId] = useStyle(prefixCls);
const iconPosition = computed(() => {
const { expandIconPosition } = props;
if (expandIconPosition !== undefined) {
return expandIconPosition;
}
return direction.value === 'rtl' ? 'end' : 'start';
});
const renderExpandIcon = (panelProps: CollapsePanelProps) => {
const { expandIcon = slots.expandIcon } = props;
const icon = expandIcon ? (
expandIcon(panelProps)
) : (
<RightOutlined rotate={panelProps.isActive ? 90 : undefined} />
);
return (
<div
class={[`${prefixCls.value}-expand-icon`, hashId.value]}
onClick={() =>
['header', 'icon'].includes(props.collapsible) && onClickItem(panelProps.panelKey)
}
>
{isValidElement(Array.isArray(expandIcon) ? icon[0] : icon)
? cloneElement(
icon,
{
class: `${prefixCls.value}-arrow`,
},
false,
)
: icon}
</div>
);
};
const setActiveKey = (activeKey: Key[]) => {
if (props.activeKey === undefined) {
stateActiveKey.value = activeKey;
}
const newKey = props.accordion ? activeKey[0] : activeKey;
emit('update:activeKey', newKey);
emit('change', newKey);
};
const onClickItem = (key: Key) => {
let activeKey = stateActiveKey.value;
if (props.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);
}
}
setActiveKey(activeKey);
};
const getNewChild = (child, index) => {
if (isEmptyElement(child)) return;
const activeKey = stateActiveKey.value;
const { accordion, destroyInactivePanel, collapsible, openAnimation } = props;
const animation = openAnimation || collapseMotion(`${rootPrefixCls.value}-motion-collapse`);
// If there is no key provide, use the panel order as default key
const key = String(child.key ?? index);
const {
header = child.children?.header?.(),
headerClass,
collapsible: childCollapsible,
disabled,
} = child.props || {};
let isActive = false;
if (accordion) {
isActive = activeKey[0] === key;
} else {
isActive = activeKey.indexOf(key) > -1;
}
let mergeCollapsible: CollapsibleType = childCollapsible ?? collapsible;
// legacy 2.x
if (disabled || disabled === '') {
mergeCollapsible = 'disabled';
}
const newProps = {
key,
panelKey: key,
header,
headerClass,
isActive,
prefixCls: prefixCls.value,
destroyInactivePanel,
openAnimation: animation,
accordion,
onItemClick: mergeCollapsible === 'disabled' ? null : onClickItem,
expandIcon: renderExpandIcon,
collapsible: mergeCollapsible,
};
return cloneElement(child, newProps);
};
const getItems = () => {
return flattenChildren(slots.default?.()).map(getNewChild);
};
return () => {
const { accordion, bordered, ghost } = props;
const collapseClassName = classNames(
prefixCls.value,
{
[`${prefixCls.value}-borderless`]: !bordered,
[`${prefixCls.value}-icon-position-${iconPosition.value}`]: true,
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
[`${prefixCls.value}-ghost`]: !!ghost,
[attrs.class as string]: !!attrs.class,
},
hashId.value,
);
return wrapSSR(
<div
class={collapseClassName}
{...getDataAndAriaProps(attrs)}
style={attrs.style as CSSProperties}
role={accordion ? 'tablist' : null}
>
{getItems()}
</div>,
);
};
},
});