Skip to content

Commit

Permalink
feat(tree): add expose some state, close #4820
Browse files Browse the repository at this point in the history
  • Loading branch information
tangjinzhou committed Oct 29, 2021
1 parent c16a5d4 commit fbd9a21
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
12 changes: 10 additions & 2 deletions components/tree/DirectoryTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataNode[]>(
props.treeData || convertTreeToData(filterEmpty(slots.default?.())),
);

watch(
() => props.treeData,
() => {
Expand All @@ -79,7 +80,14 @@ export default defineComponent({
const cachedSelectedKeys = ref<Key[]>();

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);

Expand Down
8 changes: 7 additions & 1 deletion components/tree/Tree.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(() => {
Expand Down
6 changes: 6 additions & 0 deletions components/vc-tree/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit fbd9a21

Please sign in to comment.