ant-design-vue/components/tree/DirectoryTree.jsx

218 lines
6.3 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import omit from 'omit.js';
import debounce from 'lodash/debounce';
import PropTypes from '../_util/vue-types';
import warning from '../_util/warning';
2019-01-12 03:33:27 +00:00
import { conductExpandParent, convertTreeToEntities } from '../vc-tree/src/util';
import Tree, { TreeProps } from './Tree';
import { calcRangeKeys, getFullKeyList } from './util';
import Icon from '../icon';
import BaseMixin from '../_util/BaseMixin';
import {
initDefaultProps,
getOptionProps,
getListeners,
getComponentFromProp,
} from '../_util/props-util';
2019-04-10 05:28:07 +00:00
import { ConfigConsumerProps } from '../config-provider';
2018-09-26 14:57:01 +00:00
2019-11-20 07:49:31 +00:00
// export type ExpandAction = false | 'click' | 'dblclick'; export interface
2019-09-11 14:35:25 +00:00
// DirectoryTreeProps extends TreeProps { expandAction?: ExpandAction; }
// export interface DirectoryTreeState { expandedKeys?: string[];
// selectedKeys?: string[]; }
2018-09-26 14:57:01 +00:00
2019-01-12 03:33:27 +00:00
function getIcon(props, h) {
const { isLeaf, expanded } = props;
2018-09-26 14:57:01 +00:00
if (isLeaf) {
2019-01-12 03:33:27 +00:00
return <Icon type="file" />;
2018-09-26 14:57:01 +00:00
}
2019-01-12 03:33:27 +00:00
return <Icon type={expanded ? 'folder-open' : 'folder'} />;
2018-09-26 14:57:01 +00:00
}
export default {
name: 'ADirectoryTree',
2019-02-01 09:23:00 +00:00
mixins: [BaseMixin],
2018-09-26 14:57:01 +00:00
model: {
prop: 'checkedKeys',
event: 'check',
},
2019-01-12 03:33:27 +00:00
props: initDefaultProps(
2019-09-11 14:35:25 +00:00
{
...TreeProps(),
2019-11-20 07:49:31 +00:00
expandAction: PropTypes.oneOf([false, 'click', 'doubleclick', 'dblclick']),
2019-09-11 14:35:25 +00:00
},
2019-01-12 03:33:27 +00:00
{
showIcon: true,
expandAction: 'click',
},
),
2018-09-26 14:57:01 +00:00
2019-09-11 14:35:25 +00:00
// state: DirectoryTreeState; onDebounceExpand: (event, node: AntTreeNode) =>
// void; // Shift click usage lastSelectedKey?: string; cachedSelectedKeys?:
// string[];
2019-04-10 05:28:07 +00:00
inject: {
2019-09-11 14:35:25 +00:00
configProvider: {
default: () => ConfigConsumerProps,
},
2019-04-10 05:28:07 +00:00
},
2019-01-12 03:33:27 +00:00
data() {
const props = getOptionProps(this);
const { defaultExpandAll, defaultExpandParent, expandedKeys, defaultExpandedKeys } = props;
const { keyEntities } = convertTreeToEntities(this.$slots.default);
const state = {};
2018-09-26 14:57:01 +00:00
// Selected keys
2019-01-12 03:33:27 +00:00
state._selectedKeys = props.selectedKeys || props.defaultSelectedKeys || [];
2018-09-26 14:57:01 +00:00
// Expanded keys
if (defaultExpandAll) {
2019-01-12 03:33:27 +00:00
state._expandedKeys = getFullKeyList(this.$slots.default);
2018-09-26 14:57:01 +00:00
} else if (defaultExpandParent) {
2019-01-12 03:33:27 +00:00
state._expandedKeys = conductExpandParent(expandedKeys || defaultExpandedKeys, keyEntities);
2018-09-26 14:57:01 +00:00
} else {
2019-01-12 03:33:27 +00:00
state._expandedKeys = expandedKeys || defaultExpandedKeys;
2018-09-26 14:57:01 +00:00
}
2019-09-11 14:35:25 +00:00
this.onDebounceExpand = debounce(this.expandFolderNode, 200, { leading: true });
2018-09-26 14:57:01 +00:00
return {
_selectedKeys: [],
_expandedKeys: [],
...state,
2019-01-12 03:33:27 +00:00
};
2018-09-26 14:57:01 +00:00
},
watch: {
2019-01-12 03:33:27 +00:00
expandedKeys(val) {
this.setState({ _expandedKeys: val });
2018-09-26 14:57:01 +00:00
},
2019-01-12 03:33:27 +00:00
selectedKeys(val) {
this.setState({ _selectedKeys: val });
2018-09-26 14:57:01 +00:00
},
},
methods: {
2019-01-12 03:33:27 +00:00
onExpand(expandedKeys, info) {
this.setUncontrolledState({ _expandedKeys: expandedKeys });
2018-09-26 14:57:01 +00:00
2019-01-12 03:33:27 +00:00
this.$emit('expand', expandedKeys, info);
2018-09-26 14:57:01 +00:00
2019-01-12 03:33:27 +00:00
return undefined;
2018-09-26 14:57:01 +00:00
},
2019-01-12 03:33:27 +00:00
onClick(event, node) {
const { expandAction } = this.$props;
2018-09-26 14:57:01 +00:00
// Expand the tree
if (expandAction === 'click') {
2019-01-12 03:33:27 +00:00
this.onDebounceExpand(event, node);
2018-09-26 14:57:01 +00:00
}
2019-01-12 03:33:27 +00:00
this.$emit('click', event, node);
2018-09-26 14:57:01 +00:00
},
2019-01-12 03:33:27 +00:00
onDoubleClick(event, node) {
const { expandAction } = this.$props;
2018-09-26 14:57:01 +00:00
// Expand the tree
2019-11-20 07:49:31 +00:00
if (expandAction === 'dblclick' || expandAction === 'doubleclick') {
2019-01-12 03:33:27 +00:00
this.onDebounceExpand(event, node);
2018-09-26 14:57:01 +00:00
}
2019-01-12 03:33:27 +00:00
this.$emit('doubleclick', event, node);
2019-11-20 07:49:31 +00:00
this.$emit('dblclick', event, node);
2018-09-26 14:57:01 +00:00
},
2019-01-12 03:33:27 +00:00
onSelect(keys, event) {
const { multiple } = this.$props;
const children = this.$slots.default || [];
const { _expandedKeys: expandedKeys = [] } = this.$data;
const { node, nativeEvent } = event;
const { eventKey = '' } = node;
2018-09-26 14:57:01 +00:00
2019-01-12 03:33:27 +00:00
const newState = {};
2018-09-26 14:57:01 +00:00
// Windows / Mac single pick
2019-01-12 03:33:27 +00:00
const ctrlPick = nativeEvent.ctrlKey || nativeEvent.metaKey;
const shiftPick = nativeEvent.shiftKey;
2018-09-26 14:57:01 +00:00
// Generate new selected keys
2019-01-12 03:33:27 +00:00
let newSelectedKeys;
2018-09-26 14:57:01 +00:00
if (multiple && ctrlPick) {
2019-01-12 03:33:27 +00:00
// Control click
newSelectedKeys = keys;
this.lastSelectedKey = eventKey;
this.cachedSelectedKeys = newSelectedKeys;
2018-09-26 14:57:01 +00:00
} else if (multiple && shiftPick) {
2019-01-12 03:33:27 +00:00
// Shift click
newSelectedKeys = Array.from(
new Set([
...(this.cachedSelectedKeys || []),
...calcRangeKeys(children, expandedKeys, eventKey, this.lastSelectedKey),
]),
);
2018-09-26 14:57:01 +00:00
} else {
2019-01-12 03:33:27 +00:00
// Single click
newSelectedKeys = [eventKey];
this.lastSelectedKey = eventKey;
this.cachedSelectedKeys = newSelectedKeys;
2018-09-26 14:57:01 +00:00
}
2019-01-12 03:33:27 +00:00
newState._selectedKeys = newSelectedKeys;
2018-09-26 14:57:01 +00:00
2019-01-12 03:33:27 +00:00
this.$emit('update:selectedKeys', newSelectedKeys);
this.$emit('select', newSelectedKeys, event);
2018-09-26 14:57:01 +00:00
2019-01-12 03:33:27 +00:00
this.setUncontrolledState(newState);
2018-09-26 14:57:01 +00:00
},
2019-01-12 03:33:27 +00:00
expandFolderNode(event, node) {
const { isLeaf } = node;
2018-09-26 14:57:01 +00:00
if (isLeaf || event.shiftKey || event.metaKey || event.ctrlKey) {
2019-01-12 03:33:27 +00:00
return;
2018-09-26 14:57:01 +00:00
}
2018-12-11 12:33:37 +00:00
if (this.$refs.tree.$refs.tree) {
// Get internal vc-tree
2019-01-12 03:33:27 +00:00
const internalTree = this.$refs.tree.$refs.tree;
2018-09-26 14:57:01 +00:00
2018-12-11 12:33:37 +00:00
// Call internal rc-tree expand function
// https://github.com/ant-design/ant-design/issues/12567
2019-01-12 03:33:27 +00:00
internalTree.onNodeExpand(event, node);
2018-09-26 14:57:01 +00:00
}
},
2019-01-12 03:33:27 +00:00
setUncontrolledState(state) {
2019-12-21 10:37:33 +00:00
const newState = omit(
state,
Object.keys(getOptionProps(this)).map(p => `_${p}`),
);
2018-09-26 14:57:01 +00:00
if (Object.keys(newState).length) {
2019-01-12 03:33:27 +00:00
this.setState(newState);
2018-09-26 14:57:01 +00:00
}
},
},
2019-01-12 03:33:27 +00:00
render() {
2019-04-10 05:28:07 +00:00
const { prefixCls: customizePrefixCls, ...props } = getOptionProps(this);
2019-09-11 14:35:25 +00:00
const getPrefixCls = this.configProvider.getPrefixCls;
2019-04-10 05:28:07 +00:00
const prefixCls = getPrefixCls('tree', customizePrefixCls);
2019-01-12 03:33:27 +00:00
const { _expandedKeys: expandedKeys, _selectedKeys: selectedKeys } = this.$data;
2020-01-18 13:44:13 +00:00
const listeners = getListeners(this);
warning(!listeners.doubleclick, '`doubleclick` is deprecated. please use `dblclick` instead.');
2018-09-26 14:57:01 +00:00
const treeProps = {
props: {
icon: getIcon,
...props,
prefixCls,
expandedKeys,
selectedKeys,
switcherIcon: getComponentFromProp(this, 'switcherIcon'),
2018-09-26 14:57:01 +00:00
},
2018-12-11 12:33:37 +00:00
ref: 'tree',
2018-09-26 14:57:01 +00:00
class: `${prefixCls}-directory`,
2018-09-28 06:35:26 +00:00
on: {
2020-01-18 13:44:13 +00:00
...omit(listeners, ['update:selectedKeys']),
2018-09-28 06:35:26 +00:00
select: this.onSelect,
click: this.onClick,
2019-11-20 07:49:31 +00:00
dblclick: this.onDoubleClick,
2018-09-28 06:35:26 +00:00
expand: this.onExpand,
},
2019-01-12 03:33:27 +00:00
};
return <Tree {...treeProps}>{this.$slots.default}</Tree>;
2018-09-26 14:57:01 +00:00
},
2019-01-12 03:33:27 +00:00
};