Skip to content

Commit

Permalink
Merge pull request #9719 from marmelab/Do-not-clone-datagrid-expand-p…
Browse files Browse the repository at this point in the history
…anel

Remove props injection in `<Datagrid expand>` components
  • Loading branch information
adguernier authored Mar 14, 2024
2 parents 1509bed + 3badd55 commit b102283
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
16 changes: 16 additions & 0 deletions docs/Upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,22 @@ We've changed the implementation of `<SimpleFormIterator>`, the companion child
</ArrayInput>
```

## `<Datagrid expand>` Components No Longer Receive Any Props

An undocumented features allowed datagrid expand panels to read the current resource, record, and id from their props. This is no longer the case in v5, as expand panels are now rendered without props by `<Datagrid>`.

If you used these props in your expand components, you'll have to use the `useRecordContext` hook instead:

```diff
-const PostExpandPanel = ({ record, resource, id }) => {
+const PostExpandPanel = () => {
+ const record = useRecordContext();
+ const resource = useResourceContext();
+ const id = record?.id;
// ...
}
```

## `<FormDataConsumer>` no longer passes a `getSource` function

When using `<FormDataConsumer>` inside an `<ArrayInput>`, the child function no longer receives a `getSource` callback. We've made all Input components able to work seamlessly inside an `<ArrayInput>`, so it's no longer necessary to transform their source with `getSource`:
Expand Down
8 changes: 5 additions & 3 deletions examples/simple/src/posts/PostList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
TextField,
TextInput,
TopToolbar,
useRecordContext,
useTranslate,
} from 'react-admin'; // eslint-disable-line import/no-unresolved

Expand Down Expand Up @@ -147,9 +148,10 @@ const rowClick = (_id, _resource, record) => {
return 'show';
};

const PostPanel = ({ record }) => (
<div dangerouslySetInnerHTML={{ __html: record.body }} />
);
const PostPanel = () => {
const record = useRecordContext();
return <div dangerouslySetInnerHTML={{ __html: record?.body }} />;
};

const tagSort = { field: 'name.en', order: 'ASC' } as const;

Expand Down
36 changes: 23 additions & 13 deletions examples/simple/src/users/UserEditEmbedded.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
/* eslint react/jsx-key: off */
import * as React from 'react';
import PropTypes from 'prop-types';
import { Edit, Identifier, SimpleForm, TextInput, required } from 'react-admin';
import {
Edit,
SimpleForm,
TextInput,
required,
useRecordContext,
} from 'react-admin';

const UserEditEmbedded = ({ id }: { id?: Identifier }) => (
/* Passing " " as title disables the custom title */
<Edit title=" " id={id}>
<SimpleForm defaultValues={{ role: 'user' }}>
<TextInput
source="name"
defaultValue="slim shady"
validate={required()}
/>
</SimpleForm>
</Edit>
);
const UserEditEmbedded = () => {
const record = useRecordContext();
if (!record) return null;
return (
/* Passing " " as title disables the custom title */
<Edit title=" " id={record.id} actions={false}>
<SimpleForm defaultValues={{ role: 'user' }}>
<TextInput
source="name"
defaultValue="slim shady"
validate={required()}
/>
</SimpleForm>
</Edit>
);
};

UserEditEmbedded.propTypes = {
record: PropTypes.object,
Expand Down
14 changes: 2 additions & 12 deletions packages/ra-ui-materialui/src/list/datagrid/DatagridRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,9 @@ const DatagridRow: FC<DatagridRowProps> = React.forwardRef((props, ref) => {
>
<TableCell colSpan={nbColumns}>
{isElement(expand)
? cloneElement(expand as React.ReactElement<any>, {
// @ts-ignore
record,
resource,
id: String(id),
})
? expand
: createElement(
expand as React.FunctionComponent<any>,
{
record,
resource,
id: String(id),
}
expand as React.FunctionComponent<any>
)}
</TableCell>
</TableRow>
Expand Down

0 comments on commit b102283

Please sign in to comment.