2021-09-27 15:54:06 +08:00
|
|
|
|
import HolderOutlined from '@ant-design/icons/HolderOutlined';
|
2018-06-04 11:20:17 +08:00
|
|
|
|
import classNames from 'classnames';
|
2022-06-22 14:57:09 +08:00
|
|
|
|
import type { BasicDataNode, TreeProps as RcTreeProps } from 'rc-tree';
|
|
|
|
|
import RcTree, { TreeNode } from 'rc-tree';
|
2022-05-07 14:31:54 +08:00
|
|
|
|
import type { DataNode, Key } from 'rc-tree/lib/interface';
|
2022-06-22 14:57:09 +08:00
|
|
|
|
import * as React from 'react';
|
2020-04-25 15:30:13 +08:00
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2019-08-05 18:38:10 +08:00
|
|
|
|
import collapseMotion from '../_util/motion';
|
2022-06-22 14:57:09 +08:00
|
|
|
|
import DirectoryTree from './DirectoryTree';
|
2020-11-29 14:15:08 +08:00
|
|
|
|
import dropIndicatorRender from './utils/dropIndicator';
|
2022-06-22 14:57:09 +08:00
|
|
|
|
import renderSwitcherIcon from './utils/iconUtil';
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
2022-04-11 18:15:03 +08:00
|
|
|
|
export type SwitcherIcon = React.ReactNode | ((props: { expanded: boolean }) => React.ReactNode);
|
|
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2019-05-16 23:32:16 +08:00
|
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
|
export interface AntTreeNodeProps {
|
2018-06-25 11:44:35 +08:00
|
|
|
|
className?: string;
|
2019-05-06 12:04:39 +08:00
|
|
|
|
checkable?: boolean;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
disabled?: boolean;
|
|
|
|
|
disableCheckbox?: boolean;
|
|
|
|
|
title?: string | React.ReactNode;
|
2020-04-17 23:31:39 +08:00
|
|
|
|
key?: Key;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
eventKey?: string;
|
|
|
|
|
isLeaf?: boolean;
|
|
|
|
|
checked?: boolean;
|
|
|
|
|
expanded?: boolean;
|
2018-08-17 16:36:33 +08:00
|
|
|
|
loading?: boolean;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
selected?: boolean;
|
2018-08-13 14:09:32 +08:00
|
|
|
|
selectable?: boolean;
|
2018-06-08 16:22:15 +08:00
|
|
|
|
icon?: ((treeNode: AntdTreeNodeAttribute) => React.ReactNode) | React.ReactNode;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
children?: React.ReactNode;
|
2018-08-23 20:38:56 +08:00
|
|
|
|
[customProp: string]: any;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
|
export interface AntTreeNode extends React.Component<AntTreeNodeProps, {}> {}
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
|
|
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;
|
2019-08-12 13:22:36 +08:00
|
|
|
|
selectedNodes?: DataNode[];
|
2018-06-04 11:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AntTreeNodeExpandedEvent extends AntTreeNodeBaseEvent {
|
|
|
|
|
expanded?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AntTreeNodeMouseEvent {
|
|
|
|
|
node: AntTreeNode;
|
2020-01-25 00:22:28 +08:00
|
|
|
|
event: React.DragEvent<HTMLElement>;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-16 22:33:15 +08:00
|
|
|
|
export interface AntTreeNodeDragEnterEvent extends AntTreeNodeMouseEvent {
|
2020-04-17 23:31:39 +08:00
|
|
|
|
expandedKeys: Key[];
|
2019-05-16 22:33:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 17:37:20 +08:00
|
|
|
|
export interface AntTreeNodeDropEvent {
|
|
|
|
|
node: AntTreeNode;
|
|
|
|
|
dragNode: AntTreeNode;
|
2020-04-17 23:31:39 +08:00
|
|
|
|
dragNodesKeys: Key[];
|
2018-11-15 05:45:01 +08:00
|
|
|
|
dropPosition: number;
|
|
|
|
|
dropToGap?: boolean;
|
2019-06-16 20:51:47 +08:00
|
|
|
|
event: React.MouseEvent<HTMLElement>;
|
2018-09-17 17:37:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 13:52:42 +08:00
|
|
|
|
// [Legacy] Compatible for v3
|
|
|
|
|
export type TreeNodeNormal = DataNode;
|
2019-06-28 03:28:00 +08:00
|
|
|
|
|
2021-09-27 15:54:06 +08:00
|
|
|
|
type DraggableFn = (node: AntTreeNode) => boolean;
|
|
|
|
|
interface DraggableConfig {
|
|
|
|
|
icon?: React.ReactNode | false;
|
|
|
|
|
nodeDraggable?: DraggableFn;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 23:05:49 +08:00
|
|
|
|
export interface TreeProps<T extends BasicDataNode = DataNode>
|
|
|
|
|
extends Omit<RcTreeProps<T>, 'prefixCls' | 'showLine' | 'direction' | 'draggable'> {
|
2020-06-29 11:04:58 +08:00
|
|
|
|
showLine?: boolean | { showLeafIcon: boolean };
|
2018-06-04 11:20:17 +08:00
|
|
|
|
className?: string;
|
|
|
|
|
/** 是否支持多选 */
|
|
|
|
|
multiple?: boolean;
|
|
|
|
|
/** 是否自动展开父节点 */
|
|
|
|
|
autoExpandParent?: boolean;
|
2020-12-28 15:30:18 +08:00
|
|
|
|
/** Checkable状态下节点选择完全受控(父子节点选中状态不再关联) */
|
2018-06-04 11:20:17 +08:00
|
|
|
|
checkStrictly?: boolean;
|
|
|
|
|
/** 是否支持选中 */
|
|
|
|
|
checkable?: boolean;
|
2018-07-11 22:21:01 +08:00
|
|
|
|
/** 是否禁用树 */
|
|
|
|
|
disabled?: boolean;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** 默认展开所有树节点 */
|
|
|
|
|
defaultExpandAll?: boolean;
|
|
|
|
|
/** 默认展开对应树节点 */
|
|
|
|
|
defaultExpandParent?: boolean;
|
|
|
|
|
/** 默认展开指定的树节点 */
|
2020-04-17 23:31:39 +08:00
|
|
|
|
defaultExpandedKeys?: Key[];
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** (受控)展开指定的树节点 */
|
2020-04-17 23:31:39 +08:00
|
|
|
|
expandedKeys?: Key[];
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** (受控)选中复选框的树节点 */
|
2020-04-17 23:31:39 +08:00
|
|
|
|
checkedKeys?: Key[] | { checked: Key[]; halfChecked: Key[] };
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** 默认选中复选框的树节点 */
|
2020-04-17 23:31:39 +08:00
|
|
|
|
defaultCheckedKeys?: Key[];
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** (受控)设置选中的树节点 */
|
2020-04-17 23:31:39 +08:00
|
|
|
|
selectedKeys?: Key[];
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** 默认选中的树节点 */
|
2020-04-17 23:31:39 +08:00
|
|
|
|
defaultSelectedKeys?: Key[];
|
2018-06-25 11:44:35 +08:00
|
|
|
|
selectable?: boolean;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** 点击树节点触发 */
|
|
|
|
|
filterAntTreeNode?: (node: AntTreeNode) => boolean;
|
2020-04-17 23:31:39 +08:00
|
|
|
|
loadedKeys?: Key[];
|
2019-08-05 18:38:10 +08:00
|
|
|
|
/** 设置节点可拖拽(IE>8) */
|
2021-09-27 15:54:06 +08:00
|
|
|
|
draggable?: DraggableFn | boolean | DraggableConfig;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
|
showIcon?: boolean;
|
2019-06-16 20:51:47 +08:00
|
|
|
|
icon?: ((nodeProps: AntdTreeNodeAttribute) => React.ReactNode) | React.ReactNode;
|
2022-04-01 17:10:38 +08:00
|
|
|
|
switcherIcon?: SwitcherIcon;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
prefixCls?: string;
|
2019-06-16 20:51:47 +08:00
|
|
|
|
children?: React.ReactNode;
|
2019-03-08 12:36:46 +08:00
|
|
|
|
blockNode?: boolean;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 23:05:49 +08:00
|
|
|
|
type CompoundedComponent = (<T extends BasicDataNode | DataNode = DataNode>(
|
|
|
|
|
props: React.PropsWithChildren<TreeProps<T>> & { ref?: React.Ref<RcTree> },
|
|
|
|
|
) => React.ReactElement) & {
|
|
|
|
|
defaultProps: Partial<React.PropsWithChildren<TreeProps<any>>>;
|
2020-04-25 15:30:13 +08:00
|
|
|
|
TreeNode: typeof TreeNode;
|
|
|
|
|
DirectoryTree: typeof DirectoryTree;
|
2021-11-23 23:05:49 +08:00
|
|
|
|
};
|
2020-04-25 15:30:13 +08:00
|
|
|
|
|
|
|
|
|
const Tree = React.forwardRef<RcTree, TreeProps>((props, ref) => {
|
2020-05-05 15:00:43 +08:00
|
|
|
|
const { getPrefixCls, direction, virtual } = React.useContext(ConfigContext);
|
2020-04-25 15:30:13 +08:00
|
|
|
|
const {
|
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
|
className,
|
|
|
|
|
showIcon,
|
|
|
|
|
showLine,
|
|
|
|
|
switcherIcon,
|
|
|
|
|
blockNode,
|
|
|
|
|
children,
|
|
|
|
|
checkable,
|
2020-12-09 16:36:14 +08:00
|
|
|
|
selectable,
|
2021-09-27 15:54:06 +08:00
|
|
|
|
draggable,
|
2020-04-25 15:30:13 +08:00
|
|
|
|
} = props;
|
2020-11-29 14:15:08 +08:00
|
|
|
|
const prefixCls = getPrefixCls('tree', customizePrefixCls);
|
2020-06-29 11:04:58 +08:00
|
|
|
|
const newProps = {
|
|
|
|
|
...props,
|
|
|
|
|
showLine: Boolean(showLine),
|
2020-11-29 14:15:08 +08:00
|
|
|
|
dropIndicatorRender,
|
2020-06-29 11:04:58 +08:00
|
|
|
|
};
|
2021-09-27 15:54:06 +08:00
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mergedDraggable.icon !== false) {
|
|
|
|
|
mergedDraggable.icon = mergedDraggable.icon || <HolderOutlined />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mergedDraggable;
|
|
|
|
|
}, [draggable]);
|
|
|
|
|
|
2020-04-25 15:30:13 +08:00
|
|
|
|
return (
|
|
|
|
|
<RcTree
|
|
|
|
|
itemHeight={20}
|
|
|
|
|
ref={ref}
|
2020-05-05 15:00:43 +08:00
|
|
|
|
virtual={virtual}
|
2020-06-29 11:04:58 +08:00
|
|
|
|
{...newProps}
|
2020-04-25 15:30:13 +08:00
|
|
|
|
prefixCls={prefixCls}
|
2020-09-06 13:07:39 +08:00
|
|
|
|
className={classNames(
|
|
|
|
|
{
|
|
|
|
|
[`${prefixCls}-icon-hide`]: !showIcon,
|
|
|
|
|
[`${prefixCls}-block-node`]: blockNode,
|
2020-12-09 16:36:14 +08:00
|
|
|
|
[`${prefixCls}-unselectable`]: !selectable,
|
2020-09-06 13:07:39 +08:00
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
|
},
|
|
|
|
|
className,
|
|
|
|
|
)}
|
2020-11-29 14:15:08 +08:00
|
|
|
|
direction={direction}
|
2020-04-25 15:30:13 +08:00
|
|
|
|
checkable={checkable ? <span className={`${prefixCls}-checkbox-inner`} /> : checkable}
|
2020-12-09 16:36:14 +08:00
|
|
|
|
selectable={selectable}
|
2020-04-25 15:30:13 +08:00
|
|
|
|
switcherIcon={(nodeProps: AntTreeNodeProps) =>
|
|
|
|
|
renderSwitcherIcon(prefixCls, switcherIcon, showLine, nodeProps)
|
|
|
|
|
}
|
2021-09-27 15:54:06 +08:00
|
|
|
|
draggable={draggableConfig as any}
|
2020-04-25 15:30:13 +08:00
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</RcTree>
|
|
|
|
|
);
|
2021-11-23 23:05:49 +08:00
|
|
|
|
}) as unknown as CompoundedComponent;
|
2020-04-25 15:30:13 +08:00
|
|
|
|
|
|
|
|
|
Tree.TreeNode = TreeNode;
|
|
|
|
|
|
|
|
|
|
Tree.DirectoryTree = DirectoryTree;
|
|
|
|
|
|
|
|
|
|
Tree.defaultProps = {
|
|
|
|
|
checkable: false,
|
2020-12-09 16:36:14 +08:00
|
|
|
|
selectable: true,
|
2020-04-25 15:30:13 +08:00
|
|
|
|
showIcon: false,
|
|
|
|
|
motion: {
|
|
|
|
|
...collapseMotion,
|
|
|
|
|
motionAppear: false,
|
|
|
|
|
},
|
|
|
|
|
blockNode: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Tree;
|