-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathuseItems.tsx
More file actions
195 lines (175 loc) · 4.47 KB
/
useItems.tsx
File metadata and controls
195 lines (175 loc) · 4.47 KB
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
import toArray from '@rc-component/util/lib/Children/toArray';
import React from 'react';
import type { CollapsePanelProps, CollapseProps, ItemType } from '../interface';
import CollapsePanel from '../Panel';
type Props = Pick<
CollapsePanelProps,
| 'prefixCls'
| 'onItemClick'
| 'openMotion'
| 'expandIcon'
| 'classNames'
| 'styles'
| 'headingLevel'
> &
Pick<CollapseProps, 'accordion' | 'collapsible' | 'destroyOnHidden'> & {
activeKey: React.Key[];
parentId?: string;
};
const convertItemsToNodes = (items: ItemType[], props: Props) => {
const {
prefixCls,
accordion,
collapsible,
destroyOnHidden,
onItemClick,
activeKey,
openMotion,
expandIcon,
classNames: collapseClassNames,
styles,
headingLevel,
parentId,
} = props;
return items.map((item, index) => {
const {
children,
label,
key: rawKey,
collapsible: rawCollapsible,
onItemClick: rawOnItemClick,
destroyOnHidden: rawDestroyOnHidden,
...restProps
} = item;
// You may be puzzled why you want to convert them all into strings, me too.
// Maybe: https://github.com/react-component/collapse/blob/aac303a8b6ff30e35060b4f8fecde6f4556fcbe2/src/Collapse.tsx#L15
const key = String(rawKey ?? index);
const mergeCollapsible = rawCollapsible ?? collapsible;
const mergedDestroyOnHidden = rawDestroyOnHidden ?? destroyOnHidden;
const handleItemClick = (value: React.Key) => {
if (mergeCollapsible === 'disabled') {
return;
}
onItemClick(value);
rawOnItemClick?.(value);
};
let isActive = false;
if (accordion) {
isActive = activeKey[0] === key;
} else {
isActive = activeKey.indexOf(key) > -1;
}
return (
<CollapsePanel
{...restProps}
classNames={collapseClassNames}
styles={styles}
prefixCls={prefixCls}
key={key}
panelKey={key}
isActive={isActive}
accordion={accordion}
openMotion={openMotion}
expandIcon={expandIcon}
header={label}
collapsible={mergeCollapsible}
onItemClick={handleItemClick}
destroyOnHidden={mergedDestroyOnHidden}
headingLevel={headingLevel}
id={`${parentId}__item-${key}`}
>
{children}
</CollapsePanel>
);
});
};
/**
* @deprecated The next major version will be removed
*/
const getNewChild = (
child: React.ReactElement<CollapsePanelProps>,
index: number,
props: Props,
) => {
if (!child) {
return null;
}
const {
prefixCls,
accordion,
collapsible,
destroyOnHidden,
onItemClick,
activeKey,
openMotion,
expandIcon,
classNames: collapseClassNames,
styles,
headingLevel,
parentId,
} = props;
const key = child.key || String(index);
const {
header,
headerClass,
destroyOnHidden: childDestroyOnHidden,
collapsible: childCollapsible,
onItemClick: childOnItemClick,
} = child.props;
let isActive = false;
if (accordion) {
isActive = activeKey[0] === key;
} else {
isActive = activeKey.indexOf(key) > -1;
}
const mergeCollapsible = childCollapsible ?? collapsible;
const handleItemClick = (value: React.Key) => {
if (mergeCollapsible === 'disabled') {
return;
}
onItemClick(value);
childOnItemClick?.(value);
};
const childProps = {
key,
panelKey: key,
header,
headerClass,
classNames: collapseClassNames,
styles,
isActive,
prefixCls,
destroyOnHidden: childDestroyOnHidden ?? destroyOnHidden,
openMotion,
accordion,
children: child.props.children,
onItemClick: handleItemClick,
expandIcon,
collapsible: mergeCollapsible,
headingLevel,
id: `${parentId}__item-${key}`,
};
// https://github.com/ant-design/ant-design/issues/20479
if (typeof child.type === 'string') {
return child;
}
Object.keys(childProps).forEach((propName) => {
if (typeof childProps[propName] === 'undefined') {
delete childProps[propName];
}
});
return React.cloneElement<CollapsePanelProps>(child, childProps);
};
function useItems(
items?: ItemType[],
rawChildren?: React.ReactNode,
props?: Props,
): React.ReactElement<CollapsePanelProps>[] {
if (Array.isArray(items)) {
return convertItemsToNodes(items, props);
}
return toArray(rawChildren).map((child, index) =>
getNewChild(child as React.ReactElement<CollapsePanelProps>, index, props),
);
}
export default useItems;