Skip to content

Commit

Permalink
fix:doesn't scroll when use scrollTo with top zero (#1180)
Browse files Browse the repository at this point in the history
Co-authored-by: KAROS\81948 <chenwenfan@harmonycloud.cn>
Co-authored-by: afc163 <afc163@gmail.com>
  • Loading branch information
3 people authored Aug 23, 2024
1 parent 9abaa26 commit 8b02e79
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
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) {
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);
});
});

0 comments on commit 8b02e79

Please sign in to comment.