UNPKG

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