Skip to content

Commit 895b433

Browse files
authored
refactor:tree (#6276)
1 parent 9f53d53 commit 895b433

File tree

6 files changed

+528
-39
lines changed

6 files changed

+528
-39
lines changed

components/style.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import './cascader/style';
4242
// import './timeline/style';
4343
// import './input-number/style';
4444
// import './transfer/style';
45-
import './tree/style';
45+
// import './tree/style';
4646
// import './upload/style';
4747
// import './layout/style';
4848
// import './anchor/style';

components/tree/DirectoryTree.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ExtractPropTypes, PropType } from 'vue';
1+
import type { ExtractPropTypes } from 'vue';
22
import { nextTick, onUpdated, ref, watch, defineComponent, computed } from 'vue';
33
import debounce from 'lodash-es/debounce';
44
import FolderOpenOutlined from '@ant-design/icons-vue/FolderOpenOutlined';
@@ -18,12 +18,13 @@ import { conductExpandParent } from '../vc-tree/util';
1818
import { calcRangeKeys, convertDirectoryKeysToNodes } from './utils/dictUtil';
1919
import useConfigInject from '../config-provider/hooks/useConfigInject';
2020
import { filterEmpty } from '../_util/props-util';
21+
import { someType } from '../_util/type';
2122

2223
export type ExpandAction = false | 'click' | 'doubleclick' | 'dblclick';
2324

2425
export const directoryTreeProps = () => ({
2526
...treeProps(),
26-
expandAction: { type: [Boolean, String] as PropType<ExpandAction> },
27+
expandAction: someType<ExpandAction>([Boolean, String]),
2728
});
2829

2930
export type DirectoryTreeProps = Partial<ExtractPropTypes<ReturnType<typeof directoryTreeProps>>>;

components/tree/Tree.tsx

+36-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PropType, ExtractPropTypes } from 'vue';
1+
import type { ExtractPropTypes } from 'vue';
22
import { watchEffect, ref, defineComponent, computed } from 'vue';
33
import classNames from '../_util/classNames';
44
import VcTree from '../vc-tree';
@@ -15,6 +15,10 @@ import dropIndicatorRender from './utils/dropIndicator';
1515
import devWarning from '../vc-util/devWarning';
1616
import { warning } from '../vc-util/warning';
1717
import omit from '../_util/omit';
18+
import { booleanType, someType, arrayType, functionType, objectType } from '../_util/type';
19+
20+
// CSSINJS
21+
import useStyle from './style';
1822

1923
export interface AntdTreeNodeAttribute {
2024
eventKey: string;
@@ -84,58 +88,53 @@ export const treeProps = () => {
8488
const baseTreeProps = vcTreeProps();
8589
return {
8690
...baseTreeProps,
87-
showLine: {
88-
type: [Boolean, Object] as PropType<boolean | { showLeafIcon: boolean }>,
89-
default: undefined,
90-
},
91+
showLine: someType<boolean | { showLeafIcon: boolean }>([Boolean, Object]),
9192
/** 是否支持多选 */
92-
multiple: { type: Boolean, default: undefined },
93+
multiple: booleanType(),
9394
/** 是否自动展开父节点 */
94-
autoExpandParent: { type: Boolean, default: undefined },
95+
autoExpandParent: booleanType(),
9596
/** checkable状态下节点选择完全受控(父子节点选中状态不再关联)*/
96-
checkStrictly: { type: Boolean, default: undefined },
97+
checkStrictly: booleanType(),
9798
/** 是否支持选中 */
98-
checkable: { type: Boolean, default: undefined },
99+
checkable: booleanType(),
99100
/** 是否禁用树 */
100-
disabled: { type: Boolean, default: undefined },
101+
disabled: booleanType(),
101102
/** 默认展开所有树节点 */
102-
defaultExpandAll: { type: Boolean, default: undefined },
103+
defaultExpandAll: booleanType(),
103104
/** 默认展开对应树节点 */
104-
defaultExpandParent: { type: Boolean, default: undefined },
105+
defaultExpandParent: booleanType(),
105106
/** 默认展开指定的树节点 */
106-
defaultExpandedKeys: { type: Array as PropType<Key[]> },
107+
defaultExpandedKeys: arrayType<Key[]>(),
107108
/** (受控)展开指定的树节点 */
108-
expandedKeys: { type: Array as PropType<Key[]> },
109+
expandedKeys: arrayType<Key[]>(),
109110
/** (受控)选中复选框的树节点 */
110-
checkedKeys: {
111-
type: [Array, Object] as PropType<Key[] | { checked: Key[]; halfChecked: Key[] }>,
112-
},
111+
checkedKeys: someType<Key[] | { checked: Key[]; halfChecked: Key[] }>([Array, Object]),
113112
/** 默认选中复选框的树节点 */
114-
defaultCheckedKeys: { type: Array as PropType<Key[]> },
113+
defaultCheckedKeys: arrayType<Key[]>(),
115114
/** (受控)设置选中的树节点 */
116-
selectedKeys: { type: Array as PropType<Key[]> },
115+
selectedKeys: arrayType<Key[]>(),
117116
/** 默认选中的树节点 */
118-
defaultSelectedKeys: { type: Array as PropType<Key[]> },
119-
selectable: { type: Boolean, default: undefined },
117+
defaultSelectedKeys: arrayType<Key[]>(),
118+
selectable: booleanType(),
120119

121-
loadedKeys: { type: Array as PropType<Key[]> },
122-
draggable: { type: Boolean, default: undefined },
123-
showIcon: { type: Boolean, default: undefined },
124-
icon: { type: Function as PropType<(nodeProps: AntdTreeNodeAttribute) => any> },
120+
loadedKeys: arrayType<Key[]>(),
121+
draggable: booleanType(),
122+
showIcon: booleanType(),
123+
icon: functionType<(nodeProps: AntdTreeNodeAttribute) => any>(),
125124
switcherIcon: PropTypes.any,
126125
prefixCls: String,
127126
/**
128127
* @default{title,key,children}
129128
* deprecated, please use `fieldNames` instead
130129
* 替换treeNode中 title,key,children字段为treeData中对应的字段
131130
*/
132-
replaceFields: { type: Object as PropType<FieldNames> },
133-
blockNode: { type: Boolean, default: undefined },
131+
replaceFields: objectType<FieldNames>(),
132+
blockNode: booleanType(),
134133
openAnimation: PropTypes.any,
135134
onDoubleclick: baseTreeProps.onDblclick,
136-
'onUpdate:selectedKeys': Function as PropType<(keys: Key[]) => void>,
137-
'onUpdate:checkedKeys': Function as PropType<(keys: Key[]) => void>,
138-
'onUpdate:expandedKeys': Function as PropType<(keys: Key[]) => void>,
135+
'onUpdate:selectedKeys': functionType<(keys: Key[]) => void>(),
136+
'onUpdate:checkedKeys': functionType<(keys: Key[]) => void>(),
137+
'onUpdate:expandedKeys': functionType<(keys: Key[]) => void>(),
139138
};
140139
};
141140

@@ -168,6 +167,10 @@ export default defineComponent({
168167
'`children` of Tree is deprecated. Please use `treeData` instead.',
169168
);
170169
const { prefixCls, direction, virtual } = useConfigInject('tree', props);
170+
171+
// style
172+
const [wrapSSR, hashId] = useStyle(prefixCls);
173+
171174
const treeRef = ref();
172175
const scrollTo: ScrollTo = scroll => {
173176
treeRef.value?.scrollTo(scroll);
@@ -236,7 +239,7 @@ export default defineComponent({
236239
itemHeight,
237240
};
238241
const children = slots.default ? filterEmpty(slots.default()) : undefined;
239-
return (
242+
return wrapSSR(
240243
<VcTree
241244
{...newProps}
242245
virtual={virtual.value}
@@ -251,6 +254,7 @@ export default defineComponent({
251254
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
252255
},
253256
attrs.class,
257+
hashId.value,
254258
)}
255259
direction={direction.value}
256260
checkable={checkable}
@@ -267,7 +271,7 @@ export default defineComponent({
267271
checkable: () => <span class={`${prefixCls.value}-checkbox-inner`} />,
268272
}}
269273
children={children}
270-
></VcTree>
274+
></VcTree>,
271275
);
272276
};
273277
},

components/tree/index.en-US.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
category: Components
33
type: Data Display
44
title: Tree
5-
cover: https://gw.alipayobjects.com/zos/alicdn/Xh-oWqg9k/Tree.svg
5+
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*Ag9_Q6ArswEAAAAAAAAAAAAADrJ8AQ/original
66
---
77

88
A hierarchical list structure component.
@@ -17,6 +17,7 @@ Almost anything can be represented in a tree structure. Examples include directo
1717

1818
| Property | Description | Type | Default | Version |
1919
| --- | --- | --- | --- | --- |
20+
| allowDrop | Whether to allow dropping on the node | ({ dropNode, dropPosition }) => boolean | - | |
2021
| autoExpandParent | Whether to automatically expand a parent treeNode | boolean | false | |
2122
| blockNode | Whether treeNode fill remaining horizontal space | boolean | false | |
2223
| checkable | Adds a `Checkbox` before the treeNodes | boolean | false | |
@@ -28,6 +29,7 @@ Almost anything can be represented in a tree structure. Examples include directo
2829
| expandedKeys(v-model) | (Controlled) Specifies the keys of the expanded treeNodes | string\[] \| number\[] | \[] | |
2930
| fieldNames | Replace the title,key and children fields in treeNode with the corresponding fields in treeData | object | { children:'children', title:'title', key:'key' } | 3.0.0 |
3031
| filterTreeNode | Defines a function to filter (highlight) treeNodes. When the function returns `true`, the corresponding treeNode will be highlighted | function(node) | - | |
32+
| height | Config virtual scroll height. Will not support horizontal scroll when enable this | number | - | |
3133
| loadData | Load data asynchronously | function(node) | - | |
3234
| loadedKeys | (Controlled) Set loaded tree nodes. Need work with `loadData` | string\[] \| number\[] | \[] | |
3335
| multiple | Allows selecting multiple treeNodes | boolean | false | |

components/tree/index.zh-CN.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ category: Components
33
type: 数据展示
44
title: Tree
55
subtitle: 树形控件
6-
cover: https://gw.alipayobjects.com/zos/alicdn/Xh-oWqg9k/Tree.svg
6+
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*Ag9_Q6ArswEAAAAAAAAAAAAADrJ8AQ/original
77
---
88

99
多层次的结构列表。
@@ -18,6 +18,7 @@ cover: https://gw.alipayobjects.com/zos/alicdn/Xh-oWqg9k/Tree.svg
1818

1919
| 参数 | 说明 | 类型 | 默认值 | 版本 | |
2020
| --- | --- | --- | --- | --- | --- |
21+
| allowDrop | 是否允许拖拽时放置在该节点 | ({ dropNode, dropPosition }) => boolean | - | |
2122
| autoExpandParent | 是否自动展开父节点 | boolean | false | | |
2223
| blockNode | 是否节点占据一行 | boolean | false | | |
2324
| checkable | 节点前添加 Checkbox 复选框 | boolean | false | | |
@@ -29,6 +30,7 @@ cover: https://gw.alipayobjects.com/zos/alicdn/Xh-oWqg9k/Tree.svg
2930
| expandedKeys(v-model) | (受控)展开指定的树节点 | string\[] \| number\[] | \[] | | |
3031
| fieldNames | 替换 treeNode 中 title,key,children 字段为 treeData 中对应的字段 | object | {children:'children', title:'title', key:'key' } | 3.0.0 | |
3132
| filterTreeNode | 按需筛选树节点(高亮),返回 true | function(node) | - | | |
33+
| height | 设置虚拟滚动容器高度,设置后内部节点不再支持横向滚动 | number | - | |
3234
| loadData | 异步加载数据 | function(node) | - | | |
3335
| loadedKeys | (受控)已经加载的节点,需要配合 `loadData` 使用 | string\[] \| number\[] | \[] | | |
3436
| multiple | 支持点选多个节点(节点本身) | boolean | false | | |

0 commit comments

Comments
 (0)