import type { Component } from 'react'; import React from 'react'; import HolderOutlined from '@ant-design/icons/HolderOutlined'; import classNames from 'classnames'; import type { CSSMotionProps } from 'rc-motion'; import type { BasicDataNode, TreeProps as RcTreeProps } from 'rc-tree'; import RcTree from 'rc-tree'; import type { DataNode, Key } from 'rc-tree/lib/interface'; import initCollapseMotion from '../_util/motion'; import { ConfigContext } from '../config-provider'; import { useToken } from '../theme/internal'; import useStyle from './style'; import dropIndicatorRender from './utils/dropIndicator'; import SwitcherIconCom from './utils/iconUtil'; export type SwitcherIcon = React.ReactNode | ((props: AntTreeNodeProps) => React.ReactNode); export type TreeLeafIcon = React.ReactNode | ((props: AntTreeNodeProps) => React.ReactNode); export interface AntdTreeNodeAttribute { eventKey: string; prefixCls: string; className: string; expanded: boolean; selected: boolean; checked: boolean; halfChecked: boolean; children: React.ReactNode; title: React.ReactNode; pos: string; dragOver: boolean; dragOverGapTop: boolean; dragOverGapBottom: boolean; isLeaf: boolean; selectable: boolean; disabled: boolean; disableCheckbox: boolean; } export interface AntTreeNodeProps { className?: string; checkable?: boolean; disabled?: boolean; disableCheckbox?: boolean; title?: string | React.ReactNode; key?: Key; eventKey?: string; isLeaf?: boolean; checked?: boolean; expanded?: boolean; loading?: boolean; selected?: boolean; selectable?: boolean; icon?: ((treeNode: AntdTreeNodeAttribute) => React.ReactNode) | React.ReactNode; children?: React.ReactNode; [customProp: string]: any; } export interface AntTreeNode extends Component {} export interface AntTreeNodeBaseEvent { node: AntTreeNode; nativeEvent: MouseEvent; } export interface AntTreeNodeCheckedEvent extends AntTreeNodeBaseEvent { event: 'check'; checked?: boolean; checkedNodes?: AntTreeNode[]; } export interface AntTreeNodeSelectedEvent extends AntTreeNodeBaseEvent { event: 'select'; selected?: boolean; selectedNodes?: DataNode[]; } export interface AntTreeNodeExpandedEvent extends AntTreeNodeBaseEvent { expanded?: boolean; } export interface AntTreeNodeMouseEvent { node: AntTreeNode; event: React.DragEvent; } export interface AntTreeNodeDragEnterEvent extends AntTreeNodeMouseEvent { expandedKeys: Key[]; } export interface AntTreeNodeDropEvent { node: AntTreeNode; dragNode: AntTreeNode; dragNodesKeys: Key[]; dropPosition: number; dropToGap?: boolean; event: React.MouseEvent; } // [Legacy] Compatible for v3 export type TreeNodeNormal = DataNode; type DraggableFn = (node: DataNode) => boolean; interface DraggableConfig { icon?: React.ReactNode | false; nodeDraggable?: DraggableFn; } export interface TreeProps extends Omit< RcTreeProps, 'prefixCls' | 'showLine' | 'direction' | 'draggable' | 'icon' | 'switcherIcon' > { showLine?: boolean | { showLeafIcon: boolean | TreeLeafIcon }; className?: string; /** Whether to support multiple selection */ multiple?: boolean; /** Whether to automatically expand the parent node */ autoExpandParent?: boolean; /** Node selection in Checkable state is fully controlled (the selected state of parent and child nodes is no longer associated) */ checkStrictly?: boolean; /** Whether to support selection */ checkable?: boolean; /** whether to disable the tree */ disabled?: boolean; /** Expand all tree nodes by default */ defaultExpandAll?: boolean; /** Expand the corresponding tree node by default */ defaultExpandParent?: boolean; /** Expand the specified tree node by default */ defaultExpandedKeys?: Key[]; /** (Controlled) Expand the specified tree node */ expandedKeys?: Key[]; /** (Controlled) Tree node with checked checkbox */ checkedKeys?: Key[] | { checked: Key[]; halfChecked: Key[] }; /** Tree node with checkbox checked by default */ defaultCheckedKeys?: Key[]; /** (Controlled) Set the selected tree node */ selectedKeys?: Key[]; /** Tree node selected by default */ defaultSelectedKeys?: Key[]; selectable?: boolean; /** Click on the tree node to trigger */ filterAntTreeNode?: (node: AntTreeNode) => boolean; loadedKeys?: Key[]; /** Set the node to be draggable (IE>8) */ draggable?: DraggableFn | boolean | DraggableConfig; style?: React.CSSProperties; showIcon?: boolean; icon?: | ((nodeProps: AntdTreeNodeAttribute) => React.ReactNode) | React.ReactNode | RcTreeProps['icon']; switcherIcon?: SwitcherIcon | RcTreeProps['switcherIcon']; prefixCls?: string; children?: React.ReactNode; blockNode?: boolean; } const Tree = React.forwardRef((props, ref) => { const { getPrefixCls, direction, virtual, tree } = React.useContext(ConfigContext); const { prefixCls: customizePrefixCls, className, showIcon = false, showLine, switcherIcon, blockNode = false, children, checkable = false, selectable = true, draggable, motion: customMotion, style, } = props; const prefixCls = getPrefixCls('tree', customizePrefixCls); const rootPrefixCls = getPrefixCls(); const motion: CSSMotionProps = customMotion ?? { ...initCollapseMotion(rootPrefixCls), motionAppear: false, }; const newProps = { ...props, checkable, selectable, showIcon, motion, blockNode, showLine: Boolean(showLine), dropIndicatorRender, }; const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls); const [, token] = useToken(); const itemHeight = token.paddingXS / 2 + (token.Tree?.titleHeight || token.controlHeightSM); const draggableConfig = React.useMemo(() => { if (!draggable) { return false; } let mergedDraggable: DraggableConfig = {}; switch (typeof draggable) { case 'function': mergedDraggable.nodeDraggable = draggable; break; case 'object': mergedDraggable = { ...draggable }; break; default: break; // Do nothing } if (mergedDraggable.icon !== false) { mergedDraggable.icon = mergedDraggable.icon || ; } return mergedDraggable; }, [draggable]); const renderSwitcherIcon: React.FC = (nodeProps) => ( ); return wrapCSSVar( // @ts-ignore : checkable} selectable={selectable} switcherIcon={renderSwitcherIcon as RcTreeProps['switcherIcon']} draggable={draggableConfig} > {children} , ); }); if (process.env.NODE_ENV !== 'production') { Tree.displayName = 'Tree'; } export default Tree;