diff --git a/components/tree/DirectoryTree.tsx b/components/tree/DirectoryTree.tsx index 6aa77f545..8a3532ffa 100644 --- a/components/tree/DirectoryTree.tsx +++ b/components/tree/DirectoryTree.tsx @@ -55,11 +55,12 @@ export default defineComponent({ 'dblclick', 'click', ], - setup(props, { attrs, slots, emit }) { + setup(props, { attrs, slots, emit, expose }) { // convertTreeToData 兼容 a-tree-node 历史写法,未来a-tree-node移除后,删除相关代码,不要再render中调用 treeData,否则死循环 const treeData = ref( props.treeData || convertTreeToData(filterEmpty(slots.default?.())), ); + watch( () => props.treeData, () => { @@ -79,7 +80,14 @@ export default defineComponent({ const cachedSelectedKeys = ref(); const treeRef = ref(); - + expose({ + selectedKeys: computed(() => treeRef.value?.selectedKeys), + checkedKeys: computed(() => treeRef.value?.checkedKeys), + halfCheckedKeys: computed(() => treeRef.value?.halfCheckedKeys), + loadedKeys: computed(() => treeRef.value?.loadedKeys), + loadingKeys: computed(() => treeRef.value?.loadingKeys), + expandedKeys: computed(() => treeRef.value?.expandedKeys), + }); const getInitExpandedKeys = () => { const { keyEntities } = convertDataToEntities(treeData.value); diff --git a/components/tree/Tree.tsx b/components/tree/Tree.tsx index 9673f85a0..43e7011be 100644 --- a/components/tree/Tree.tsx +++ b/components/tree/Tree.tsx @@ -1,5 +1,5 @@ import type { PropType, ExtractPropTypes } from 'vue'; -import { watchEffect, ref, defineComponent } from 'vue'; +import { watchEffect, ref, defineComponent, computed } from 'vue'; import classNames from '../_util/classNames'; import VcTree, { TreeNode } from '../vc-tree'; import PropTypes from '../_util/vue-types'; @@ -164,6 +164,12 @@ export default defineComponent({ onNodeExpand: (...args) => { treeRef.value?.onNodeExpand(...args); }, + selectedKeys: computed(() => treeRef.value?.selectedKeys), + checkedKeys: computed(() => treeRef.value?.checkedKeys), + halfCheckedKeys: computed(() => treeRef.value?.halfCheckedKeys), + loadedKeys: computed(() => treeRef.value?.loadedKeys), + loadingKeys: computed(() => treeRef.value?.loadingKeys), + expandedKeys: computed(() => treeRef.value?.expandedKeys), }); watchEffect(() => { diff --git a/components/vc-tree/Tree.tsx b/components/vc-tree/Tree.tsx index 476801ba6..d0f1f7017 100644 --- a/components/vc-tree/Tree.tsx +++ b/components/vc-tree/Tree.tsx @@ -952,6 +952,12 @@ export default defineComponent({ onNodeExpand, scrollTo, onKeydown, + selectedKeys: computed(() => selectedKeys.value), + checkedKeys: computed(() => checkedKeys.value), + halfCheckedKeys: computed(() => halfCheckedKeys.value), + loadedKeys: computed(() => loadedKeys.value), + loadingKeys: computed(() => loadingKeys.value), + expandedKeys: computed(() => expandedKeys.value), }); onUnmounted(() => { window.removeEventListener('dragend', onWindowDragEnd);