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 cloneElement in list actions and bulk actions #9707

Merged
merged 3 commits into from
Mar 14, 2024
Merged

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Mar 11, 2024

Problem

Wenever we use cloneElement, developers find it hard to swap the component with their own as they don't know which props are injected.

Solution

Remove cloneElement and make child elements pull data from the context.

Applied to List components.

Note: there are still some cloneElement to support the deprecated <List filters={<Filter>...</Filter>} /> syntax (instead of <List filters={[...]} />). The discussion is open to determine if we should remove those.

<List hasCreate> Is No Longer Supported

To force a List view to display a Create button even though the corresponding resource doesn't have a create component, pass a custom actions component to the List component:

-import { List } from 'react-admin';
+import { List, ListActions } from 'react-admin';

const PostList = () => (
-   <List hasCreate>
+   <List actions={<ListActions hasCreate />}>
        ...
    </List>
);

Updates to bulkActionButtons Syntax

The bulkActionButtons prop has been moved from the <List> component to the <Datagrid> component.

const PostList = () => (
-   <List bulkActionButtons={<BulkActionButtons />}>
+   <List>
-      <Datagrid>
+      <Datagrid bulkActionButtons={<BulkActionButtons />}>
           ...
       </Datagrid>
   </List>
);

Besides, the buttons passed as bulkActionButtons no longer receive any prop. If you need the current filter values or the selected ids, you'll have to use the useListContext hook:

-const BulkResetViewsButton = ({ resource, selectedIds }) => {
+const BulkResetViewsButton = () => {
+   const { resource, selectedIds } = useListContext();
    const notify = useNotify();
    const unselectAll = useUnselectAll(resource);
    const [updateMany, { isPending }] = useUpdateMany();

    const handleClick = () => {
        updateMany(
            resource,
            { ids: selectedIds, data: { views: 0 } },
            {
                onSuccess: () => {
                    notify('Views reset');
                    unselectAll();
                },
                onError: () => notify('Views not reset', { type: 'error' }),
            }
        );
    }
    return (
        <Button
            label="Reset views"
            disabled={isPending}
            onClick={() => updateMany()}
        >
            <VisibilityOff />
        </Button>
    );
};

@adguernier adguernier merged commit a0c9553 into next Mar 14, 2024
12 checks passed
@adguernier adguernier deleted the list-no-clone branch March 14, 2024 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants