UNPKG

4.8 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3var _excluded = ["children", "label", "key", "collapsible", "onItemClick", "destroyInactivePanel"];
4import toArray from "rc-util/es/Children/toArray";
5import React from 'react';
6import CollapsePanel from "../Panel";
7var convertItemsToNodes = function convertItemsToNodes(items, props) {
8 var prefixCls = props.prefixCls,
9 accordion = props.accordion,
10 collapsible = props.collapsible,
11 destroyInactivePanel = props.destroyInactivePanel,
12 onItemClick = props.onItemClick,
13 activeKey = props.activeKey,
14 openMotion = props.openMotion,
15 expandIcon = props.expandIcon;
16 return items.map(function (item, index) {
17 var children = item.children,
18 label = item.label,
19 rawKey = item.key,
20 rawCollapsible = item.collapsible,
21 rawOnItemClick = item.onItemClick,
22 rawDestroyInactivePanel = item.destroyInactivePanel,
23 restProps = _objectWithoutProperties(item, _excluded);
24
25 // You may be puzzled why you want to convert them all into strings, me too.
26 // Maybe: https://github.com/react-component/collapse/blob/aac303a8b6ff30e35060b4f8fecde6f4556fcbe2/src/Collapse.tsx#L15
27 var key = String(rawKey !== null && rawKey !== void 0 ? rawKey : index);
28 var mergeCollapsible = rawCollapsible !== null && rawCollapsible !== void 0 ? rawCollapsible : collapsible;
29 var mergeDestroyInactivePanel = rawDestroyInactivePanel !== null && rawDestroyInactivePanel !== void 0 ? rawDestroyInactivePanel : destroyInactivePanel;
30 var handleItemClick = function handleItemClick(value) {
31 if (mergeCollapsible === 'disabled') return;
32 onItemClick(value);
33 rawOnItemClick === null || rawOnItemClick === void 0 || rawOnItemClick(value);
34 };
35 var isActive = false;
36 if (accordion) {
37 isActive = activeKey[0] === key;
38 } else {
39 isActive = activeKey.indexOf(key) > -1;
40 }
41 return /*#__PURE__*/React.createElement(CollapsePanel, _extends({}, restProps, {
42 prefixCls: prefixCls,
43 key: key,
44 panelKey: key,
45 isActive: isActive,
46 accordion: accordion,
47 openMotion: openMotion,
48 expandIcon: expandIcon,
49 header: label,
50 collapsible: mergeCollapsible,
51 onItemClick: handleItemClick,
52 destroyInactivePanel: mergeDestroyInactivePanel
53 }), children);
54 });
55};
56
57/**
58 * @deprecated The next major version will be removed
59 */
60var getNewChild = function getNewChild(child, index, props) {
61 if (!child) return null;
62 var prefixCls = props.prefixCls,
63 accordion = props.accordion,
64 collapsible = props.collapsible,
65 destroyInactivePanel = props.destroyInactivePanel,
66 onItemClick = props.onItemClick,
67 activeKey = props.activeKey,
68 openMotion = props.openMotion,
69 expandIcon = props.expandIcon;
70 var key = child.key || String(index);
71 var _child$props = child.props,
72 header = _child$props.header,
73 headerClass = _child$props.headerClass,
74 childDestroyInactivePanel = _child$props.destroyInactivePanel,
75 childCollapsible = _child$props.collapsible,
76 childOnItemClick = _child$props.onItemClick;
77 var isActive = false;
78 if (accordion) {
79 isActive = activeKey[0] === key;
80 } else {
81 isActive = activeKey.indexOf(key) > -1;
82 }
83 var mergeCollapsible = childCollapsible !== null && childCollapsible !== void 0 ? childCollapsible : collapsible;
84 var handleItemClick = function handleItemClick(value) {
85 if (mergeCollapsible === 'disabled') return;
86 onItemClick(value);
87 childOnItemClick === null || childOnItemClick === void 0 || childOnItemClick(value);
88 };
89 var childProps = {
90 key: key,
91 panelKey: key,
92 header: header,
93 headerClass: headerClass,
94 isActive: isActive,
95 prefixCls: prefixCls,
96 destroyInactivePanel: childDestroyInactivePanel !== null && childDestroyInactivePanel !== void 0 ? childDestroyInactivePanel : destroyInactivePanel,
97 openMotion: openMotion,
98 accordion: accordion,
99 children: child.props.children,
100 onItemClick: handleItemClick,
101 expandIcon: expandIcon,
102 collapsible: mergeCollapsible
103 };
104
105 // https://github.com/ant-design/ant-design/issues/20479
106 if (typeof child.type === 'string') {
107 return child;
108 }
109 Object.keys(childProps).forEach(function (propName) {
110 if (typeof childProps[propName] === 'undefined') {
111 delete childProps[propName];
112 }
113 });
114 return /*#__PURE__*/React.cloneElement(child, childProps);
115};
116function useItems(items, rawChildren, props) {
117 if (Array.isArray(items)) {
118 return convertItemsToNodes(items, props);
119 }
120 return toArray(rawChildren).map(function (child, index) {
121 return getNewChild(child, index, props);
122 });
123}
124export default useItems;
\No newline at end of file