1
- import { defineComponent , inject } from 'vue' ;
1
+ import { defineComponent , inject , VNode , PropType } from 'vue' ;
2
2
import classNames from '../_util/classNames' ;
3
3
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined' ;
4
4
import FileOutlined from '@ant-design/icons-vue/FileOutlined' ;
@@ -14,6 +14,46 @@ import { cloneElement } from '../_util/vnode';
14
14
import { defaultConfigProvider } from '../config-provider' ;
15
15
16
16
const TreeNode = VcTree . TreeNode ;
17
+
18
+ export interface TreeDataItem {
19
+ key ?: string | number ;
20
+ title ?: string ;
21
+ isLeaf ?: boolean ;
22
+ selectable ?: boolean ;
23
+ children ?: TreeDataItem [ ] ;
24
+ disableCheckbox ?: boolean ;
25
+ disabled ?: boolean ;
26
+ class ?: string ;
27
+ style ?: any ;
28
+ checkable ?: boolean ;
29
+ icon ?: any ;
30
+ slots ?: any ;
31
+ switcherIcon ?: any ;
32
+ }
33
+
34
+ interface DefaultEvent {
35
+ nativeEvent : MouseEvent ;
36
+ node : any ;
37
+ }
38
+
39
+ export interface CheckEvent extends DefaultEvent {
40
+ checked : boolean ;
41
+ checkedNodes : VNode [ ] ;
42
+ checkedNodesPositions : { node : VNode ; pos : string | number } [ ] ;
43
+ event : string ;
44
+ halfCheckedKeys : ( string | number ) [ ] ;
45
+ }
46
+
47
+ export interface ExpendEvent extends DefaultEvent {
48
+ expanded : boolean ;
49
+ }
50
+
51
+ export interface SelectEvent extends DefaultEvent {
52
+ event : string ;
53
+ selected : boolean ;
54
+ selectedNodes : VNode [ ] ;
55
+ }
56
+
17
57
function TreeProps ( ) {
18
58
return {
19
59
showLine : PropTypes . looseBool ,
@@ -32,30 +72,36 @@ function TreeProps() {
32
72
/** 默认展开对应树节点 */
33
73
defaultExpandParent : PropTypes . looseBool ,
34
74
/** 默认展开指定的树节点 */
35
- defaultExpandedKeys : PropTypes . array ,
75
+ defaultExpandedKeys : PropTypes . arrayOf (
76
+ PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
77
+ ) ,
36
78
/** (受控)展开指定的树节点 */
37
- expandedKeys : PropTypes . array ,
79
+ expandedKeys : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
38
80
/** (受控)选中复选框的树节点 */
39
81
checkedKeys : PropTypes . oneOfType ( [
40
- PropTypes . array ,
82
+ PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
41
83
PropTypes . shape ( {
42
- checked : PropTypes . array ,
43
- halfChecked : PropTypes . array ,
84
+ checked : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
85
+ halfChecked : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
44
86
} ) . loose ,
45
87
] ) ,
46
88
/** 默认选中复选框的树节点 */
47
- defaultCheckedKeys : PropTypes . array ,
89
+ defaultCheckedKeys : PropTypes . arrayOf (
90
+ PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
91
+ ) ,
48
92
/** (受控)设置选中的树节点 */
49
- selectedKeys : PropTypes . array ,
93
+ selectedKeys : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
50
94
/** 默认选中的树节点 */
51
- defaultSelectedKeys : PropTypes . array ,
95
+ defaultSelectedKeys : PropTypes . arrayOf (
96
+ PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
97
+ ) ,
52
98
selectable : PropTypes . looseBool ,
53
99
54
100
/** filter some AntTreeNodes as you need. it should return true */
55
101
filterAntTreeNode : PropTypes . func ,
56
102
/** 异步加载数据 */
57
103
loadData : PropTypes . func ,
58
- loadedKeys : PropTypes . array ,
104
+ loadedKeys : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
59
105
// onLoaded: (loadedKeys: string[], info: { event: 'load', node: AntTreeNode; }) => void,
60
106
/** 响应右键点击 */
61
107
// onRightClick: (options: AntTreeNodeMouseEvent) => void,
@@ -77,7 +123,9 @@ function TreeProps() {
77
123
prefixCls : PropTypes . string ,
78
124
filterTreeNode : PropTypes . func ,
79
125
openAnimation : PropTypes . any ,
80
- treeData : PropTypes . array ,
126
+ treeData : {
127
+ type : Array as PropType < TreeDataItem [ ] > ,
128
+ } ,
81
129
/**
82
130
* @default {title,key,children}
83
131
* 替换treeNode中 title,key,children字段为treeData中对应的字段
@@ -123,7 +171,7 @@ export default defineComponent({
123
171
} ,
124
172
TreeNode,
125
173
methods : {
126
- renderSwitcherIcon ( prefixCls : string , switcherIcon , { isLeaf, loading, expanded } ) {
174
+ renderSwitcherIcon ( prefixCls : string , switcherIcon : VNode , { isLeaf, loading, expanded } ) {
127
175
const { showLine } = this . $props ;
128
176
if ( loading ) {
129
177
return < LoadingOutlined class = { `${ prefixCls } -switcher-loading-icon` } /> ;
@@ -148,26 +196,19 @@ export default defineComponent({
148
196
< CaretDownFilled class = { switcherCls } />
149
197
) ;
150
198
} ,
151
- updateTreeData ( treeData ) {
199
+ updateTreeData ( treeData : TreeDataItem [ ] ) {
152
200
const { $slots } = this ;
153
201
const defaultFields = { children : 'children' , title : 'title' , key : 'key' } ;
154
202
const replaceFields = { ...defaultFields , ...this . $props . replaceFields } ;
155
203
return treeData . map ( item => {
156
204
const key = item [ replaceFields . key ] ;
157
205
const children = item [ replaceFields . children ] ;
158
- const { slots = { } , scopedSlots = { } , class : cls , style, ...restProps } = item ;
206
+ const { slots = { } , class : cls , style, ...restProps } = item ;
159
207
const treeNodeProps = {
160
208
...restProps ,
161
- icon : $slots [ scopedSlots . icon ] || $slots [ slots . icon ] || restProps . icon ,
162
- switcherIcon :
163
- $slots [ scopedSlots . switcherIcon ] ||
164
- $slots [ slots . switcherIcon ] ||
165
- restProps . switcherIcon ,
166
- title :
167
- $slots [ scopedSlots . title ] ||
168
- $slots [ slots . title ] ||
169
- $slots . title ||
170
- restProps [ replaceFields . title ] ,
209
+ icon : $slots [ slots . icon ] || restProps . icon ,
210
+ switcherIcon : $slots [ slots . switcherIcon ] || restProps . switcherIcon ,
211
+ title : $slots [ slots . title ] || $slots . title || restProps [ replaceFields . title ] ,
171
212
dataRef : item ,
172
213
key,
173
214
class : cls ,
@@ -179,18 +220,18 @@ export default defineComponent({
179
220
return treeNodeProps ;
180
221
} ) ;
181
222
} ,
182
- setTreeRef ( node ) {
223
+ setTreeRef ( node : VNode ) {
183
224
this . tree = node ;
184
225
} ,
185
- handleCheck ( checkedObj , eventObj ) {
226
+ handleCheck ( checkedObj : ( number | string ) [ ] , eventObj : CheckEvent ) {
186
227
this . $emit ( 'update:checkedKeys' , checkedObj ) ;
187
228
this . $emit ( 'check' , checkedObj , eventObj ) ;
188
229
} ,
189
- handleExpand ( expandedKeys , eventObj ) {
230
+ handleExpand ( expandedKeys : ( number | string ) [ ] , eventObj : ExpendEvent ) {
190
231
this . $emit ( 'update:expandedKeys' , expandedKeys ) ;
191
232
this . $emit ( 'expand' , expandedKeys , eventObj ) ;
192
233
} ,
193
- handleSelect ( selectedKeys : string [ ] , eventObj ) {
234
+ handleSelect ( selectedKeys : ( number | string ) [ ] , eventObj : SelectEvent ) {
194
235
this . $emit ( 'update:selectedKeys' , selectedKeys ) ;
195
236
this . $emit ( 'select' , selectedKeys , eventObj ) ;
196
237
} ,
0 commit comments