2018-06-04 11:20:17 +08:00
|
|
|
|
import * as React from 'react';
|
|
|
|
|
import RcTree, { TreeNode } from 'rc-tree';
|
|
|
|
|
import DirectoryTree from './DirectoryTree';
|
|
|
|
|
import classNames from 'classnames';
|
2018-08-17 16:04:11 +08:00
|
|
|
|
import Icon from '../icon';
|
2018-12-05 19:12:18 +08:00
|
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2019-05-06 12:04:39 +08:00
|
|
|
|
import { collapseMotion } from '../_util/motion';
|
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;
|
|
|
|
|
key?: string;
|
|
|
|
|
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;
|
|
|
|
|
selectedNodes?: AntTreeNode[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AntTreeNodeExpandedEvent extends AntTreeNodeBaseEvent {
|
|
|
|
|
expanded?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AntTreeNodeMouseEvent {
|
|
|
|
|
node: AntTreeNode;
|
2019-06-16 20:51:47 +08:00
|
|
|
|
event: React.MouseEvent<HTMLElement>;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-16 22:33:15 +08:00
|
|
|
|
export interface AntTreeNodeDragEnterEvent extends AntTreeNodeMouseEvent {
|
|
|
|
|
expandedKeys: string[];
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 17:37:20 +08:00
|
|
|
|
export interface AntTreeNodeDropEvent {
|
|
|
|
|
node: AntTreeNode;
|
|
|
|
|
dragNode: AntTreeNode;
|
|
|
|
|
dragNodesKeys: string[];
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
|
export interface TreeProps {
|
|
|
|
|
showLine?: boolean;
|
|
|
|
|
className?: string;
|
|
|
|
|
/** 是否支持多选 */
|
|
|
|
|
multiple?: boolean;
|
|
|
|
|
/** 是否自动展开父节点 */
|
|
|
|
|
autoExpandParent?: boolean;
|
|
|
|
|
/** checkable状态下节点选择完全受控(父子节点选中状态不再关联)*/
|
|
|
|
|
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;
|
|
|
|
|
/** 默认展开指定的树节点 */
|
|
|
|
|
defaultExpandedKeys?: string[];
|
|
|
|
|
/** (受控)展开指定的树节点 */
|
|
|
|
|
expandedKeys?: string[];
|
|
|
|
|
/** (受控)选中复选框的树节点 */
|
|
|
|
|
checkedKeys?: string[] | { checked: string[]; halfChecked: string[] };
|
|
|
|
|
/** 默认选中复选框的树节点 */
|
|
|
|
|
defaultCheckedKeys?: string[];
|
|
|
|
|
/** (受控)设置选中的树节点 */
|
|
|
|
|
selectedKeys?: string[];
|
|
|
|
|
/** 默认选中的树节点 */
|
|
|
|
|
defaultSelectedKeys?: string[];
|
2018-06-25 11:44:35 +08:00
|
|
|
|
selectable?: boolean;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** 展开/收起节点时触发 */
|
2019-06-16 20:51:47 +08:00
|
|
|
|
onExpand?: (expandedKeys: string[], info: AntTreeNodeExpandedEvent) => void | PromiseLike<void>;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** 点击复选框触发 */
|
2018-12-07 20:02:01 +08:00
|
|
|
|
onCheck?: (
|
|
|
|
|
checkedKeys: string[] | { checked: string[]; halfChecked: string[] },
|
|
|
|
|
e: AntTreeNodeCheckedEvent,
|
|
|
|
|
) => void;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** 点击树节点触发 */
|
|
|
|
|
onSelect?: (selectedKeys: string[], e: AntTreeNodeSelectedEvent) => void;
|
|
|
|
|
/** 单击树节点触发 */
|
|
|
|
|
onClick?: (e: React.MouseEvent<HTMLElement>, node: AntTreeNode) => void;
|
|
|
|
|
/** 双击树节点触发 */
|
|
|
|
|
onDoubleClick?: (e: React.MouseEvent<HTMLElement>, node: AntTreeNode) => void;
|
|
|
|
|
/** filter some AntTreeNodes as you need. it should return true */
|
|
|
|
|
filterAntTreeNode?: (node: AntTreeNode) => boolean;
|
|
|
|
|
/** 异步加载数据 */
|
2019-06-16 20:51:47 +08:00
|
|
|
|
loadData?: (node: AntTreeNode) => PromiseLike<void>;
|
2018-06-08 16:48:58 +08:00
|
|
|
|
loadedKeys?: string[];
|
2019-03-28 16:20:59 +08:00
|
|
|
|
onLoad?: (loadedKeys: string[], info: { event: 'load'; node: AntTreeNode }) => void;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** 响应右键点击 */
|
|
|
|
|
onRightClick?: (options: AntTreeNodeMouseEvent) => void;
|
|
|
|
|
/** 设置节点可拖拽(IE>8)*/
|
|
|
|
|
draggable?: boolean;
|
|
|
|
|
onDragStart?: (options: AntTreeNodeMouseEvent) => void;
|
2019-05-16 22:33:15 +08:00
|
|
|
|
onDragEnter?: (options: AntTreeNodeDragEnterEvent) => void;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
onDragOver?: (options: AntTreeNodeMouseEvent) => void;
|
|
|
|
|
onDragLeave?: (options: AntTreeNodeMouseEvent) => void;
|
2019-01-02 09:21:45 +08:00
|
|
|
|
onDragEnd?: (options: AntTreeNodeMouseEvent) => void;
|
2018-09-17 17:37:20 +08:00
|
|
|
|
onDrop?: (options: AntTreeNodeDropEvent) => void;
|
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;
|
|
|
|
|
switcherIcon?: React.ReactElement<unknown>;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
prefixCls?: string;
|
|
|
|
|
filterTreeNode?: (node: AntTreeNode) => boolean;
|
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> {
|
2018-06-12 15:34:51 +08:00
|
|
|
|
static TreeNode: React.ComponentClass<AntTreeNodeProps> = TreeNode;
|
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;
|
|
|
|
|
|
2018-12-12 20:25:55 +08:00
|
|
|
|
renderSwitcherIcon = (
|
|
|
|
|
prefixCls: string,
|
|
|
|
|
switcherIcon: React.ReactElement<any> | undefined,
|
|
|
|
|
{ isLeaf, expanded, loading }: AntTreeNodeProps,
|
|
|
|
|
) => {
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const { showLine } = this.props;
|
2018-08-17 16:36:33 +08:00
|
|
|
|
if (loading) {
|
2018-12-07 20:02:01 +08:00
|
|
|
|
return <Icon type="loading" className={`${prefixCls}-switcher-loading-icon`} />;
|
2018-08-17 16:36:33 +08:00
|
|
|
|
}
|
|
|
|
|
if (showLine) {
|
|
|
|
|
if (isLeaf) {
|
2018-12-07 20:02:01 +08:00
|
|
|
|
return <Icon type="file" className={`${prefixCls}-switcher-line-icon`} />;
|
2018-08-17 16:04:11 +08:00
|
|
|
|
}
|
2018-08-17 16:36:33 +08:00
|
|
|
|
return (
|
|
|
|
|
<Icon
|
2018-09-01 20:11:13 +08:00
|
|
|
|
type={expanded ? 'minus-square' : 'plus-square'}
|
2018-08-17 16:36:33 +08:00
|
|
|
|
className={`${prefixCls}-switcher-line-icon`}
|
2018-09-02 18:46:53 +08:00
|
|
|
|
theme="outlined"
|
2018-08-17 16:36:33 +08:00
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2018-12-12 20:25:55 +08:00
|
|
|
|
const switcherCls = `${prefixCls}-switcher-icon`;
|
2018-08-17 16:36:33 +08:00
|
|
|
|
if (isLeaf) {
|
|
|
|
|
return null;
|
2018-12-12 20:25:55 +08:00
|
|
|
|
} else if (switcherIcon) {
|
|
|
|
|
const switcherOriginCls = switcherIcon.props.className || '';
|
|
|
|
|
return React.cloneElement(switcherIcon, {
|
2019-03-08 12:36:46 +08:00
|
|
|
|
className: classNames(switcherOriginCls, switcherCls),
|
2018-12-12 20:25:55 +08:00
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return <Icon type="caret-down" className={switcherCls} theme="filled" />;
|
2018-08-17 16:36:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
|
};
|
2018-08-17 16:36:33 +08:00
|
|
|
|
|
2018-10-10 11:48:43 +08:00
|
|
|
|
setTreeRef = (node: any) => {
|
|
|
|
|
this.tree = node;
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
|
renderTree = ({ getPrefixCls }: ConfigConsumerProps) => {
|
2018-08-17 16:36:33 +08:00
|
|
|
|
const props = this.props;
|
2019-03-08 12:36:46 +08:00
|
|
|
|
const { prefixCls: customizePrefixCls, className, showIcon, switcherIcon, blockNode } = props;
|
2018-11-10 21:40:21 +08:00
|
|
|
|
const checkable = props.checkable;
|
2018-12-05 19:12:18 +08:00
|
|
|
|
const prefixCls = getPrefixCls('tree', customizePrefixCls);
|
2018-06-04 11:20:17 +08:00
|
|
|
|
return (
|
|
|
|
|
<RcTree
|
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,
|
|
|
|
|
})}
|
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) =>
|
2018-12-12 20:25:55 +08:00
|
|
|
|
this.renderSwitcherIcon(prefixCls, switcherIcon, nodeProps)
|
2018-12-07 20:02:01 +08:00
|
|
|
|
}
|
2018-06-04 11:20:17 +08:00
|
|
|
|
>
|
|
|
|
|
{this.props.children}
|
|
|
|
|
</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
|
|
|
|
}
|