-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathuseItems.tsx
More file actions
79 lines (69 loc) · 1.96 KB
/
useItems.tsx
File metadata and controls
79 lines (69 loc) · 1.96 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
import React from 'react';
import type { CollapsePanelProps, CollapseProps, ItemType } from '../interface';
import CollapsePanel from '../Panel';
type Props = Pick<CollapsePanelProps, 'prefixCls' | 'onItemClick' | 'openMotion' | 'expandIcon'> &
Pick<CollapseProps, 'accordion' | 'collapsible' | 'destroyInactivePanel'> & {
activeKey: React.Key[];
};
const convertItemsToNodes = (items: ItemType[], props: Props) => {
const {
prefixCls,
accordion,
collapsible,
destroyInactivePanel,
onItemClick,
activeKey,
openMotion,
expandIcon,
} = props;
return items.map((item, index) => {
const {
children,
label,
key: rawKey,
collapsible: rawCollapsible,
onItemClick: rawOnItemClick,
destroyInactivePanel: rawDestroyInactivePanel,
...restProps
} = item;
const key = String(rawKey ?? index);
const mergeCollapsible = rawCollapsible ?? collapsible;
const mergeDestroyInactivePanel = rawDestroyInactivePanel ?? destroyInactivePanel;
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}
prefixCls={prefixCls}
key={key}
panelKey={key}
isActive={isActive}
accordion={accordion}
openMotion={openMotion}
expandIcon={expandIcon}
header={label}
collapsible={mergeCollapsible}
onItemClick={handleItemClick}
destroyInactivePanel={mergeDestroyInactivePanel}
>
{children}
</CollapsePanel>
);
});
};
function useItems(items?: ItemType[], props?: Props) {
if (Array.isArray(items)) {
return convertItemsToNodes(items, props);
}
return null;
}
export default useItems;