Skip to content

Commit 52ba86c

Browse files
committed
Tree: fix type definition
1 parent c46f1ba commit 52ba86c

File tree

1 file changed

+103
-96
lines changed

1 file changed

+103
-96
lines changed

types/tree.d.ts

Lines changed: 103 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,172 @@
1-
import { CreateElement, VNode } from 'vue'
2-
import { ElementUIComponent } from './component'
3-
4-
/** The node of the tree */
5-
export interface TreeNode {
6-
id?: any,
7-
label?: string,
8-
isLeaf?: boolean,
9-
children?: TreeNode[]
1+
import { CreateElement, VNode } from 'vue';
2+
import { ElementUIComponent } from './component';
3+
4+
export interface TreeData {
5+
id?: any;
6+
label?: string;
7+
isLeaf?: boolean;
8+
children?: TreeData[];
109
}
1110

12-
export interface RenderContent {
13-
/**
14-
* Render function for a specific node
15-
*
16-
* @param h The render function
17-
* @param data The data object containing the specific node
18-
*/
19-
(h: CreateElement, data: { node: TreeNode }): VNode
11+
export interface TreeNode<K, D> {
12+
checked: boolean;
13+
childNodes: TreeNode<K, D>[];
14+
data: D;
15+
expanded: boolean;
16+
id: number;
17+
indeterminate: boolean;
18+
isLeaf: boolean;
19+
level: number;
20+
loaded: boolean;
21+
loading: boolean;
22+
parent: TreeNode<K, D> | null;
23+
store: any;
24+
visible: boolean;
25+
disabled: boolean;
26+
icon: string;
27+
key: K;
28+
nextSibling: TreeNode<K, D> | null;
29+
previousSibling: TreeNode<K, D> | null;
2030
}
2131

22-
export interface FilterNodeMethod {
23-
/**
24-
* Filter method for each node
25-
*
26-
* @param value The query string
27-
* @param data The original data object
28-
* @param node Tree node
29-
*/
30-
(value: string, data: TreeNode, node: any): boolean
31-
}
32-
33-
export interface AllowDragMethod {
34-
/**
35-
* Function executed before dragging a node
36-
*
37-
* @param node The node to be dragged
38-
*/
39-
(node: any): boolean
40-
}
41-
42-
export interface AllowDropMethod {
43-
/**
44-
* Function executed before the dragging node is dropped
45-
*
46-
* @param draggingNode The dragging node
47-
* @param dropNode The target node
48-
* @param type Drop type
49-
*/
50-
(draggingNode: any, dropNode: any, type: string): boolean
32+
/** incomplete, you can convert to any to use other properties */
33+
export interface TreeStore<K, D> {
34+
_getAllNodes: () => TreeNode<K, D>[];
5135
}
5236

5337
/** Tree Component */
54-
export declare class ElTree extends ElementUIComponent {
38+
export declare class ElTree<K = any, D = TreeData> extends ElementUIComponent {
39+
/** TreeStore */
40+
store: TreeStore<K, D>;
41+
5542
/** Tree data */
56-
data: TreeNode[]
43+
data: D[];
5744

5845
/** Text displayed when data is void */
59-
emptyText: string
46+
emptyText: string;
6047

6148
/** Unique identity key name for nodes, its value should be unique across the whole tree */
62-
nodeKey: string
49+
nodeKey: string;
6350

6451
/** Configuration options, see the following table */
65-
props: object
52+
props: object;
6653

6754
/** Method for loading subtree data */
68-
load: (node: TreeNode, resolve: Function) => void
55+
load: (data: D, resolve: Function) => void;
6956

70-
/** Render function for tree node */
71-
renderContent: RenderContent
57+
/**
58+
* Render function for a specific node
59+
*
60+
* @param h The render function
61+
*/
62+
renderContent: (h: CreateElement, context: { node: TreeNode<K, D>; data: D; store: TreeStore<K, D> }) => VNode;
7263

7364
/** Whether current node is highlighted */
74-
highlightCurrent: boolean
65+
highlightCurrent: boolean;
7566

7667
/** Whether to expand all nodes by default */
77-
defaultExpandAll: boolean
68+
defaultExpandAll: boolean;
7869

7970
/** Whether to expand or collapse node when clicking on the node. If false, then expand or collapse node only when clicking on the arrow icon. */
80-
expandOnClickNode: boolean
71+
expandOnClickNode: boolean;
8172

8273
/** Whether to check or uncheck node when clicking on the node, if false, the node can only be checked or unchecked by clicking on the checkbox. */
83-
checkOnClickNode: boolean
74+
checkOnClickNode: boolean;
8475

8576
/** Whether to expand father node when a child node is expanded */
86-
autoExpandParent: boolean
77+
autoExpandParent: boolean;
8778

8879
/** Array of keys of initially expanded nodes */
89-
defaultExpandedKeys: any[]
80+
defaultExpandedKeys: K[];
9081

9182
/** Whether node is selectable */
92-
showCheckbox: boolean
83+
showCheckbox: boolean;
9384

9485
/** Whether checked state of a node not affects its father and child nodes when show-checkbox is true */
95-
checkStrictly: boolean
86+
checkStrictly: boolean;
9687

9788
/** Array of keys of initially checked nodes */
98-
defaultCheckedKeys: any[]
89+
defaultCheckedKeys: K[];
9990

100-
/** This function will be executed on each node when use filter method. If return false, tree node will be hidden. */
101-
filterNodeMethod: FilterNodeMethod
91+
/**
92+
* This function will be executed on each node when use filter method. If return false, tree node will be hidden.
93+
*
94+
* @param value The query string
95+
* @param data The original data object
96+
* @param node Tree node
97+
*/
98+
filterNodeMethod: (value: string, data: D, node: TreeNode<K, D>) => boolean;
10299

103100
/** Whether only one node among the same level can be expanded at one time */
104-
accordion: boolean
101+
accordion: boolean;
105102

106103
/** Horizontal indentation of nodes in adjacent levels in pixels */
107-
indent: number
104+
indent: number;
108105

109106
/** Whether enable tree nodes drag and drop */
110-
draggable: boolean
107+
draggable: boolean;
111108

112-
/** Function to be executed before dragging a node */
113-
allowDrag: AllowDragMethod
109+
/**
110+
* Function to be executed before dragging a node
111+
*
112+
* @param node The node to be dragged
113+
*/
114+
allowDrag: (node: TreeNode<K, D>) => boolean;
114115

115-
/** Function to be executed before the dragging node is dropped */
116-
allowDrop: AllowDropMethod
116+
/**
117+
* Function to be executed before the dragging node is dropped
118+
*
119+
* @param draggingNode The dragging node
120+
* @param dropNode The target node
121+
* @param type Drop type
122+
*/
123+
allowDrop: (draggingNode: TreeNode<K, D>, dropNode: TreeNode<K, D>, type: 'prev' | 'inner' | 'next') => boolean;
117124

118125
/**
119126
* Filter all tree nodes. Filtered nodes will be hidden
120127
*
121128
* @param value The value to be used as first parameter for `filter-node-method`
122129
*/
123-
filter (value: any): void
130+
filter(value: any): void;
124131

125132
/**
126133
* Update the children of the node which specified by the key
127-
*
134+
*
128135
* @param key the key of the node which children will be updated
129136
* @param data the children data
130137
*/
131-
updateKeyChildren (key: any, data: TreeNode[]): void
138+
updateKeyChildren(key: K, data: D[]): void;
132139

133140
/**
134141
* If the node can be selected (`show-checkbox` is `true`), it returns the currently selected array of nodes
135142
*
136143
* @param leafOnly If the `leafOnly` is `true`, it only returns the currently selected array of sub-nodes
137144
* @param includeHalfChecked If the `includeHalfChecked` is `true`, the return value contains halfchecked nodes
138145
*/
139-
getCheckedNodes (leafOnly?: boolean, includeHalfChecked?: boolean): TreeNode[]
146+
getCheckedNodes(leafOnly?: boolean, includeHalfChecked?: boolean): D[];
140147

141148
/**
142149
* Set certain nodes to be checked. Only works when `node-key` is assigned
143150
*
144151
* @param nodes An array of nodes to be checked
145152
* @param leafOnly If the parameter is true, it only returns the currently selected array of sub-nodes
146153
*/
147-
setCheckedNodes (nodes: TreeNode[], leafOnly?: boolean): void
154+
setCheckedNodes(data: D[], leafOnly?: boolean): void;
148155

149156
/**
150157
* If the node can be selected (`show-checkbox` is `true`), it returns the currently selected array of nodes' keys
151158
*
152159
* @param leafOnly If the `leafOnly` is `true`, it only returns the currently selected array of sub-nodes
153160
*/
154-
getCheckedKeys (leafOnly?: boolean): any[]
161+
getCheckedKeys(leafOnly?: boolean): K[];
155162

156163
/**
157164
* Set certain nodes to be checked. Only works when `node-key` is assigned
158165
*
159166
* @param keys An array of node's keys to be checked
160167
* @param leafOnly If the parameter is true, it only returns the currently selected array of sub-nodes
161168
*/
162-
setCheckedKeys (keys: any[], leafOnly?: boolean): void
169+
setCheckedKeys(keys: K[], leafOnly?: boolean): void;
163170

164171
/**
165172
* Set node to be checked or not. Only works when `node-key` is assigned
@@ -168,77 +175,77 @@ export declare class ElTree extends ElementUIComponent {
168175
* @param checked Indicating the node checked or not
169176
* @param deep Indicating whether to checked state deeply or not
170177
*/
171-
setChecked (data: TreeNode | any, checked: boolean, deep: boolean): void
178+
setChecked(data: D | K, checked: boolean, deep: boolean): void;
172179

173180
/**
174181
* If the node can be selected (`show-checkbox` is `true`), it returns the currently half selected array of nodes
175182
*/
176-
getHalfCheckedNodes (): void
183+
getHalfCheckedNodes(): D[];
177184

178185
/**
179186
* If the node can be selected (`show-checkbox` is `true`), it returns the currently half selected array of nodes' keys
180187
*/
181-
getHalfCheckedKeys (): void;
188+
getHalfCheckedKeys(): K[];
182189

183190
/**
184191
* Return the highlight node's key (null if no node is highlighted)
185192
*/
186-
getCurrentKey (): any
193+
getCurrentKey(): K;
187194

188195
/**
189196
* Set highlighted node by key, only works when node-key is assigned
190197
*
191198
* @param key The node's key to be highlighted
192199
*/
193-
setCurrentKey (key: any): void
200+
setCurrentKey(key: K): void;
194201

195202
/**
196203
* Return the highlight node (null if no node is highlighted)
197204
*/
198-
getCurrentNode (): TreeNode
205+
getCurrentNode(): D;
199206

200207
/**
201208
* Set highlighted node, only works when node-key is assigned
202209
*
203210
* @param node The node to be highlighted
204211
*/
205-
setCurrentNode (node: TreeNode): void
212+
setCurrentNode(data: D): void;
206213

207214
/**
208215
* Get node by node key or node data
209-
*
216+
*
210217
* @param by node key or node data
211218
*/
212-
getNode (by: TreeNode | any): TreeNode
219+
getNode(by: D | K): D;
213220

214221
/**
215222
* Remove node by key or node data or node instance
216-
*
223+
*
217224
* @param by key or node data or node instance
218225
*/
219-
remove (by: TreeNode | any): void
226+
remove(by: D | K): void;
220227

221228
/**
222229
* Append a child node to specified node
223-
*
230+
*
224231
* @param childData the data of appended node
225232
* @param parent key or node data or node instance of the parent node
226233
*/
227-
append (childData: TreeNode, parent: TreeNode | any): void
234+
append(childData: D, parent: D | K): void;
228235

229236
/**
230237
* insert a node before specified node
231-
*
238+
*
232239
* @param data the data of inserted node
233240
* @param ref key or node data or node instance of the reference node
234241
*/
235-
insertBefore (data: TreeNode, ref: TreeNode | any): void
242+
insertBefore(data: D, ref: D | K): void;
236243

237244
/**
238245
* insert a node after specified node
239-
*
246+
*
240247
* @param data the data of inserted node
241248
* @param ref key or node data or node instance of the reference node
242249
*/
243-
insertAfter (data: TreeNode, ref: TreeNode | any): void
250+
insertAfter(data: D, ref: D | K): void;
244251
}

0 commit comments

Comments
 (0)