useItems.js 5.1 KB

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