Skip to content

feat: Allow scroll events to be added on certain table components #8150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 5, 2025
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
6 changes: 4 additions & 2 deletions packages/react-aria-components/src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ export interface TableHeaderRenderProps {
isHovered: boolean
}

export interface TableHeaderProps<T> extends StyleRenderProps<TableHeaderRenderProps>, HoverEvents {
export interface TableHeaderProps<T> extends StyleRenderProps<TableHeaderRenderProps>, HoverEvents, Pick<React.HTMLAttributes<HTMLTableSectionElement>, 'onScroll'> {
/** A list of table columns. */
columns?: Iterable<T>,
/** A list of `Column(s)` or a function. If the latter, a list of columns must be provided using the `columns` prop. */
Expand Down Expand Up @@ -589,6 +589,7 @@ export const TableHeader = /*#__PURE__*/ createBranchComponent(
<THead
{...mergeProps(filterDOMProps(props as any), rowGroupProps, hoverProps)}
{...renderProps}
onScroll={props.onScroll}
ref={ref}
data-hovered={isHovered || undefined}>
{headerRows}
Expand Down Expand Up @@ -915,7 +916,7 @@ export interface TableBodyRenderProps {
isDropTarget: boolean
}

export interface TableBodyProps<T> extends Omit<CollectionProps<T>, 'disabledKeys'>, StyleRenderProps<TableBodyRenderProps> {
export interface TableBodyProps<T> extends Omit<CollectionProps<T>, 'disabledKeys'>, StyleRenderProps<TableBodyRenderProps>, Pick<React.HTMLAttributes<HTMLTableSectionElement>, 'onScroll'> {
/** Provides content to display when there are no rows in the table. */
renderEmptyState?: (props: TableBodyRenderProps) => ReactNode
}
Expand Down Expand Up @@ -979,6 +980,7 @@ export const TableBody = /*#__PURE__*/ createBranchComponent('tablebody', <T ext
<TBody
{...mergeProps(filterDOMProps(props as any), rowGroupProps)}
{...renderProps}
onScroll={props.onScroll}
ref={ref}
data-empty={isEmpty || undefined}>
{isDroppable && <RootDropIndicator />}
Expand Down
14 changes: 11 additions & 3 deletions packages/react-aria-components/test/Table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,14 @@ describe('Table', () => {
}
});

it('should support DOM props', () => {
it('should support DOM props', async () => {
const onScrollHeader = jest.fn();
const onScrollBody = jest.fn();
let {getByRole, getAllByRole} = renderTable({
tableProps: {'data-testid': 'table'},
tableHeaderProps: {'data-testid': 'table-header'},
tableHeaderProps: {'data-testid': 'table-header', onScroll: onScrollHeader},
columnProps: {'data-testid': 'column'},
tableBodyProps: {'data-testid': 'table-body'},
tableBodyProps: {'data-testid': 'table-body', onScroll: onScrollBody},
rowProps: {'data-testid': 'row'},
cellProps: {'data-testid': 'cell'}
});
Expand All @@ -331,6 +333,12 @@ describe('Table', () => {
for (let cell of getAllByRole('gridcell')) {
expect(cell).toHaveAttribute('data-testid', 'cell');
}

// trigger scrolls
fireEvent.scroll(rowGroups[0]);
fireEvent.scroll(rowGroups[1]);
expect(onScrollHeader).toBeCalledTimes(1);
expect(onScrollBody).toBeCalledTimes(1);
});

it('should render checkboxes for selection', async () => {
Expand Down