2018-06-04 11:20:17 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import debounce from 'lodash/debounce';
|
2019-08-12 13:22:36 +08:00
|
|
|
import { conductExpandParent } from 'rc-tree/lib/util';
|
2020-01-15 12:13:20 +08:00
|
|
|
import { EventDataNode, DataNode } from 'rc-tree/lib/interface';
|
2019-08-12 13:22:36 +08:00
|
|
|
import { convertDataToEntities, convertTreeToData } from 'rc-tree/lib/utils/treeUtil';
|
2019-11-28 12:34:33 +08:00
|
|
|
import { FileOutlined, FolderOpenOutlined, FolderOutlined } from '@ant-design/icons';
|
2019-08-05 18:38:10 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2020-01-15 12:13:20 +08:00
|
|
|
import Tree, { TreeProps, AntdTreeNodeAttribute } from './Tree';
|
2019-09-28 11:31:28 +08:00
|
|
|
import { calcRangeKeys, convertDirectoryKeysToNodes } from './utils/dictUtil';
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
export type ExpandAction = false | 'click' | 'doubleClick';
|
|
|
|
|
|
|
|
export interface DirectoryTreeProps extends TreeProps {
|
|
|
|
expandAction?: ExpandAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DirectoryTreeState {
|
|
|
|
expandedKeys?: string[];
|
|
|
|
selectedKeys?: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIcon(props: AntdTreeNodeAttribute): React.ReactNode {
|
|
|
|
const { isLeaf, expanded } = props;
|
|
|
|
if (isLeaf) {
|
2019-11-28 12:34:33 +08:00
|
|
|
return <FileOutlined />;
|
2018-06-04 11:20:17 +08:00
|
|
|
}
|
2019-11-28 12:34:33 +08:00
|
|
|
return expanded ? <FolderOpenOutlined /> : <FolderOutlined />;
|
2018-06-04 11:20:17 +08:00
|
|
|
}
|
|
|
|
|
2019-08-12 13:22:36 +08:00
|
|
|
function getTreeData({ treeData, children }: DirectoryTreeProps) {
|
|
|
|
return treeData || convertTreeToData(children);
|
|
|
|
}
|
|
|
|
|
2019-02-19 10:02:06 +08:00
|
|
|
class DirectoryTree extends React.Component<DirectoryTreeProps, DirectoryTreeState> {
|
2018-06-04 11:20:17 +08:00
|
|
|
static defaultProps = {
|
|
|
|
showIcon: true,
|
|
|
|
expandAction: 'click',
|
|
|
|
};
|
|
|
|
|
2019-02-19 08:32:29 +08:00
|
|
|
static getDerivedStateFromProps(nextProps: DirectoryTreeProps) {
|
2019-02-21 20:49:04 +08:00
|
|
|
const newState: DirectoryTreeState = {};
|
2019-02-19 08:32:29 +08:00
|
|
|
if ('expandedKeys' in nextProps) {
|
2019-02-21 20:49:04 +08:00
|
|
|
newState.expandedKeys = nextProps.expandedKeys;
|
2019-02-19 08:32:29 +08:00
|
|
|
}
|
|
|
|
if ('selectedKeys' in nextProps) {
|
2019-02-21 20:49:04 +08:00
|
|
|
newState.selectedKeys = nextProps.selectedKeys;
|
2019-02-19 08:32:29 +08:00
|
|
|
}
|
2019-02-21 20:49:04 +08:00
|
|
|
return newState;
|
2019-02-19 08:32:29 +08:00
|
|
|
}
|
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
state: DirectoryTreeState;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2018-10-10 11:48:43 +08:00
|
|
|
tree: Tree;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-01-15 12:13:20 +08:00
|
|
|
onDebounceExpand: (event: React.MouseEvent<HTMLElement>, node: EventDataNode) => void;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
// Shift click usage
|
|
|
|
lastSelectedKey?: string;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
cachedSelectedKeys?: string[];
|
|
|
|
|
|
|
|
constructor(props: DirectoryTreeProps) {
|
|
|
|
super(props);
|
|
|
|
|
2019-08-12 13:22:36 +08:00
|
|
|
const { defaultExpandAll, defaultExpandParent, expandedKeys, defaultExpandedKeys } = props;
|
|
|
|
const { keyEntities } = convertDataToEntities(getTreeData(props));
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
// Selected keys
|
|
|
|
this.state = {
|
|
|
|
selectedKeys: props.selectedKeys || props.defaultSelectedKeys || [],
|
|
|
|
};
|
|
|
|
|
|
|
|
// Expanded keys
|
|
|
|
if (defaultExpandAll) {
|
2019-08-12 13:22:36 +08:00
|
|
|
this.state.expandedKeys = Object.keys(keyEntities);
|
2018-06-04 11:20:17 +08:00
|
|
|
} else if (defaultExpandParent) {
|
2018-12-07 20:02:01 +08:00
|
|
|
this.state.expandedKeys = conductExpandParent(
|
|
|
|
expandedKeys || defaultExpandedKeys,
|
|
|
|
keyEntities,
|
|
|
|
);
|
2018-06-04 11:20:17 +08:00
|
|
|
} else {
|
2018-09-25 15:29:07 +08:00
|
|
|
this.state.expandedKeys = expandedKeys || defaultExpandedKeys;
|
2018-06-04 11:20:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.onDebounceExpand = debounce(this.expandFolderNode, 200, {
|
|
|
|
leading: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-15 12:13:20 +08:00
|
|
|
onExpand = (
|
|
|
|
expandedKeys: string[],
|
|
|
|
info: {
|
|
|
|
node: EventDataNode;
|
|
|
|
expanded: boolean;
|
|
|
|
nativeEvent: MouseEvent;
|
|
|
|
},
|
|
|
|
) => {
|
2018-06-04 11:20:17 +08:00
|
|
|
const { onExpand } = this.props;
|
|
|
|
|
|
|
|
this.setUncontrolledState({ expandedKeys });
|
|
|
|
|
|
|
|
// Call origin function
|
|
|
|
if (onExpand) {
|
|
|
|
return onExpand(expandedKeys, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2020-01-15 12:13:20 +08:00
|
|
|
onClick = (event: React.MouseEvent<HTMLElement>, node: EventDataNode) => {
|
2018-06-04 11:20:17 +08:00
|
|
|
const { onClick, expandAction } = this.props;
|
|
|
|
|
|
|
|
// Expand the tree
|
|
|
|
if (expandAction === 'click') {
|
|
|
|
this.onDebounceExpand(event, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onClick) {
|
|
|
|
onClick(event, node);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2020-01-15 12:13:20 +08:00
|
|
|
onDoubleClick = (event: React.MouseEvent<HTMLElement>, node: EventDataNode) => {
|
2018-06-04 11:20:17 +08:00
|
|
|
const { onDoubleClick, expandAction } = this.props;
|
|
|
|
|
|
|
|
// Expand the tree
|
|
|
|
if (expandAction === 'doubleClick') {
|
|
|
|
this.onDebounceExpand(event, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onDoubleClick) {
|
|
|
|
onDoubleClick(event, node);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2020-01-15 12:13:20 +08:00
|
|
|
onSelect = (
|
|
|
|
keys: string[],
|
|
|
|
event: {
|
|
|
|
event: 'select';
|
|
|
|
selected: boolean;
|
|
|
|
node: any;
|
|
|
|
selectedNodes: DataNode[];
|
|
|
|
nativeEvent: MouseEvent;
|
|
|
|
},
|
|
|
|
) => {
|
2019-08-12 13:22:36 +08:00
|
|
|
const { onSelect, multiple } = this.props;
|
2018-12-12 05:15:07 +08:00
|
|
|
const { expandedKeys = [] } = this.state;
|
2018-06-04 11:20:17 +08:00
|
|
|
const { node, nativeEvent } = event;
|
|
|
|
const { eventKey = '' } = node.props;
|
|
|
|
|
2019-08-12 13:22:36 +08:00
|
|
|
const treeData = getTreeData(this.props);
|
2018-06-04 11:20:17 +08:00
|
|
|
const newState: DirectoryTreeState = {};
|
|
|
|
|
2019-04-09 14:20:59 +08:00
|
|
|
// We need wrap this event since some value is not same
|
2020-01-15 12:13:20 +08:00
|
|
|
const newEvent: any = {
|
2019-04-09 14:20:59 +08:00
|
|
|
...event,
|
|
|
|
selected: true, // Directory selected always true
|
|
|
|
};
|
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
// Windows / Mac single pick
|
|
|
|
const ctrlPick: boolean = nativeEvent.ctrlKey || nativeEvent.metaKey;
|
|
|
|
const shiftPick: boolean = nativeEvent.shiftKey;
|
|
|
|
|
|
|
|
// Generate new selected keys
|
2018-12-12 04:26:32 +08:00
|
|
|
let newSelectedKeys: string[];
|
2018-06-04 11:20:17 +08:00
|
|
|
if (multiple && ctrlPick) {
|
|
|
|
// Control click
|
|
|
|
newSelectedKeys = keys;
|
|
|
|
this.lastSelectedKey = eventKey;
|
|
|
|
this.cachedSelectedKeys = newSelectedKeys;
|
2019-08-12 13:22:36 +08:00
|
|
|
newEvent.selectedNodes = convertDirectoryKeysToNodes(treeData, newSelectedKeys);
|
2018-06-04 11:20:17 +08:00
|
|
|
} else if (multiple && shiftPick) {
|
|
|
|
// Shift click
|
2018-12-07 20:02:01 +08:00
|
|
|
newSelectedKeys = Array.from(
|
|
|
|
new Set([
|
|
|
|
...(this.cachedSelectedKeys || []),
|
2019-08-12 13:22:36 +08:00
|
|
|
...calcRangeKeys(treeData, expandedKeys, eventKey, this.lastSelectedKey),
|
2018-12-07 20:02:01 +08:00
|
|
|
]),
|
|
|
|
);
|
2019-08-12 13:22:36 +08:00
|
|
|
newEvent.selectedNodes = convertDirectoryKeysToNodes(treeData, newSelectedKeys);
|
2018-06-04 11:20:17 +08:00
|
|
|
} else {
|
|
|
|
// Single click
|
|
|
|
newSelectedKeys = [eventKey];
|
|
|
|
this.lastSelectedKey = eventKey;
|
|
|
|
this.cachedSelectedKeys = newSelectedKeys;
|
2019-08-12 13:22:36 +08:00
|
|
|
newEvent.selectedNodes = [event.node.props.data];
|
2018-06-04 11:20:17 +08:00
|
|
|
}
|
|
|
|
newState.selectedKeys = newSelectedKeys;
|
|
|
|
|
|
|
|
if (onSelect) {
|
2019-04-09 14:20:59 +08:00
|
|
|
onSelect(newSelectedKeys, newEvent);
|
2018-06-04 11:20:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setUncontrolledState(newState);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2018-10-10 11:48:43 +08:00
|
|
|
setTreeRef = (node: Tree) => {
|
|
|
|
this.tree = node;
|
|
|
|
};
|
|
|
|
|
2020-01-15 12:13:20 +08:00
|
|
|
expandFolderNode = (event: React.MouseEvent<HTMLElement>, node: any) => {
|
2020-02-10 10:08:32 +08:00
|
|
|
const { isLeaf } = node;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
if (isLeaf || event.shiftKey || event.metaKey || event.ctrlKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-10 11:48:43 +08:00
|
|
|
// Get internal rc-tree
|
|
|
|
const internalTree = this.tree.tree;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2018-10-10 11:48:43 +08:00
|
|
|
// Call internal rc-tree expand function
|
|
|
|
// https://github.com/ant-design/ant-design/issues/12567
|
|
|
|
internalTree.onNodeExpand(event, node);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
setUncontrolledState = (state: DirectoryTreeState) => {
|
|
|
|
const newState = omit(state, Object.keys(this.props));
|
|
|
|
if (Object.keys(newState).length) {
|
|
|
|
this.setState(newState);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
renderDirectoryTree = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2018-12-05 19:12:18 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className, ...props } = this.props;
|
2018-06-04 11:20:17 +08:00
|
|
|
const { expandedKeys, selectedKeys } = this.state;
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('tree', customizePrefixCls);
|
2020-01-02 19:10:16 +08:00
|
|
|
const connectClassName = classNames(`${prefixCls}-directory`, className, {
|
|
|
|
[`${prefixCls}-directory-rtl`]: direction === 'rtl',
|
|
|
|
});
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Tree
|
|
|
|
icon={getIcon}
|
2018-10-10 11:48:43 +08:00
|
|
|
ref={this.setTreeRef}
|
2019-08-12 13:22:36 +08:00
|
|
|
blockNode
|
2018-06-04 11:20:17 +08:00
|
|
|
{...props}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
className={connectClassName}
|
|
|
|
expandedKeys={expandedKeys}
|
|
|
|
selectedKeys={selectedKeys}
|
|
|
|
onSelect={this.onSelect}
|
|
|
|
onClick={this.onClick}
|
|
|
|
onDoubleClick={this.onDoubleClick}
|
|
|
|
onExpand={this.onExpand}
|
|
|
|
/>
|
|
|
|
);
|
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.renderDirectoryTree}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2018-06-04 11:20:17 +08:00
|
|
|
}
|
2019-02-19 10:02:06 +08:00
|
|
|
|
2019-02-19 10:07:57 +08:00
|
|
|
export default DirectoryTree;
|