useItems.js 4.7 KB

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