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';
|
2018-12-05 19:12:18 +08:00
|
|
|
|
import { ConfigConsumer, ConfigConsumerProps } 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class Tree extends React.Component<TreeProps, any> {
|
2020-01-15 12:13:20 +08:00
|
|
|
|
static TreeNode = TreeNode;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
|
static DirectoryTree = DirectoryTree;
|
|
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
|
checkable: false,
|
|
|
|
|
showIcon: false,
|
2019-05-06 12:04:39 +08:00
|
|
|
|
motion: {
|
|
|
|
|
...collapseMotion,
|
|
|
|
|
motionAppear: false,
|
2018-07-30 12:05:29 +08:00
|
|
|
|
},
|
2019-03-08 12:36:46 +08:00
|
|
|
|
blockNode: false,
|
2018-06-04 11:20:17 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-10 11:48:43 +08:00
|
|
|
|
tree: any;
|
|
|
|
|
|
|
|
|
|
setTreeRef = (node: any) => {
|
|
|
|
|
this.tree = node;
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
|
renderTree = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
|
const { props } = this;
|
|
|
|
|
const {
|
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
|
className,
|
|
|
|
|
showIcon,
|
2019-09-28 11:31:28 +08:00
|
|
|
|
showLine,
|
2019-08-05 18:38:10 +08:00
|
|
|
|
switcherIcon,
|
|
|
|
|
blockNode,
|
|
|
|
|
children,
|
|
|
|
|
} = props;
|
|
|
|
|
const { checkable } = props;
|
2018-12-05 19:12:18 +08:00
|
|
|
|
const prefixCls = getPrefixCls('tree', customizePrefixCls);
|
2018-06-04 11:20:17 +08:00
|
|
|
|
return (
|
|
|
|
|
<RcTree
|
2019-12-31 15:45:28 +08:00
|
|
|
|
itemHeight={20}
|
2018-10-10 11:48:43 +08:00
|
|
|
|
ref={this.setTreeRef}
|
2018-06-04 11:20:17 +08:00
|
|
|
|
{...props}
|
2018-12-05 19:12:18 +08:00
|
|
|
|
prefixCls={prefixCls}
|
2019-03-08 12:36:46 +08:00
|
|
|
|
className={classNames(className, {
|
|
|
|
|
[`${prefixCls}-icon-hide`]: !showIcon,
|
|
|
|
|
[`${prefixCls}-block-node`]: blockNode,
|
2020-01-02 19:10:16 +08:00
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2019-03-08 12:36:46 +08:00
|
|
|
|
})}
|
2018-06-04 11:20:17 +08:00
|
|
|
|
checkable={checkable ? <span className={`${prefixCls}-checkbox-inner`} /> : checkable}
|
2018-12-07 20:02:01 +08:00
|
|
|
|
switcherIcon={(nodeProps: AntTreeNodeProps) =>
|
2019-09-28 11:31:28 +08:00
|
|
|
|
renderSwitcherIcon(prefixCls, switcherIcon, showLine, nodeProps)
|
2018-12-07 20:02:01 +08:00
|
|
|
|
}
|
2018-06-04 11:20:17 +08:00
|
|
|
|
>
|
2019-08-05 18:38:10 +08:00
|
|
|
|
{children}
|
2018-06-04 11:20:17 +08:00
|
|
|
|
</RcTree>
|
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
|
return <ConfigConsumer>{this.renderTree}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
|
}
|
2018-06-04 11:20:17 +08:00
|
|
|
|
}
|