Skip to content

Conversation

@MadCcc
Copy link
Member

@MadCcc MadCcc commented Feb 10, 2022

close ant-design/ant-design#33986

Bug 触发的原因是第一次有 active 的选项时,dropdown还没有显示,这时候是被藏在页面左上角的,所以整个视窗定位之后会回到顶部。
这个 PR 的解决方法就是把 dropdown 的 getBoundingClientRect().top 加入 deps 数组监听,但是这样做会使每次打开 cascader 的时候都 scroll 到 active 项的位置。
用 raf 在 getBoundingClientRect().top 大于 0 时再触发 scroll,不影响 deps
Plan C: 只滚动父元素

@vercel
Copy link

vercel bot commented Feb 10, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/react-component/cascader/EvmCNTSyQAmTABJ7jhidDfRpAk8p
✅ Preview: https://cascader-git-fix-scroll-react-component.vercel.app

@codecov
Copy link

codecov bot commented Feb 10, 2022

Codecov Report

Merging #248 (4959198) into master (eca071e) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #248   +/-   ##
=======================================
  Coverage   99.78%   99.79%           
=======================================
  Files          15       15           
  Lines         474      484   +10     
  Branches      118      122    +4     
=======================================
+ Hits          473      483   +10     
  Misses          1        1           
Impacted Files Coverage Δ
src/OptionList/index.tsx 100.00% <100.00%> (ø)
src/utils/commonUtil.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eca071e...4959198. Read the comment docs.

@MadCcc MadCcc requested review from afc163 and zombieJ February 10, 2022 14:05
useKeyboard(ref, mergedOptions, fieldNames, activeValueCells, onPathOpen, onKeyboardSelect);

// >>>>> Active Scroll
const containerTop = containerRef.current?.getBoundingClientRect().top || 0;
Copy link
Member

Choose a reason for hiding this comment

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

这个还不如直接下面的 effect 来个 raf 做 scroll...

ele?.scrollIntoView?.({ block: 'nearest' });
}
const scrollOptionIntoView = () => {
if (containerRef.current?.getBoundingClientRect().top > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

试了下这个似乎判断不了,这个方法没判断是否在视窗内。

Copy link
Member

Choose a reason for hiding this comment

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

👌

ele?.scrollIntoView?.({ block: 'nearest' });
}
} else {
raf(scrollOptionIntoView);
Copy link
Member

Choose a reason for hiding this comment

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

另外测一下是不是一帧够了,按理说不需要循环等待。而且这个嵌套的 raf effect 的 cleanup 也没有处理到

Copy link
Member Author

Choose a reason for hiding this comment

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

试了一下一帧没有效果

Copy link
Member Author

Choose a reason for hiding this comment

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

处理了下嵌套 raf 的 cleanup

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.

Cascader 首次点击会使浏览器回到顶部

3 participants