Skip to content

Commit e53c2bb

Browse files
committed
fix: 修复treedrop没有继承tree函数的问题,以及treesearch调用tree函数的bug
1 parent ddf6626 commit e53c2bb

File tree

3 files changed

+39
-54
lines changed

3 files changed

+39
-54
lines changed

src/components/TreeDrop.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import CTreeSearch from './TreeSearch.vue'
6262
import { TreeNode } from '../store'
6363
import { placementEnum } from '../const'
6464
import { PlacementType, TreeNodeKeyType, TreeDropSlotProps } from '../types'
65+
import { getCtreeMethods } from '../utils'
6566
6667
const prefixCls = 'ctree-tree-drop'
6768
@@ -449,6 +450,7 @@ export default defineComponent({
449450
)
450451
//#endr
451452
return {
453+
...getCtreeMethods(treeSearchRef),
452454
treeSearchValue,
453455
dropdownVisible,
454456
checkedCount,

src/components/TreeSearch.vue

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
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'
6666
import CTree from './Tree.vue'
67-
import { API_METHODS, ApiType } from '../const'
67+
import { ApiType } from '../const'
6868
import type { TreeNodeKeyType } from '../types'
69+
import { getCtreeMethods } from '../utils'
6970
7071
const prefixCls = 'ctree-tree-search'
7172
7273
const 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-
9475
export 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
}

src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import CTree from './components/Tree.vue'
2+
import type {Ref} from 'vue'
3+
import { API_METHODS } from './const'
4+
5+
type CTreeInstanceType = InstanceType<typeof CTree>
6+
type CtreeInstanceKeyType = keyof CTreeInstanceType
7+
8+
// Vue 2.6 内部没改变的话可以这样获取 Vue.extend 中的 methods。Vue 版本有升级的话需要注意这个特性有没有改变
9+
// 如果是对象的话可以直接 CTree.methods ,并且是安全的。
10+
// let ctreeMethods: CTreeApiMethodsType = {} as CTreeApiMethodsType
11+
12+
export function getCtreeMethods<T>(treeRef: Ref<T>) {
13+
return API_METHODS.reduce((prev, cur) => {
14+
prev[cur] = function (...args: any[]) {
15+
return (treeRef.value[cur as keyof T] as Function).apply(
16+
treeRef.value,
17+
args
18+
)
19+
}
20+
return prev
21+
}, {} as Record<string, Function>)
22+
}

0 commit comments

Comments
 (0)