35
35
ref =" treeRef"
36
36
v-bind =" $attrs"
37
37
v-model =" treeModelValue"
38
- @set-data =" setData "
38
+ @set-data =" onSetData "
39
39
@checked-change =" checkedChange"
40
40
>
41
41
<template v-for =" (_ , slot ) in $slots " :name =" slot " v-slot =" scope " >
@@ -64,33 +64,14 @@ import {
64
64
PropType
65
65
} from ' vue-demi'
66
66
import CTree from ' ./Tree.vue'
67
- import { API_METHODS , ApiType } from ' ../const'
67
+ import { ApiType } from ' ../const'
68
68
import type { TreeNodeKeyType } from ' ../types'
69
+ import { getCtreeMethods } from ' ../utils'
69
70
70
71
const prefixCls = ' ctree-tree-search'
71
72
72
73
const treeNodePrefixCls = ' ctree-tree-node'
73
74
74
- type CTreeInstanceType = InstanceType <typeof CTree >
75
- type CTreeApiMethodsType = { [key in ApiType ]: CTreeInstanceType [key ] }
76
- type CtreeInstanceKeyType = keyof CTreeInstanceType
77
-
78
- // Vue 2.6 内部没改变的话可以这样获取 Vue.extend 中的 methods。Vue 版本有升级的话需要注意这个特性有没有改变
79
- // 如果是对象的话可以直接 CTree.methods ,并且是安全的。
80
- // let ctreeMethods: CTreeApiMethodsType = {} as CTreeApiMethodsType
81
-
82
- function getCtreeMethods(treeRef : Ref <CTreeInstanceType >) {
83
- return API_METHODS .reduce ((prev , cur ) => {
84
- prev [cur ] = function (... args : any []) {
85
- return treeRef .value [cur as CtreeInstanceKeyType ].apply (
86
- treeRef .value ,
87
- args
88
- )
89
- }
90
- return prev
91
- }, {} as Record <string , Function >)
92
- }
93
-
94
75
export default defineComponent ({
95
76
name: ' CTreeSearch' ,
96
77
inheritAttrs: false ,
@@ -278,13 +259,13 @@ export default defineComponent({
278
259
} else {
279
260
if (props .searchRemote ) {
280
261
// 远程搜索
281
- treeRef .value .methods . loadRootNodes ().then (() => {
262
+ treeRef .value .loadRootNodes ().then (() => {
282
263
updateCheckAllStatus ()
283
264
resolve ()
284
265
})
285
266
} else {
286
267
// 本地搜索
287
- treeRef .value .methods . filter (searchKeyword )
268
+ treeRef .value .filter (searchKeyword )
288
269
updateCheckAllStatus ()
289
270
resolve ()
290
271
}
@@ -299,15 +280,15 @@ export default defineComponent({
299
280
function handleCheckAll(): void {
300
281
if (props .searchDisabled || checkAllStatus .disabled ) return
301
282
302
- const currentVisibleKeys = treeRef .value . methods
283
+ const currentVisibleKeys = treeRef .value
303
284
.getCurrentVisibleNodes ()
304
- .map ((node : any ) => node [treeRef .value .methods . keyField ])
285
+ .map ((node : any ) => node [treeRef .value .keyField ])
305
286
if (checkAllStatus .checked || checkAllStatus .indeterminate ) {
306
287
// 反选
307
- treeRef .value .methods . setCheckedKeys (currentVisibleKeys , false )
288
+ treeRef .value .setCheckedKeys (currentVisibleKeys , false )
308
289
} else {
309
290
// 全选
310
- treeRef .value .methods . setCheckedKeys (currentVisibleKeys , true )
291
+ treeRef .value .setCheckedKeys (currentVisibleKeys , true )
311
292
}
312
293
313
294
updateCheckAllStatus ()
@@ -325,10 +306,10 @@ export default defineComponent({
325
306
isShowingChecked .value = ! isShowingChecked .value
326
307
if (isShowingChecked .value ) {
327
308
// 展示已选
328
- treeRef .value .methods . showCheckedNodes ()
309
+ treeRef .value .showCheckedNodes ()
329
310
} else {
330
311
// 恢复展示
331
- treeRef .value .methods . filter (keyword .value , () => true )
312
+ treeRef .value .filter (keyword .value , () => true )
332
313
}
333
314
334
315
updateCheckAllStatus ()
@@ -353,7 +334,7 @@ export default defineComponent({
353
334
// #endregion Event handlers
354
335
/** 更新全选复选框状态 */
355
336
function updateCheckAllStatus(): void {
356
- const currentVisibleNodes = treeRef .value .methods . getCurrentVisibleNodes ()
337
+ const currentVisibleNodes = treeRef .value .getCurrentVisibleNodes ()
357
338
const length = currentVisibleNodes .length
358
339
let hasChecked = false
359
340
let hasUnchecked = false
@@ -379,39 +360,19 @@ export default defineComponent({
379
360
}
380
361
381
362
function updateCheckedCount(): void {
382
- checkedCount .value = treeRef .value .methods . getCheckedKeys ().length
363
+ checkedCount .value = treeRef .value .getCheckedKeys ().length
383
364
}
384
365
385
366
function checkedChange(value1 : any , value2 : any ) {
386
367
updateCheckAllStatus ()
387
368
emit (' checked-change' , value1 , value2 )
388
369
}
389
370
390
- function setData () {
371
+ function onSetData () {
391
372
emit (' set-data' )
392
373
handleSetData ()
393
374
}
394
375
onMounted (() => {
395
- // 因为获取不到 CTree.methods 的类型,所以这边 methods 的类型不好写
396
- // const methods: {
397
- // [key in keyof CTreeApiMethodsType]: CTreeApiMethodsType[key]
398
- // } = treeRef.value.methods
399
- // for (let key in methods) {
400
- // const keyCache: keyof CTreeApiMethodsType =
401
- // key as keyof CTreeApiMethodsType
402
- // if (API_METHODS.indexOf(keyCache) > -1) {
403
- // // 躲避 TypeScript 类型推断错误
404
- // const _methods = ctreeMethods as any
405
- // _methods[keyCache] = function <
406
- // T extends (typeof ctreeMethods)[typeof keyCache]
407
- // >(...args: Parameters<T>): ReturnType<T> {
408
- // return (methods as any)[keyCache].apply(
409
- // (this as any).$refs.treeRef.value as CTreeInstanceType,
410
- // args
411
- // )
412
- // }
413
- // }
414
- // }
415
376
if (checkable .value && ! checkedCount .value ) {
416
377
handleSetData ()
417
378
}
@@ -458,7 +419,7 @@ export default defineComponent({
458
419
updateCheckAllStatus ,
459
420
getKeyword ,
460
421
checkedChange ,
461
- setData ,
422
+ onSetData ,
462
423
clearKeyword ,
463
424
search
464
425
}
0 commit comments