Skip to content
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

<DatagridBody> creates the <RecordContext> instead of <DatagridRow> #9808

57 changes: 54 additions & 3 deletions docs/Upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,60 @@ Besides, the buttons passed as `bulkActionButtons` no longer receive any prop. I

The deprecated `<PaginationLimit>` component was removed.

### `<DatagridBody>` No Longer Provides `record` Prop To `<DatagridRow>`

The `<DatagridBody>` component no longer provides a `record` prop to its `<DatagridRow>` children. Instead, it provides a `recordContext` for each row:
djhi marked this conversation as resolved.
Show resolved Hide resolved

```diff
const DatagridBody: FC<DatagridBodyProps> = React.forwardRef(({/*...*/}, ref)
=> (
<TableBody>
- {data.map((record, rowIndex) =>
- cloneElement(
+ {data.map((record, rowIndex) => (
+ <RecordContextProvider
+ value={record}
+ key={record.id ?? `row${rowIndex}`}
+ >
+ {cloneElement(
row,
{
//...
id: record.id ?? `row${rowIndex}`,
- key: record.id ?? `row${rowIndex}`,
onToggleItem,
- record,
resource,
//...
},
children
- )
)}
+ </RecordContextProvider>
Copy link
Member

Choose a reason for hiding this comment

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

wrong diff: you should show how a custom DatagridRow no longer reads the record from its props, but from the Recordcontext.

+ ))}
</TableBody>
)
);
```

Also, `<DatagridRow>` no longer provides a `<RecordContextProvider>` to its children:
adguernier marked this conversation as resolved.
Show resolved Hide resolved

```diff
const DatagridRow: FC<DatagridRowProps> = React.forwardRef((props, ref) => {
return (
- <RecordContextProvider value={record}>
+ <>
<TableRow>
{/*...*/}
</TableRow>
- </RecordContextProvider>
+ </>
);
});
```

See the [`<Datagrid body/>`](./Datagrid.md#body) documentation to learn how to create your own row component.

## Show and Edit Pages

### Custom Edit or Show Actions No Longer Receive Any Props
Expand Down Expand Up @@ -1206,9 +1260,6 @@ The deprecated `<ThemeProvider theme>` prop was removed. Use the `ThemesContext.

The `data-generator-retail` package has been updated to provide types for all its records. In the process, we renamed the `commands` resource to `orders`. Accordingly, the `nb_commands` property of the `customers` resource has been renamed to `nb_orders` and the `command_id` property of the `invoices` and `reviews` resources has been renamed to `order_id`.




## Upgrading to v4

If you are on react-admin v3, follow the [Upgrading to v4](https://marmelab.com/react-admin/doc/4.16/Upgrade.html) guide before upgrading to v5.
56 changes: 30 additions & 26 deletions packages/ra-ui-materialui/src/list/datagrid/DatagridBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { cloneElement, memo, FC, ReactElement } from 'react';
import PropTypes from 'prop-types';
import { SxProps, TableBody, TableBodyProps } from '@mui/material';
import clsx from 'clsx';
import { Identifier, RaRecord } from 'ra-core';
import { Identifier, RaRecord, RecordContextProvider } from 'ra-core';

import { DatagridClasses } from './useDatagridStyles';
import DatagridRow, { PureDatagridRow, RowClickFunction } from './DatagridRow';
Expand Down Expand Up @@ -34,31 +34,35 @@ const DatagridBody: FC<DatagridBodyProps> = React.forwardRef(
className={clsx('datagrid-body', className, DatagridClasses.tbody)}
{...rest}
>
{data.map((record, rowIndex) =>
cloneElement(
row,
{
className: clsx(DatagridClasses.row, {
[DatagridClasses.rowEven]: rowIndex % 2 === 0,
[DatagridClasses.rowOdd]: rowIndex % 2 !== 0,
}),
expand,
hasBulkActions: hasBulkActions && !!selectedIds,
hover,
id: record.id ?? `row${rowIndex}`,
key: record.id ?? `row${rowIndex}`,
onToggleItem,
record,
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved
resource,
rowClick,
selectable: !isRowSelectable || isRowSelectable(record),
selected: selectedIds?.includes(record.id),
sx: rowSx?.(record, rowIndex),
style: rowStyle?.(record, rowIndex),
},
children
)
)}
{data.map((record, rowIndex) => (
<RecordContextProvider
value={record}
key={record.id ?? `row${rowIndex}`}
>
{cloneElement(
row,
{
className: clsx(DatagridClasses.row, {
[DatagridClasses.rowEven]: rowIndex % 2 === 0,
[DatagridClasses.rowOdd]: rowIndex % 2 !== 0,
}),
expand,
hasBulkActions: hasBulkActions && !!selectedIds,
hover,
id: record.id ?? `row${rowIndex}`,
onToggleItem,
resource,
rowClick,
selectable:
!isRowSelectable || isRowSelectable(record),
selected: selectedIds?.includes(record.id),
sx: rowSx?.(record, rowIndex),
style: rowStyle?.(record, rowIndex),
},
children
)}
</RecordContextProvider>
))}
</TableBody>
)
);
Expand Down