Skip to content

Commit 8b02e79

Browse files
IsKarosKAROS\81948afc163
authored
fix:doesn't scroll when use scrollTo with top zero (#1180)
Co-authored-by: KAROS\81948 <[email protected]> Co-authored-by: afc163 <[email protected]>
1 parent 9abaa26 commit 8b02e79

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

docs/examples/scrollY.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ const Test = () => {
5959
>
6060
Scroll To key 9
6161
</button>
62+
<button
63+
onClick={() => {
64+
tblRef.current?.scrollTo({
65+
top: 0,
66+
});
67+
}}
68+
>
69+
Scroll To top
70+
</button>
6271
<Table
6372
ref={tblRef}
6473
columns={columns}

src/Table.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ function Table<RecordType extends DefaultRecordType>(
332332
// Native scroll
333333
const { index, top, key } = config;
334334

335-
if (top) {
335+
// * 考虑top为0的情况
336+
if (top || top === 0) {
336337
scrollBodyRef.current?.scrollTo({
337338
top,
338339
});

tests/Scroll.spec.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,22 @@ describe('Table.Scroll', () => {
135135
domSpy.mockRestore();
136136
vi.useRealTimers();
137137
});
138+
139+
it('trigger inner scrollTo when set `top` 0 after render', () => {
140+
let isTriggerScroll = false;
141+
spyElementPrototypes(HTMLElement, {
142+
scrollTo: _ => {
143+
isTriggerScroll = true;
144+
},
145+
});
146+
147+
const tRef = React.createRef();
148+
149+
const wrapper = mount(createTable({ ref: tRef }));
150+
151+
tRef.current.scrollTo({
152+
top: 0,
153+
});
154+
expect(isTriggerScroll).toEqual(true);
155+
});
138156
});

0 commit comments

Comments
 (0)