refactor: tree to ts

pull/3053/head
Amour1688 2020-10-22 14:25:02 +08:00
parent 4dbed130e2
commit 0d5d2234c6
5 changed files with 37 additions and 27 deletions

View File

@ -1,4 +1,4 @@
import { inject } from 'vue'; import { defineComponent, inject } from 'vue';
import omit from 'omit.js'; import omit from 'omit.js';
import debounce from 'lodash-es/debounce'; import debounce from 'lodash-es/debounce';
import FolderOpenOutlined from '@ant-design/icons-vue/FolderOpenOutlined'; import FolderOpenOutlined from '@ant-design/icons-vue/FolderOpenOutlined';
@ -15,7 +15,8 @@ import {
getFullKeyListByTreeData, getFullKeyListByTreeData,
} from './util'; } from './util';
import BaseMixin from '../_util/BaseMixin'; import BaseMixin from '../_util/BaseMixin';
import { initDefaultProps, getOptionProps, getComponent, getSlot } from '../_util/props-util'; import { getOptionProps, getComponent, getSlot } from '../_util/props-util';
import initDefaultProps from '../_util/props-util/initDefaultProps';
import { defaultConfigProvider } from '../config-provider'; import { defaultConfigProvider } from '../config-provider';
// export type ExpandAction = false | 'click' | 'dblclick'; export interface // export type ExpandAction = false | 'click' | 'dblclick'; export interface
@ -23,6 +24,11 @@ import { defaultConfigProvider } from '../config-provider';
// export interface DirectoryTreeState { expandedKeys?: string[]; // export interface DirectoryTreeState { expandedKeys?: string[];
// selectedKeys?: string[]; } // selectedKeys?: string[]; }
export interface DirectoryTreeState {
_expandedKeys?: string[];
_selectedKeys?: string[];
}
function getIcon(props) { function getIcon(props) {
const { isLeaf, expanded } = props; const { isLeaf, expanded } = props;
if (isLeaf) { if (isLeaf) {
@ -31,14 +37,10 @@ function getIcon(props) {
return expanded ? <FolderOpenOutlined /> : <FolderOutlined />; return expanded ? <FolderOpenOutlined /> : <FolderOutlined />;
} }
export default { export default defineComponent({
name: 'ADirectoryTree', name: 'ADirectoryTree',
mixins: [BaseMixin], mixins: [BaseMixin],
inheritAttrs: false, inheritAttrs: false,
// model: {
// prop: 'checkedKeys',
// event: 'check',
// },
props: initDefaultProps( props: initDefaultProps(
{ {
...TreeProps(), ...TreeProps(),
@ -51,6 +53,11 @@ export default {
), ),
setup() { setup() {
return { return {
children: null,
onDebounceExpand: null,
tree: null,
lastSelectedKey: [],
cachedSelectedKeys: [],
configProvider: inject('configProvider', defaultConfigProvider), configProvider: inject('configProvider', defaultConfigProvider),
}; };
}, },
@ -59,7 +66,7 @@ export default {
const { defaultExpandAll, defaultExpandParent, expandedKeys, defaultExpandedKeys } = props; const { defaultExpandAll, defaultExpandParent, expandedKeys, defaultExpandedKeys } = props;
const children = getSlot(this); const children = getSlot(this);
const { keyEntities } = convertTreeToEntities(children); const { keyEntities } = convertTreeToEntities(children);
const state = {}; const state: DirectoryTreeState = {};
// Selected keys // Selected keys
state._selectedKeys = props.selectedKeys || props.defaultSelectedKeys || []; state._selectedKeys = props.selectedKeys || props.defaultSelectedKeys || [];
@ -75,15 +82,15 @@ export default {
} else { } else {
state._expandedKeys = expandedKeys || defaultExpandedKeys; state._expandedKeys = expandedKeys || defaultExpandedKeys;
} }
this.onDebounceExpand = debounce(this.expandFolderNode, 200, { leading: true });
this.children = null;
return { return {
_selectedKeys: [], _selectedKeys: [],
_expandedKeys: [], _expandedKeys: [],
...state, ...state,
}; };
}, },
created() {
this.onDebounceExpand = debounce(this.expandFolderNode, 200, { leading: true });
},
watch: { watch: {
expandedKeys(val) { expandedKeys(val) {
this.setState({ _expandedKeys: val }); this.setState({ _expandedKeys: val });
@ -130,7 +137,7 @@ export default {
const { node, nativeEvent } = event; const { node, nativeEvent } = event;
const { eventKey = '' } = node; const { eventKey = '' } = node;
const newState = {}; const newState: DirectoryTreeState = {};
// We need wrap this event since some value is not same // We need wrap this event since some value is not same
const newEvent = { const newEvent = {
@ -235,4 +242,4 @@ export default {
}; };
return <Tree {...treeProps}>{this.children}</Tree>; return <Tree {...treeProps}>{this.children}</Tree>;
}, },
}; });

View File

@ -1,4 +1,4 @@
import { inject } from 'vue'; import { defineComponent, inject } from 'vue';
import classNames from '../_util/classNames'; import classNames from '../_util/classNames';
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined'; import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
import FileOutlined from '@ant-design/icons-vue/FileOutlined'; import FileOutlined from '@ant-design/icons-vue/FileOutlined';
@ -8,7 +8,8 @@ import PlusSquareOutlined from '@ant-design/icons-vue/PlusSquareOutlined';
import VcTree from '../vc-tree'; import VcTree from '../vc-tree';
import animation from '../_util/openAnimation'; import animation from '../_util/openAnimation';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import { initDefaultProps, getOptionProps, getComponent, getSlot } from '../_util/props-util'; import { getOptionProps, getComponent, getSlot } from '../_util/props-util';
import initDefaultProps from '../_util/props-util/initDefaultProps';
import { cloneElement } from '../_util/vnode'; import { cloneElement } from '../_util/vnode';
import { defaultConfigProvider } from '../config-provider'; import { defaultConfigProvider } from '../config-provider';
@ -102,7 +103,7 @@ function TreeProps() {
export { TreeProps }; export { TreeProps };
export default { export default defineComponent({
name: 'ATree', name: 'ATree',
inheritAttrs: false, inheritAttrs: false,
props: initDefaultProps(TreeProps(), { props: initDefaultProps(TreeProps(), {
@ -116,12 +117,13 @@ export default {
}), }),
setup() { setup() {
return { return {
tree: null,
configProvider: inject('configProvider', defaultConfigProvider), configProvider: inject('configProvider', defaultConfigProvider),
}; };
}, },
TreeNode, TreeNode,
methods: { methods: {
renderSwitcherIcon(prefixCls, switcherIcon, { isLeaf, loading, expanded }) { renderSwitcherIcon(prefixCls: string, switcherIcon, { isLeaf, loading, expanded }) {
const { showLine } = this.$props; const { showLine } = this.$props;
if (loading) { if (loading) {
return <LoadingOutlined class={`${prefixCls}-switcher-loading-icon`} />; return <LoadingOutlined class={`${prefixCls}-switcher-loading-icon`} />;
@ -184,7 +186,7 @@ export default {
this.$emit('update:expandedKeys', expandedKeys); this.$emit('update:expandedKeys', expandedKeys);
this.$emit('expand', expandedKeys, eventObj); this.$emit('expand', expandedKeys, eventObj);
}, },
handleSelect(selectedKeys, eventObj) { handleSelect(selectedKeys: string[], eventObj) {
this.$emit('update:selectedKeys', selectedKeys); this.$emit('update:selectedKeys', selectedKeys);
this.$emit('select', selectedKeys, eventObj); this.$emit('select', selectedKeys, eventObj);
}, },
@ -216,10 +218,10 @@ export default {
onCheck: this.handleCheck, onCheck: this.handleCheck,
onExpand: this.handleExpand, onExpand: this.handleExpand,
onSelect: this.handleSelect, onSelect: this.handleSelect,
}; } as any;
if (treeData) { if (treeData) {
vcTreeProps.treeData = treeData; vcTreeProps.treeData = treeData;
} }
return <VcTree {...vcTreeProps} __propsSymbol__={[]} />; return <VcTree {...vcTreeProps} __propsSymbol__={[]} />;
}, },
}; });

View File

@ -1,10 +1,11 @@
import { App } from 'vue';
import Tree from './Tree'; import Tree from './Tree';
import DirectoryTree from './DirectoryTree'; import DirectoryTree from './DirectoryTree';
Tree.TreeNode.name = 'ATreeNode'; Tree.TreeNode.name = 'ATreeNode';
Tree.DirectoryTree = DirectoryTree; Tree.DirectoryTree = DirectoryTree;
/* istanbul ignore next */ /* istanbul ignore next */
Tree.install = function(app) { Tree.install = function(app: App) {
app.component(Tree.name, Tree); app.component(Tree.name, Tree);
app.component(Tree.TreeNode.name, Tree.TreeNode); app.component(Tree.TreeNode.name, Tree.TreeNode);
app.component(DirectoryTree.name, DirectoryTree); app.component(DirectoryTree.name, DirectoryTree);

View File

@ -1,11 +1,11 @@
import { getNodeChildren, convertTreeToEntities } from '../vc-tree/src/util'; import { getNodeChildren, convertTreeToEntities } from '../vc-tree/src/util';
import { getSlot } from '../_util/props-util'; import { getSlot } from '../_util/props-util';
const Record = { enum Record {
None: 'node', None,
Start: 'start', Start,
End: 'end', End,
}; }
// TODO: Move this logic into `rc-tree` // TODO: Move this logic into `rc-tree`
function traverseNodesKey(rootChildren, callback) { function traverseNodesKey(rootChildren, callback) {
@ -88,7 +88,7 @@ export function convertDirectoryKeysToNodes(rootChildren, keys) {
return nodes; return nodes;
} }
export function getFullKeyListByTreeData(treeData, replaceFields = {}) { export function getFullKeyListByTreeData(treeData: any, replaceFields: any = {}) {
let keys = []; let keys = [];
const { key = 'key', children = 'children' } = replaceFields(treeData || []).forEach(item => { const { key = 'key', children = 'children' } = replaceFields(treeData || []).forEach(item => {
keys.push(item[key]); keys.push(item[key]);