Skip to content

Commit 4b7ff3b

Browse files
committed
feat: 实现客户端滚动支持
1 parent c24581c commit 4b7ff3b

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/BootstrapBlazor/Components/TreeView/TreeView.razor.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,44 @@ export function init(id, options) {
2828
});
2929
}
3030

31-
export function scroll(id, options) {
31+
const scrollIntoView = (el, options) => {
32+
if (el) {
33+
el.scrollIntoView(options ?? { behavior: 'smooth', block: 'nearest', inline: 'start' });
34+
}
35+
}
36+
37+
const virtualScroll = (el, options) => {
38+
const searchHeight = el.querySelector('.tree-search')?.offsetHeight ?? 0;
39+
40+
el.scrollHandler = setInterval(() => {
41+
const item = el.querySelector(".tree-content.active");
42+
const root = el.querySelector(".is-virtual");
43+
if (item) {
44+
clearInterval(el.scrollHandler);
45+
scrollIntoView(item, options);
46+
return;
47+
}
48+
49+
const top = el.scrollTop + el.offsetHeight - searchHeight;
50+
if (top < root.offsetHeight) {
51+
el.scrollTo({ top: top, left: 0, behavior: "smooth" });
52+
}
53+
else {
54+
clearInterval(el.scrollHandler);
55+
}
56+
}, 80);
57+
}
58+
59+
export function scroll(id, options, find = false) {
3260
const el = document.getElementById(id);
61+
3362
const item = el.querySelector(".tree-content.active");
3463
if (item) {
35-
item.scrollIntoView(options ?? { behavior: 'smooth', block: 'nearest', inline: 'start' });
64+
scrollIntoView(el, options);
65+
return;
66+
}
67+
if (find) {
68+
virtualScroll(el, options);
3669
}
3770
}
3871

@@ -113,5 +146,8 @@ export function dispose(id) {
113146

114147
if (el) {
115148
EventHandler.off(el, 'keyup', '.tree-root');
149+
if (el.scrollHandler) {
150+
clearInterval(el.scrollHandler);
151+
}
116152
}
117153
}

0 commit comments

Comments
 (0)