3535 ref =" treeRef"
3636 v-bind =" $attrs"
3737 v-model =" treeModelValue"
38- @set-data =" setData "
38+ @set-data =" onSetData "
3939 @checked-change =" checkedChange"
4040 >
4141 <template v-for =" (_ , slot ) in $slots " :name =" slot " v-slot =" scope " >
@@ -64,33 +64,14 @@ import {
6464 PropType
6565} from ' vue-demi'
6666import CTree from ' ./Tree.vue'
67- import { API_METHODS , ApiType } from ' ../const'
67+ import { ApiType } from ' ../const'
6868import type { TreeNodeKeyType } from ' ../types'
69+ import { getCtreeMethods } from ' ../utils'
6970
7071const prefixCls = ' ctree-tree-search'
7172
7273const treeNodePrefixCls = ' ctree-tree-node'
7374
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-
9475export default defineComponent ({
9576 name: ' CTreeSearch' ,
9677 inheritAttrs: false ,
@@ -278,13 +259,13 @@ export default defineComponent({
278259 } else {
279260 if (props .searchRemote ) {
280261 // 远程搜索
281- treeRef .value .methods . loadRootNodes ().then (() => {
262+ treeRef .value .loadRootNodes ().then (() => {
282263 updateCheckAllStatus ()
283264 resolve ()
284265 })
285266 } else {
286267 // 本地搜索
287- treeRef .value .methods . filter (searchKeyword )
268+ treeRef .value .filter (searchKeyword )
288269 updateCheckAllStatus ()
289270 resolve ()
290271 }
@@ -299,15 +280,15 @@ export default defineComponent({
299280 function handleCheckAll(): void {
300281 if (props .searchDisabled || checkAllStatus .disabled ) return
301282
302- const currentVisibleKeys = treeRef .value . methods
283+ const currentVisibleKeys = treeRef .value
303284 .getCurrentVisibleNodes ()
304- .map ((node : any ) => node [treeRef .value .methods . keyField ])
285+ .map ((node : any ) => node [treeRef .value .keyField ])
305286 if (checkAllStatus .checked || checkAllStatus .indeterminate ) {
306287 // 反选
307- treeRef .value .methods . setCheckedKeys (currentVisibleKeys , false )
288+ treeRef .value .setCheckedKeys (currentVisibleKeys , false )
308289 } else {
309290 // 全选
310- treeRef .value .methods . setCheckedKeys (currentVisibleKeys , true )
291+ treeRef .value .setCheckedKeys (currentVisibleKeys , true )
311292 }
312293
313294 updateCheckAllStatus ()
@@ -325,10 +306,10 @@ export default defineComponent({
325306 isShowingChecked .value = ! isShowingChecked .value
326307 if (isShowingChecked .value ) {
327308 // 展示已选
328- treeRef .value .methods . showCheckedNodes ()
309+ treeRef .value .showCheckedNodes ()
329310 } else {
330311 // 恢复展示
331- treeRef .value .methods . filter (keyword .value , () => true )
312+ treeRef .value .filter (keyword .value , () => true )
332313 }
333314
334315 updateCheckAllStatus ()
@@ -353,7 +334,7 @@ export default defineComponent({
353334 // #endregion Event handlers
354335 /** 更新全选复选框状态 */
355336 function updateCheckAllStatus(): void {
356- const currentVisibleNodes = treeRef .value .methods . getCurrentVisibleNodes ()
337+ const currentVisibleNodes = treeRef .value .getCurrentVisibleNodes ()
357338 const length = currentVisibleNodes .length
358339 let hasChecked = false
359340 let hasUnchecked = false
@@ -379,39 +360,19 @@ export default defineComponent({
379360 }
380361
381362 function updateCheckedCount(): void {
382- checkedCount .value = treeRef .value .methods . getCheckedKeys ().length
363+ checkedCount .value = treeRef .value .getCheckedKeys ().length
383364 }
384365
385366 function checkedChange(value1 : any , value2 : any ) {
386367 updateCheckAllStatus ()
387368 emit (' checked-change' , value1 , value2 )
388369 }
389370
390- function setData () {
371+ function onSetData () {
391372 emit (' set-data' )
392373 handleSetData ()
393374 }
394375 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- // }
415376 if (checkable .value && ! checkedCount .value ) {
416377 handleSetData ()
417378 }
@@ -458,7 +419,7 @@ export default defineComponent({
458419 updateCheckAllStatus ,
459420 getKeyword ,
460421 checkedChange ,
461- setData ,
422+ onSetData ,
462423 clearKeyword ,
463424 search
464425 }
0 commit comments