2018-06-04 11:20:17 +08:00
|
|
|
|
import * as React from 'react';
|
2020-01-15 12:13:20 +08:00
|
|
|
|
import RcTree, { TreeNode, TreeProps as RcTreeProps } from 'rc-tree';
|
2018-06-04 11:20:17 +08:00
|
|
|
|
import classNames from 'classnames';
|
2020-04-17 23:31:39 +08:00
|
|
|
|
import { DataNode, Key } from 'rc-tree/lib/interface';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
|
import DirectoryTree from './DirectoryTree';
|
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';
|
2019-09-28 11:31:28 +08:00
|
|
|
|
import renderSwitcherIcon from './utils/iconUtil';
|
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
|
|
|
|
|
2020-01-15 12:13:20 +08:00
|
|
|
|
export interface TreeProps extends Omit<RcTreeProps, 'prefixCls'> {
|
2018-06-04 11:20:17 +08:00
|
|
|
|
showLine?: boolean;
|
|
|
|
|
className?: string;
|
|
|
|
|
/** 是否支持多选 */
|
|
|
|
|
multiple?: boolean;
|
|
|
|
|
/** 是否自动展开父节点 */
|
|
|
|
|
autoExpandParent?: boolean;
|
2019-08-05 18:38:10 +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) */
|
2018-06-04 11:20:17 +08:00
|
|
|
|
draggable?: boolean;
|
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
|
showIcon?: boolean;
|
2019-06-16 20:51:47 +08:00
|
|
|
|
icon?: ((nodeProps: AntdTreeNodeAttribute) => React.ReactNode) | React.ReactNode;
|
2019-06-24 11:29:58 +08:00
|
|
|
|
switcherIcon?: React.ReactElement<any>;
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 15:30:13 +08:00
|
|
|
|
interface CompoundedComponent
|
|
|
|
|
extends React.ForwardRefExoticComponent<TreeProps & React.RefAttributes<RcTree>> {
|
|
|
|
|
TreeNode: typeof TreeNode;
|
|
|
|
|
DirectoryTree: typeof DirectoryTree;
|
2018-06-04 11:20:17 +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,
|
|
|
|
|
} = props;
|
|
|
|
|
const prefixCls = getPrefixCls('tree', customizePrefixCls);
|
|
|
|
|
return (
|
|
|
|
|
<RcTree
|
|
|
|
|
itemHeight={20}
|
|
|
|
|
ref={ref}
|
2020-05-05 15:00:43 +08:00
|
|
|
|
virtual={virtual}
|
2020-04-25 15:30:13 +08:00
|
|
|
|
{...props}
|
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
|
className={classNames(className, {
|
|
|
|
|
[`${prefixCls}-icon-hide`]: !showIcon,
|
|
|
|
|
[`${prefixCls}-block-node`]: blockNode,
|
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
|
})}
|
|
|
|
|
checkable={checkable ? <span className={`${prefixCls}-checkbox-inner`} /> : checkable}
|
|
|
|
|
switcherIcon={(nodeProps: AntTreeNodeProps) =>
|
|
|
|
|
renderSwitcherIcon(prefixCls, switcherIcon, showLine, nodeProps)
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</RcTree>
|
|
|
|
|
);
|
|
|
|
|
}) as CompoundedComponent;
|
|
|
|
|
|
|
|
|
|
Tree.TreeNode = TreeNode;
|
|
|
|
|
|
|
|
|
|
Tree.DirectoryTree = DirectoryTree;
|
|
|
|
|
|
|
|
|
|
Tree.defaultProps = {
|
|
|
|
|
checkable: false,
|
|
|
|
|
showIcon: false,
|
|
|
|
|
motion: {
|
|
|
|
|
...collapseMotion,
|
|
|
|
|
motionAppear: false,
|
|
|
|
|
},
|
|
|
|
|
blockNode: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Tree;
|