Browse Source

feat(tree): add expose some state, close #4820

pull/4825/head
tangjinzhou 3 years ago
parent
commit
fbd9a21dc8
  1. 12
      components/tree/DirectoryTree.tsx
  2. 8
      components/tree/Tree.tsx
  3. 6
      components/vc-tree/Tree.tsx

12
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-noderender treeData
const treeData = ref<DataNode[]>(
props.treeData || convertTreeToData(filterEmpty(slots.default?.())),
);
watch(
() => props.treeData,
() => {
@ -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);

8
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(() => {

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

Loading…
Cancel
Save