Skip to content

Commit a7b68af

Browse files
authored
fix: Adjust the top height of the proxy to solve the sliding issue (#1310) (#1313)
1 parent b94558d commit a7b68af

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/VirtualTable/BodyGrid.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
104104
},
105105
});
106106

107+
// https://github.com/ant-design/ant-design/issues/54734
108+
Object.defineProperty(obj, 'scrollTop', {
109+
get: () => listRef.current?.getScrollInfo().y || 0,
110+
111+
set: (value: number) => {
112+
listRef.current?.scrollTo({
113+
top: value,
114+
});
115+
},
116+
});
117+
107118
return obj;
108119
});
109120

tests/Virtual.spec.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { resetWarned } from '@rc-component/util/lib/warning';
66
import React from 'react';
77
import { VirtualTable, type Reference, type VirtualTableProps } from '../src';
88

9+
const identity = (value: any) => value;
10+
911
global.scrollToConfig = null;
12+
global.collectGetScrollInfoReturn = identity;
1013

1114
vi.mock('rc-virtual-list', async () => {
1215
const RealVirtualList = ((await vi.importActual('rc-virtual-list')) as any).default;
@@ -20,6 +23,10 @@ vi.mock('rc-virtual-list', async () => {
2023
global.scrollToConfig = config;
2124
return myRef.current.scrollTo(config);
2225
},
26+
getScrollInfo: () => {
27+
const originResult = myRef.current.getScrollInfo();
28+
return global.collectGetScrollInfoReturn(originResult);
29+
},
2330
}));
2431

2532
return <RealVirtualList ref={myRef} {...props} data-scroll-width={props.scrollWidth} />;
@@ -59,7 +66,8 @@ describe('Table.Virtual', () => {
5966
beforeEach(() => {
6067
scrollLeftCalled = false;
6168
setScrollLeft.mockReset();
62-
global.scrollToConfig = null;
69+
global.scrollToConfig = vi.fn(identity);
70+
// global.collectGetScrollInfoReturn.mockReset();
6371
vi.useFakeTimers();
6472
resetWarned();
6573
});
@@ -606,4 +614,26 @@ describe('Table.Virtual', () => {
606614
expect(container.querySelector('.rc-table')).toHaveClass('rc-table-fix-end-shadow-show');
607615
});
608616
});
617+
618+
/**
619+
* In antd, we need to call the scrollTop method through ref to achieve scrolling.
620+
* see: https://github.com/ant-design/ant-design/issues/54734
621+
*/
622+
it('should get and set scrollTop correctly', async () => {
623+
const ref = React.createRef<any>();
624+
625+
global.collectGetScrollInfoReturn = (origin: any) => ({
626+
...origin,
627+
y: 50,
628+
});
629+
630+
getTable({ internalRefs: { body: ref } });
631+
632+
expect(ref.current.scrollTop).toBe(50);
633+
634+
ref.current.scrollTop = 200;
635+
expect(global.scrollToConfig).toEqual({
636+
top: 200,
637+
});
638+
});
609639
});

0 commit comments

Comments
 (0)