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
29 changes: 26 additions & 3 deletions packages/ra-ui-materialui/src/list/datagrid/DatagridRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ const DatagridRow: React.ForwardRefExoticComponent<
getPathForRecord,
]
);
const isRowClickable = Boolean(rowClick ?? hasDetailView);

const handleKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLTableRowElement>) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
handleClick(event as any);
}
},
[handleClick]
);

// Accessible name for screen readers: without this, the row is announced
// as a bare "button"/"link" with no indication of what it does.
const rowAccessibleLabel = isRowClickable
? translate('ra.action.open', {
_: `Open ${resource} #${record.id}`,
recordRepresentation: `${resource} #${record.id}`,
})
: undefined;

return (
<>
Expand All @@ -173,12 +193,15 @@ const DatagridRow: React.ForwardRefExoticComponent<
className={clsx(className, {
[DatagridClasses.expandable]: expandable,
[DatagridClasses.selectable]: selectable,
[DatagridClasses.clickableRow]: rowClick ?? hasDetailView,
[DatagridClasses.clickableRow]: isRowClickable,
})}
key={id}
style={style}
key={record.id}
hover={hover}
onClick={handleClick}
onKeyDown={isRowClickable ? handleKeyDown : undefined}
role={isRowClickable ? 'button' : undefined}
tabIndex={isRowClickable ? 0 : undefined}
aria-label={rowAccessibleLabel}
{...rest}
>
{expand && (
Expand Down
17 changes: 15 additions & 2 deletions packages/ra-ui-materialui/src/list/datatable/DataTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ export const DataTableRow = React.memo(
getPathForRecord,
]
);
const isRowClickable = Boolean(rowClick ?? hasDetailView);

const handleKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLTableRowElement>) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
handleClick(event as any);
}
},
[handleClick]
);

return (
<>
Expand All @@ -167,12 +178,14 @@ export const DataTableRow = React.memo(
className={clsx(className, {
[DataTableClasses.expandable]: expandable,
[DataTableClasses.selectable]: selectable,
[DataTableClasses.clickableRow]:
rowClick ?? hasDetailView,
[DataTableClasses.clickableRow]: isRowClickable,
})}
key={record.id}
hover={hover}
onClick={handleClick}
onKeyDown={isRowClickable ? handleKeyDown : undefined}
role={isRowClickable ? 'button' : undefined}
tabIndex={isRowClickable ? 0 : undefined}
{...rest}
>
{expand && (
Expand Down