Skip to content

Commit fbd9a21

Browse files
committed
feat(tree): add expose some state, close #4820
1 parent c16a5d4 commit fbd9a21

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

components/tree/DirectoryTree.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ export default defineComponent({
5555
'dblclick',
5656
'click',
5757
],
58-
setup(props, { attrs, slots, emit }) {
58+
setup(props, { attrs, slots, emit, expose }) {
5959
// convertTreeToData 兼容 a-tree-node 历史写法,未来a-tree-node移除后,删除相关代码,不要再render中调用 treeData,否则死循环
6060
const treeData = ref<DataNode[]>(
6161
props.treeData || convertTreeToData(filterEmpty(slots.default?.())),
6262
);
63+
6364
watch(
6465
() => props.treeData,
6566
() => {
@@ -79,7 +80,14 @@ export default defineComponent({
7980
const cachedSelectedKeys = ref<Key[]>();
8081

8182
const treeRef = ref();
82-
83+
expose({
84+
selectedKeys: computed(() => treeRef.value?.selectedKeys),
85+
checkedKeys: computed(() => treeRef.value?.checkedKeys),
86+
halfCheckedKeys: computed(() => treeRef.value?.halfCheckedKeys),
87+
loadedKeys: computed(() => treeRef.value?.loadedKeys),
88+
loadingKeys: computed(() => treeRef.value?.loadingKeys),
89+
expandedKeys: computed(() => treeRef.value?.expandedKeys),
90+
});
8391
const getInitExpandedKeys = () => {
8492
const { keyEntities } = convertDataToEntities(treeData.value);
8593

components/tree/Tree.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { PropType, ExtractPropTypes } from 'vue';
2-
import { watchEffect, ref, defineComponent } from 'vue';
2+
import { watchEffect, ref, defineComponent, computed } from 'vue';
33
import classNames from '../_util/classNames';
44
import VcTree, { TreeNode } from '../vc-tree';
55
import PropTypes from '../_util/vue-types';
@@ -164,6 +164,12 @@ export default defineComponent({
164164
onNodeExpand: (...args) => {
165165
treeRef.value?.onNodeExpand(...args);
166166
},
167+
selectedKeys: computed(() => treeRef.value?.selectedKeys),
168+
checkedKeys: computed(() => treeRef.value?.checkedKeys),
169+
halfCheckedKeys: computed(() => treeRef.value?.halfCheckedKeys),
170+
loadedKeys: computed(() => treeRef.value?.loadedKeys),
171+
loadingKeys: computed(() => treeRef.value?.loadingKeys),
172+
expandedKeys: computed(() => treeRef.value?.expandedKeys),
167173
});
168174

169175
watchEffect(() => {

components/vc-tree/Tree.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,12 @@ export default defineComponent({
952952
onNodeExpand,
953953
scrollTo,
954954
onKeydown,
955+
selectedKeys: computed(() => selectedKeys.value),
956+
checkedKeys: computed(() => checkedKeys.value),
957+
halfCheckedKeys: computed(() => halfCheckedKeys.value),
958+
loadedKeys: computed(() => loadedKeys.value),
959+
loadingKeys: computed(() => loadingKeys.value),
960+
expandedKeys: computed(() => expandedKeys.value),
955961
});
956962
onUnmounted(() => {
957963
window.removeEventListener('dragend', onWindowDragEnd);

0 commit comments

Comments
 (0)