Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/VirtualTable/BodyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
},
});

// https://github.com/ant-design/ant-design/issues/54734
Object.defineProperty(obj, 'scrollTop', {
get: () => listRef.current?.getScrollInfo().y || 0,

set: (value: number) => {
listRef.current?.scrollTo({
top: value,
});
},
});

return obj;
});

Expand Down
32 changes: 31 additions & 1 deletion tests/Virtual.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { resetWarned } from '@rc-component/util/lib/warning';
import React from 'react';
import { VirtualTable, type Reference, type VirtualTableProps } from '../src';

const identity = (value: any) => value;

global.scrollToConfig = null;
global.collectGetScrollInfoReturn = identity;

vi.mock('rc-virtual-list', async () => {
const RealVirtualList = ((await vi.importActual('rc-virtual-list')) as any).default;
Expand All @@ -20,6 +23,10 @@ vi.mock('rc-virtual-list', async () => {
global.scrollToConfig = config;
return myRef.current.scrollTo(config);
},
getScrollInfo: () => {
const originResult = myRef.current.getScrollInfo();
return global.collectGetScrollInfoReturn(originResult);
},
}));

return <RealVirtualList ref={myRef} {...props} data-scroll-width={props.scrollWidth} />;
Expand Down Expand Up @@ -59,7 +66,8 @@ describe('Table.Virtual', () => {
beforeEach(() => {
scrollLeftCalled = false;
setScrollLeft.mockReset();
global.scrollToConfig = null;
global.scrollToConfig = vi.fn(identity);
// global.collectGetScrollInfoReturn.mockReset();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure proper test isolation and prevent state from leaking between tests, global.collectGetScrollInfoReturn should be reset to its default identity function before each test runs. The commented-out mockReset() call is not suitable here, as this global variable is not a mock function.

Suggested change
// global.collectGetScrollInfoReturn.mockReset();
global.collectGetScrollInfoReturn = identity;

vi.useFakeTimers();
resetWarned();
});
Expand Down Expand Up @@ -606,4 +614,26 @@ describe('Table.Virtual', () => {
expect(container.querySelector('.rc-table')).toHaveClass('rc-table-fix-end-shadow-show');
});
});

/**
* In antd, we need to call the scrollTop method through ref to achieve scrolling.
* see: https://github.com/ant-design/ant-design/issues/54734
*/
it('should get and set scrollTop correctly', async () => {
const ref = React.createRef<any>();

global.collectGetScrollInfoReturn = (origin: any) => ({
...origin,
y: 50,
});

getTable({ internalRefs: { body: ref } });

expect(ref.current.scrollTop).toBe(50);

ref.current.scrollTop = 200;
expect(global.scrollToConfig).toEqual({
top: 200,
});
});
});