Skip to content

Commit

Permalink
fix: should not reset virtual scrollLeft when data changed (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
linxianxi committed Jun 18, 2024
1 parent 80ed65b commit c2b48ac
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,9 @@ function Table<RecordType extends DefaultRecordType>(
const triggerOnScroll = () => {
if (horizonScroll && scrollBodyRef.current) {
onInternalScroll({
currentTarget: getDOM(scrollBodyRef.current),
} as React.UIEvent<HTMLDivElement>);
currentTarget: getDOM(scrollBodyRef.current) as HTMLElement,
scrollLeft: scrollBodyRef.current?.scrollLeft,
});
} else {
setPingedLeft(false);
setPingedRight(false);
Expand Down
34 changes: 33 additions & 1 deletion tests/Virtual.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ vi.mock('rc-virtual-list', async () => {
describe('Table.Virtual', () => {
let scrollLeftCalled = false;

const setScrollLeft = vi.fn();

beforeAll(() => {
spyElementPrototypes(HTMLElement, {
getBoundingClientRect: () => ({
Expand All @@ -43,7 +45,7 @@ describe('Table.Virtual', () => {
scrollLeftCalled = true;
return 100;
},
set: () => {},
set: setScrollLeft as any,
},
clientWidth: {
get: () => 80,
Expand All @@ -56,6 +58,7 @@ describe('Table.Virtual', () => {

beforeEach(() => {
scrollLeftCalled = false;
setScrollLeft.mockReset();
global.scrollToConfig = null;
vi.useFakeTimers();
resetWarned();
Expand Down Expand Up @@ -210,6 +213,35 @@ describe('Table.Virtual', () => {
expect(scrollLeftCalled).toBeTruthy();
});

it('should not reset scroll when data changed', async () => {
const { container, rerender } = getTable();

resize(container.querySelector('.rc-table')!);

rerender(
<VirtualTable
columns={[
{
dataIndex: 'name',
},
{
dataIndex: 'age',
},
{
dataIndex: 'address',
},
]}
rowKey="name"
scroll={{ x: 100, y: 100 }}
data={[{}]}
/>,
);
vi.runAllTimers();

// mock scrollLeft is 100, but virtual offsetX is 0
expect(setScrollLeft).toHaveBeenCalledWith(undefined, 0);
});

it('should follow correct width', () => {
const { container } = getTable({
columns: [
Expand Down
Loading

0 comments on commit c2b48ac

Please sign in to comment.