Tree.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use client";
  2. import React from 'react';
  3. import HolderOutlined from "@ant-design/icons/es/icons/HolderOutlined";
  4. import classNames from 'classnames';
  5. import RcTree from 'rc-tree';
  6. import initCollapseMotion from '../_util/motion';
  7. import { ConfigContext } from '../config-provider';
  8. import { useToken } from '../theme/internal';
  9. import useStyle from './style';
  10. import dropIndicatorRender from './utils/dropIndicator';
  11. import SwitcherIconCom from './utils/iconUtil';
  12. const Tree = /*#__PURE__*/React.forwardRef((props, ref) => {
  13. var _a;
  14. const {
  15. getPrefixCls,
  16. direction,
  17. virtual,
  18. tree
  19. } = React.useContext(ConfigContext);
  20. const {
  21. prefixCls: customizePrefixCls,
  22. className,
  23. showIcon = false,
  24. showLine,
  25. switcherIcon,
  26. switcherLoadingIcon,
  27. blockNode = false,
  28. children,
  29. checkable = false,
  30. selectable = true,
  31. draggable,
  32. motion: customMotion,
  33. style
  34. } = props;
  35. const prefixCls = getPrefixCls('tree', customizePrefixCls);
  36. const rootPrefixCls = getPrefixCls();
  37. const motion = customMotion !== null && customMotion !== void 0 ? customMotion : Object.assign(Object.assign({}, initCollapseMotion(rootPrefixCls)), {
  38. motionAppear: false
  39. });
  40. const newProps = Object.assign(Object.assign({}, props), {
  41. checkable,
  42. selectable,
  43. showIcon,
  44. motion,
  45. blockNode,
  46. showLine: Boolean(showLine),
  47. dropIndicatorRender
  48. });
  49. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
  50. const [, token] = useToken();
  51. const itemHeight = token.paddingXS / 2 + (((_a = token.Tree) === null || _a === void 0 ? void 0 : _a.titleHeight) || token.controlHeightSM);
  52. const draggableConfig = React.useMemo(() => {
  53. if (!draggable) {
  54. return false;
  55. }
  56. let mergedDraggable = {};
  57. switch (typeof draggable) {
  58. case 'function':
  59. mergedDraggable.nodeDraggable = draggable;
  60. break;
  61. case 'object':
  62. mergedDraggable = Object.assign({}, draggable);
  63. break;
  64. default:
  65. break;
  66. // Do nothing
  67. }
  68. if (mergedDraggable.icon !== false) {
  69. mergedDraggable.icon = mergedDraggable.icon || /*#__PURE__*/React.createElement(HolderOutlined, null);
  70. }
  71. return mergedDraggable;
  72. }, [draggable]);
  73. const renderSwitcherIcon = nodeProps => (/*#__PURE__*/React.createElement(SwitcherIconCom, {
  74. prefixCls: prefixCls,
  75. switcherIcon: switcherIcon,
  76. switcherLoadingIcon: switcherLoadingIcon,
  77. treeNodeProps: nodeProps,
  78. showLine: showLine
  79. }));
  80. return wrapCSSVar(
  81. /*#__PURE__*/
  82. // @ts-ignore
  83. React.createElement(RcTree, Object.assign({
  84. itemHeight: itemHeight,
  85. ref: ref,
  86. virtual: virtual
  87. }, newProps, {
  88. // newProps may contain style so declare style below it
  89. style: Object.assign(Object.assign({}, tree === null || tree === void 0 ? void 0 : tree.style), style),
  90. prefixCls: prefixCls,
  91. className: classNames({
  92. [`${prefixCls}-icon-hide`]: !showIcon,
  93. [`${prefixCls}-block-node`]: blockNode,
  94. [`${prefixCls}-unselectable`]: !selectable,
  95. [`${prefixCls}-rtl`]: direction === 'rtl'
  96. }, tree === null || tree === void 0 ? void 0 : tree.className, className, hashId, cssVarCls),
  97. direction: direction,
  98. checkable: checkable ? /*#__PURE__*/React.createElement("span", {
  99. className: `${prefixCls}-checkbox-inner`
  100. }) : checkable,
  101. selectable: selectable,
  102. switcherIcon: renderSwitcherIcon,
  103. draggable: draggableConfig
  104. }), children));
  105. });
  106. if (process.env.NODE_ENV !== 'production') {
  107. Tree.displayName = 'Tree';
  108. }
  109. export default Tree;