Skip to content

feat: Allow scroll and context menu 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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
18 changes: 12 additions & 6 deletions packages/react-aria-components/src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ export function useTableOptions(): TableOptionsContextValue {
return useContext(TableOptionsContext)!;
}

const tableHeaderPropNames = new Set(['onScroll']);

export interface TableHeaderRenderProps {
/**
* Whether the table header is currently hovered with a mouse.
Expand All @@ -540,7 +542,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 @@ -587,7 +589,7 @@ export const TableHeader = /*#__PURE__*/ createBranchComponent(

return (
<THead
{...mergeProps(filterDOMProps(props as any), rowGroupProps, hoverProps)}
{...mergeProps(filterDOMProps(props as any, {propNames: tableHeaderPropNames}), rowGroupProps, hoverProps)}
{...renderProps}
ref={ref}
data-hovered={isHovered || undefined}>
Expand Down Expand Up @@ -902,7 +904,9 @@ export const ColumnResizer = forwardRef(function ColumnResizer(props: ColumnResi
);
});

export interface TableBodyRenderProps {
const tableBodyPropNames = new Set(['onScroll']);

export interface TableBodyRenderProps extends Pick<React.HTMLAttributes<HTMLTableSectionElement>, 'onScroll'> {
/**
* Whether the table body has no rows and should display its empty state.
* @selector [data-empty]
Expand Down Expand Up @@ -975,7 +979,7 @@ export const TableBody = /*#__PURE__*/ createBranchComponent('tablebody', <T ext
// call useLoadMore here and walk up the DOM to the nearest scrollable element to set scrollRef
return (
<TBody
{...mergeProps(filterDOMProps(props as any), rowGroupProps)}
{...mergeProps(filterDOMProps(props as any, {propNames: tableBodyPropNames}), rowGroupProps)}
{...renderProps}
ref={ref}
data-empty={collection.size === 0 || undefined}>
Expand All @@ -989,14 +993,16 @@ export const TableBody = /*#__PURE__*/ createBranchComponent('tablebody', <T ext
);
});

const rowPropNames = new Set(['onContextMenu']);

export interface RowRenderProps extends ItemRenderProps {
/** Whether the row's children have keyboard focus. */
isFocusVisibleWithin: boolean,
/** The unique id of the row. */
id?: Key
}

export interface RowProps<T> extends StyleRenderProps<RowRenderProps>, LinkDOMProps, HoverEvents {
export interface RowProps<T> extends StyleRenderProps<RowRenderProps>, LinkDOMProps, HoverEvents, Pick<React.HTMLAttributes<HTMLTableRowElement>, 'onContextMenu'> {
/** A list of columns used when dynamically rendering cells. */
columns?: Iterable<T>,
/** The cells within the row. Supports static items or a function for dynamic rendering. */
Expand Down Expand Up @@ -1110,7 +1116,7 @@ export const Row = /*#__PURE__*/ createBranchComponent(
</TR>
)}
<TR
{...mergeProps(filterDOMProps(props as any), rowProps, focusProps, hoverProps, draggableItem?.dragProps, focusWithinProps)}
{...mergeProps(filterDOMProps(props as any, {propNames: rowPropNames}), rowProps, focusProps, hoverProps, draggableItem?.dragProps, focusWithinProps)}
{...renderProps}
ref={ref}
data-disabled={states.isDisabled || undefined}
Expand Down
20 changes: 16 additions & 4 deletions packages/react-aria-components/test/Table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,16 @@ describe('Table', () => {
}
});

it('should support DOM props', () => {
it('should support DOM props', async () => {
const onContextMenu = jest.fn();
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'},
rowProps: {'data-testid': 'row'},
tableBodyProps: {'data-testid': 'table-body', onScroll: onScrollBody},
rowProps: {'data-testid': 'row', onContextMenu},
cellProps: {'data-testid': 'cell'}
});
let table = getByRole('grid');
Expand All @@ -312,6 +315,15 @@ 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);
const row = getAllByRole('row')[1];
fireEvent.contextMenu(row);
expect(onContextMenu).toBeCalledTimes(1);
});

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