Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Tree): support scrollIntoView, close #2930 #4860

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

wangw11056
Copy link
Collaborator

@wangw11056 wangw11056 commented Jun 12, 2024

Add a new property to the Tree component to support the scrollIntoView when using the filterTreeNode property. close #2930

@@ -1239,7 +1281,7 @@ export class Tree extends Component<TreeProps, TreeState> {
return (
<ul
role="tree"
ref={ref}
ref={ref || this.normalListRef}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normalListRef 是在 ref 没有时才有用,这样感觉后续基于 normalListRef 的逻辑都很不稳定

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果要两个地方都要拿到 ref,那应该是包裹在同一个函数里。

* @defaultValue false
* @skip
*/
scrollIntoView?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个名字感觉不是很准确,应该强调是将 filter 的 node 滚动到 view。另外这个属性为什么要 @Skip?还有就是解释里的话应该说明一下是将哪个 filter 的 node 滚到到视区内。

const itemNode = treeNode.querySelector<
HTMLElement & { scrollIntoViewIfNeeded?: () => void }
>(`.${prefix}tree-node.${prefix}filtered`);
itemNode && itemNode.scrollIntoViewIfNeeded && itemNode.scrollIntoViewIfNeeded();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scrollIntoViewIfNeeded 是有兼容性问题的,应该考虑 fallback 到 scrollIntoView,参考其他组件实现,另外这一连串的判断用 optional chain 就可以了。

const { prefix } = this.props;

clearTimeout(this.highlightTimer);
this.highlightTimer = window.setTimeout(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个叫 highlightTimer 是有点奇怪的,这里面做的并不是 highlight 的事情。另外这个 timer 在没有组件销毁时的处理逻辑。

this.virtualListRef = createRef();

this.handleTreeNodeScrollIntoView();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要在 constructor 里引入副效应,如果需要组件 mount 后立刻执行,可以是在didMount 里做

@@ -531,6 +540,39 @@ export class Tree extends Component<TreeProps, TreeState> {
};
}

componentDidUpdate() {
this.handleTreeNodeScrollIntoView();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个感觉太频繁了,会影响性能,同时可能会影响到用户的正常操作,每次 scroll 都会改变位置,对用户也是打扰,还是需要比较谨慎的。这里至少应该是只在筛选词发生变化时才执行,这样才比较符合用户预期。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Tree]Tree查询能否支持高亮的同时,并滚动到具体位置?
2 participants