Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/VirtualTable/BodyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
listItemHeight,
getComponent,
onScroll: onTablePropScroll,
onVirtualScroll: onTablePropVirtualScroll,
} = useContext(StaticContext);

// =========================== Ref ============================
Expand Down Expand Up @@ -276,11 +277,12 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
component={wrapperComponent}
scrollWidth={scrollX as number}
direction={direction}
onVirtualScroll={({ x }) => {
onVirtualScroll={({ x, y }) => {
onScroll({
currentTarget: listRef.current?.nativeElement,
scrollLeft: x,
});
onTablePropVirtualScroll?.({ x, y });
}}
onScroll={onTablePropScroll}
extraRender={extraRender}
Expand Down
3 changes: 2 additions & 1 deletion src/VirtualTable/context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createContext } from '@rc-component/context';
import type { GetComponent, TableSticky } from '../interface';
import type { GetComponent, OnVirtualScroll, TableSticky } from '../interface';

export interface StaticContextProps {
scrollY: number;
listItemHeight: number;
sticky: boolean | TableSticky;
getComponent: GetComponent;
onScroll?: React.UIEventHandler<HTMLDivElement>;
onVirtualScroll?: OnVirtualScroll;
}

export const StaticContext = createContext<StaticContextProps>(null);
Expand Down
23 changes: 19 additions & 4 deletions src/VirtualTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { get as getValue, useEvent, warning } from '@rc-component/util';
import * as React from 'react';
import { INTERNAL_HOOKS } from '../constant';
import { makeImmutable } from '../context/TableContext';
import type { CustomizeScrollBody, GetComponent, Reference } from '../interface';
import type { CustomizeScrollBody, GetComponent, OnVirtualScroll, Reference } from '../interface';
import Table, { DEFAULT_PREFIX, type CompareProps, type TableProps } from '../Table';
import Grid from './BodyGrid';
import { StaticContext } from './context';
Expand All @@ -16,6 +16,12 @@ const renderBody: CustomizeScrollBody<any> = (rawData, props) => {
export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordType>, 'scroll'> {
listItemHeight?: number;
scroll: { x?: number; y?: number };
/**
* Triggered when virtual list offset changes.
* Prefer this over native `onScroll` when you need the virtual horizontal offset `x`
* (virtual list may use margin/transform instead of DOM `scrollLeft`).
*/
onVirtualScroll?: OnVirtualScroll;
}

const VirtualTable = <RecordType,>(
Expand All @@ -32,6 +38,7 @@ const VirtualTable = <RecordType,>(
listItemHeight,
components,
onScroll,
onVirtualScroll,
} = props;

let { x: scrollX, y: scrollY } = scroll || {};
Expand Down Expand Up @@ -60,11 +67,19 @@ const VirtualTable = <RecordType,>(

// Memo this
const onInternalScroll = useEvent(onScroll);
const onInternalVirtualScroll = useEvent(onVirtualScroll);

// ========================= Context ==========================
const context = React.useMemo(
() => ({ sticky, scrollY, listItemHeight, getComponent, onScroll: onInternalScroll }),
[sticky, scrollY, listItemHeight, getComponent, onInternalScroll],
() => ({
sticky,
scrollY,
listItemHeight,
getComponent,
onScroll: onInternalScroll,
onVirtualScroll: onInternalVirtualScroll,
}),
[sticky, scrollY, listItemHeight, getComponent, onInternalScroll, onInternalVirtualScroll],
);

// ========================== Render ==========================
Expand All @@ -89,7 +104,7 @@ const VirtualTable = <RecordType,>(
};

export type ForwardGenericVirtualTable = (<RecordType>(
props: TableProps<RecordType> & React.RefAttributes<Reference>,
props: VirtualTableProps<RecordType> & React.RefAttributes<Reference>,
) => React.ReactElement<any>) & { displayName?: string };

const RefVirtualTable = React.forwardRef(VirtualTable) as ForwardGenericVirtualTable;
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
FixedType,
GetComponentProps,
GetRowKey,
OnVirtualScroll,
Reference,
RenderedCell,
} from './interface';
Expand Down Expand Up @@ -43,6 +44,7 @@ export {
type FixedType,
type GetComponentProps,
type GetRowKey,
type OnVirtualScroll,
type RenderedCell,
};

Expand Down
3 changes: 3 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ export type OnCustomizeScroll = (info: {
scrollLeft?: number;
}) => void;

/** Virtual list scroll offset from `@rc-component/virtual-list` */
export type OnVirtualScroll = (info: { x: number; y: number }) => void;

export type CustomizeScrollBody<RecordType> = (
data: readonly RecordType[],
info: {
Expand Down
20 changes: 20 additions & 0 deletions tests/Virtual.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,26 @@ describe('Table.Virtual', () => {
expect(onScroll).toHaveBeenCalled();
});

it('onVirtualScroll event should work', async () => {
const onVirtualScroll = vi.fn();
const { container } = getTable({ onVirtualScroll });

await waitFakeTimer();

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

fireEvent.wheel(container.querySelector('.rc-table-tbody-virtual-holder')!, {
deltaX: 10,
});

expect(onVirtualScroll).toHaveBeenCalled();
expect(
onVirtualScroll.mock.calls.some(
([info]) => typeof info.x === 'number' && info.x > 0 && typeof info.y === 'number',
),
).toBe(true);
});

it('scrollable when empty', async () => {
const onScroll = vi.fn();
const { container } = getTable({ data: [], onScroll });
Expand Down