Skip to content

Commit

Permalink
feat(tree): support scroll instance function and key param (#3415)
Browse files Browse the repository at this point in the history
  • Loading branch information
uyarn authored Dec 5, 2024
1 parent d7464f4 commit 33e9a50
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/tree/_example-composition/vscroll.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<t-space>
<t-button @click="append()">插入根节点</t-button>
</t-space>
<t-space>
<t-button @click="scrollTo()">滚动到指定节点</t-button>
</t-space>
</t-space>
<t-tree
ref="tree"
Expand Down Expand Up @@ -129,4 +132,14 @@ const append = (node) => {
const remove = (node) => {
node.remove();
};
const scrollTo = () => {
tree.value.scrollTo({
// 指定key滚动,即当前节点对应的唯一值,推荐使用
key: 't30',
behavior: 'smooth',
// 指定index滚动,如果存在多级嵌套,需要自己计算index
// index: 100,
});
};
</script>
13 changes: 13 additions & 0 deletions src/tree/_example/vscroll.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<t-space>
<t-button @click="append()">插入根节点</t-button>
</t-space>
<t-space>
<t-button @click="scrollTo()">滚动到指定节点</t-button>
</t-space>
</t-space>
<t-tree
ref="tree"
Expand Down Expand Up @@ -144,6 +147,16 @@ export default {
remove(node) {
node.remove();
},
scrollTo() {
const { tree } = this.$refs;
tree.scrollTo({
// 指定key滚动,即当前节点对应的唯一值,推荐使用
key: 't30',
behavior: 'smooth',
// 指定index滚动,如果存在多级嵌套,需要自己计算index
// index: 100,
});
},
},
};
</script>
22 changes: 21 additions & 1 deletion src/tree/hooks/useTreeScroll.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import get from 'lodash/get';
import {
computed, onMounted, TypeRef, useVirtualScroll, TypeScroll, TreeNode,
} from '../adapt';
import { TypeTreeState, TypeTimer } from '../tree-types';
import log from '../../_common/js/log';

import type { ComponentScrollToElementParams } from '../../common';

// tree 虚拟滚动整合
export default function useTreeScroll(state: TypeTreeState) {
Expand Down Expand Up @@ -76,11 +80,27 @@ export default function useTreeScroll(state: TypeTreeState) {
emitScrollEvent(e);
};

const handleScrollTo = (params: ComponentScrollToElementParams) => {
let { index } = params;
if (!index && index !== 0) {
if (!params.key) {
log.error('Tree', 'scrollToElement: one of `index` or `key` must exist.');
return;
}
index = allNodes.value?.findIndex((item) => [get(item.data, 'key'), get(item.data, 'value')].includes(params.key));
if (index < 0) {
log.error('Tree', `${params.key} does not exist in data, check \`key\` or \`data\` please.`);
return;
}
}
virtualConfig.scrollToElement({ ...params, index: index - 1 });
};

return {
// 虚拟滚动相关
treeContentRef,
onInnerVirtualScroll,
virtualConfig,
scrollToElement: virtualConfig.scrollToElement,
scrollToElement: handleScrollTo,
};
}
6 changes: 4 additions & 2 deletions src/tree/tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default defineComponent({

useDragHandle(state);
const { setActived, setExpanded, setChecked } = useTreeAction(state);
const { onInnerVirtualScroll, virtualConfig } = useTreeScroll(state);
const { onInnerVirtualScroll, virtualConfig, scrollToElement } = useTreeScroll(state);
const { renderTreeNodes, nodesEmpty } = useTreeNodes(state);
const {
treeClasses, treeContentStyles, scrollStyles, cursorStyles,
Expand Down Expand Up @@ -86,7 +86,9 @@ export default defineComponent({
scrollStyles,
cursorStyles,
virtualConfig,
scrollToElement: virtualConfig.scrollToElement,
scrollTo: scrollToElement,
// deprecated
scrollToElement,
};
},

Expand Down
23 changes: 23 additions & 0 deletions test/snap/__snapshots__/csr.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -131045,6 +131045,29 @@ exports[`csr snapshot test > csr test ./src/tree/_example/vscroll.vue 1`] = `
</div>
</div>
</div>
<div
class="t-space-item"
>
<div
class="t-space t-space-horizontal"
style="gap: 16px;"
>
<div
class="t-space-item"
>
<button
class="t-button t-button--variant-base t-button--theme-primary"
type="button"
>
<span
class="t-button__text"
>
滚动到指定节点
</span>
</button>
</div>
</div>
</div>
</div>
</div>
<div
Expand Down
2 changes: 1 addition & 1 deletion test/snap/__snapshots__/ssr.test.js.snap

Large diffs are not rendered by default.

0 comments on commit 33e9a50

Please sign in to comment.