UNPKG

3.48 kBJavaScriptView Raw
1"use client";
2
3import React from 'react';
4import HolderOutlined from "@ant-design/icons/es/icons/HolderOutlined";
5import classNames from 'classnames';
6import RcTree from 'rc-tree';
7import initCollapseMotion from '../_util/motion';
8import { ConfigContext } from '../config-provider';
9import useStyle from './style';
10import dropIndicatorRender from './utils/dropIndicator';
11import SwitcherIconCom from './utils/iconUtil';
12import { useToken } from '../theme/internal';
13const Tree = /*#__PURE__*/React.forwardRef((props, ref) => {
14 var _a;
15 const {
16 getPrefixCls,
17 direction,
18 virtual,
19 tree
20 } = React.useContext(ConfigContext);
21 const {
22 prefixCls: customizePrefixCls,
23 className,
24 showIcon = false,
25 showLine,
26 switcherIcon,
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 treeNodeProps: nodeProps,
77 showLine: showLine
78 }));
79 return wrapCSSVar( /*#__PURE__*/React.createElement(RcTree, Object.assign({
80 itemHeight: itemHeight,
81 ref: ref,
82 virtual: virtual
83 }, newProps, {
84 // newProps may contain style so declare style below it
85 style: Object.assign(Object.assign({}, tree === null || tree === void 0 ? void 0 : tree.style), style),
86 prefixCls: prefixCls,
87 className: classNames({
88 [`${prefixCls}-icon-hide`]: !showIcon,
89 [`${prefixCls}-block-node`]: blockNode,
90 [`${prefixCls}-unselectable`]: !selectable,
91 [`${prefixCls}-rtl`]: direction === 'rtl'
92 }, tree === null || tree === void 0 ? void 0 : tree.className, className, hashId, cssVarCls),
93 direction: direction,
94 checkable: checkable ? /*#__PURE__*/React.createElement("span", {
95 className: `${prefixCls}-checkbox-inner`
96 }) : checkable,
97 selectable: selectable,
98 switcherIcon: renderSwitcherIcon,
99 draggable: draggableConfig
100 }), children));
101});
102if (process.env.NODE_ENV !== 'production') {
103 Tree.displayName = 'Tree';
104}
105export default Tree;
\No newline at end of file