Skip to content

Commit c2b48ac

Browse files
authored
fix: should not reset virtual scrollLeft when data changed (#1146)
1 parent 80ed65b commit c2b48ac

File tree

3 files changed

+240
-3
lines changed

3 files changed

+240
-3
lines changed

src/Table.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,9 @@ function Table<RecordType extends DefaultRecordType>(
485485
const triggerOnScroll = () => {
486486
if (horizonScroll && scrollBodyRef.current) {
487487
onInternalScroll({
488-
currentTarget: getDOM(scrollBodyRef.current),
489-
} as React.UIEvent<HTMLDivElement>);
488+
currentTarget: getDOM(scrollBodyRef.current) as HTMLElement,
489+
scrollLeft: scrollBodyRef.current?.scrollLeft,
490+
});
490491
} else {
491492
setPingedLeft(false);
492493
setPingedRight(false);

tests/Virtual.spec.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ vi.mock('rc-virtual-list', async () => {
3333
describe('Table.Virtual', () => {
3434
let scrollLeftCalled = false;
3535

36+
const setScrollLeft = vi.fn();
37+
3638
beforeAll(() => {
3739
spyElementPrototypes(HTMLElement, {
3840
getBoundingClientRect: () => ({
@@ -43,7 +45,7 @@ describe('Table.Virtual', () => {
4345
scrollLeftCalled = true;
4446
return 100;
4547
},
46-
set: () => {},
48+
set: setScrollLeft as any,
4749
},
4850
clientWidth: {
4951
get: () => 80,
@@ -56,6 +58,7 @@ describe('Table.Virtual', () => {
5658

5759
beforeEach(() => {
5860
scrollLeftCalled = false;
61+
setScrollLeft.mockReset();
5962
global.scrollToConfig = null;
6063
vi.useFakeTimers();
6164
resetWarned();
@@ -210,6 +213,35 @@ describe('Table.Virtual', () => {
210213
expect(scrollLeftCalled).toBeTruthy();
211214
});
212215

216+
it('should not reset scroll when data changed', async () => {
217+
const { container, rerender } = getTable();
218+
219+
resize(container.querySelector('.rc-table')!);
220+
221+
rerender(
222+
<VirtualTable
223+
columns={[
224+
{
225+
dataIndex: 'name',
226+
},
227+
{
228+
dataIndex: 'age',
229+
},
230+
{
231+
dataIndex: 'address',
232+
},
233+
]}
234+
rowKey="name"
235+
scroll={{ x: 100, y: 100 }}
236+
data={[{}]}
237+
/>,
238+
);
239+
vi.runAllTimers();
240+
241+
// mock scrollLeft is 100, but virtual offsetX is 0
242+
expect(setScrollLeft).toHaveBeenCalledWith(undefined, 0);
243+
});
244+
213245
it('should follow correct width', () => {
214246
const { container } = getTable({
215247
columns: [

0 commit comments

Comments
 (0)