Skip to content

Create a codemod to replace Datagrid by DataTable #10762

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

Merged
merged 20 commits into from
Jun 5, 2025

Conversation

erwanMarmelab
Copy link
Contributor

@erwanMarmelab erwanMarmelab commented May 28, 2025

Problem

We have too many Datagrid that we want to transform to DataTable to do it manually

Solution

Create a codemod to do it automatically

How To Test

  1. Create a test.tsx and fill it with the example you want to transform. e.g.: the following one
import * as React from 'react';
import { List, Datagrid, TextField } from 'react-admin';

const PostList = () => (
    <List>
        <Datagrid>
            <TextField source="id" label="Post number" />
            <TextField source="title" />
            <TextField source="body" />
        </Datagrid>
    </List>
);

export default PostList;
  1. run the following command:
npx jscodeshift test.tsx \
    --extensions=ts,tsx \
    --parser=tsx \
    --transform=./packages/ra-core/codemods/replace-Datagrid-DataTable.ts

To Do

  • Write unit tests
  • Create the codemod
    • replace imports
    • replace the Datagrid component
    • wrap every fields with a <DataTable.Col source="xxx">
    • keep the Datagrid props
    • delete the optimized prop of the Datagrid component
    • replace the rowStyle prop of the Datagrid component by rowSx
    • replace the & .RaDatagrid-xxxx key of the sx prop of the Datagrid component by & .RaDataTable-xxxx
    • don't wrap a field if it is a TextField (with only source, label and/or empty)
    • don't wrap a field if it is a NumberField (with only source, label, empty, option and/or local)

Additional Checks

  • The PR targets master for a bugfix or a documentation fix, or next for a feature
  • The documentation is up to date

@erwanMarmelab erwanMarmelab added the WIP Work In Progress label May 28, 2025
@djhi djhi merged commit be5f8bc into master Jun 5, 2025
15 checks passed
@djhi djhi deleted the codemod-DataTable-Datagrid branch June 5, 2025 07:12
@djhi djhi modified the milestones: 5.8.5, 5.8.4 Jun 11, 2025
@dalen
Copy link

dalen commented Jun 21, 2025

It would be great if this handled DataGridConfigurable with omit -> hiddenColumns as well.

Also, sortable={false} -> disableSort seems to be missing.

@slax57
Copy link
Contributor

slax57 commented Jun 23, 2025

Hi @dalen
Thank you so much for this valuable feedback!
sortable={false} seems to be something that was forgotten indeed, so I'll create an issue for that right away!

DatagridConfigurable on the other hand is outside the scope of what we envisioned for the first version of the codemod, but it certainly would be a nice addition! -> Would you like to open a feature request to see if others would be interested?
And if you are willing to, you can also open a PR implementing that feature!
Thanks!

@slax57
Copy link
Contributor

slax57 commented Jun 23, 2025

@dalen on second look I realize that supporting sortable and sortByOrder was also out of scope. So both ideas are to be considered as new features to the codemod. (But they remain good ideas nevertheless!)
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Development

Successfully merging this pull request may close these issues.

4 participants