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

Remove props injection in <Datagrid expand> components #9719

Merged
merged 1 commit into from
Mar 14, 2024
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
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
@@ -1,6 +1,6 @@
import React, {
isValidElement,
cloneElement,

Check warning on line 3 in packages/ra-ui-materialui/src/list/datagrid/DatagridRow.tsx

View workflow job for this annotation

GitHub Actions / typecheck

'cloneElement' is defined but never used. Allowed unused vars must match /^_/u
createElement,
useState,
useEffect,
Expand Down Expand Up @@ -221,19 +221,9 @@
>
<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
Loading