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

fix:doesn't scroll when use scrollTo with top zero #1180

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/examples/scrollY.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ const Test = () => {
>
Scroll To key 9
</button>
<button
onClick={() => {
tblRef.current?.scrollTo({
top: 0,
});
}}
>
Scroll To top
</button>
<Table
ref={tblRef}
columns={columns}
Expand Down
3 changes: 2 additions & 1 deletion src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ function Table<RecordType extends DefaultRecordType>(
// Native scroll
const { index, top, key } = config;

if (top) {
// * 考虑top为0的情况
if (top || top === 0) {
Copy link
Member

@li-jia-nan li-jia-nan Aug 24, 2024

Choose a reason for hiding this comment

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

逻辑不太清晰,不能依靠注释来解释逻辑,我的建议是写一个方法判断:

function isVerifyValue(value: any) {
  return typeof value === "number" && !Number.isNaN(value);
}

if (isValidValue(top)) {
  scrollBodyRef.current?.scrollTo({ top });
}

Copy link
Member

Choose a reason for hiding this comment

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

scrollBodyRef.current?.scrollTo({
top,
});
Expand Down
18 changes: 18 additions & 0 deletions tests/Scroll.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,22 @@ describe('Table.Scroll', () => {
domSpy.mockRestore();
vi.useRealTimers();
});

it('trigger inner scrollTo when set `top` 0 after render', () => {
let isTriggerScroll = false;
spyElementPrototypes(HTMLElement, {
scrollTo: _ => {
isTriggerScroll = true;
},
});

const tRef = React.createRef();

const wrapper = mount(createTable({ ref: tRef }));

tRef.current.scrollTo({
top: 0,
});
expect(isTriggerScroll).toEqual(true);
});
});