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

[DataGrid] Alternative actions column definition API #15041

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

cherniavskii
Copy link
Member

@cherniavskii cherniavskii commented Oct 21, 2024

Closes #12429

Before:

function Demo() {
  const deleteUser = (id) => {
    console.log('deleteUser', id);
  };
  const toggleAdmin = (id) => {
    console.log('toggleAdmin', id);
  };

  const columns: GridColDef[] = [
    {
      field: 'actions',
      type: 'actions',
      getActions: (params) => [
        <GridActionsCellItem
          icon={<DeleteIcon />}
          label="Delete"
          onClick={() => deleteUser(params.id)}
        />,
        <GridActionsCellItem
          icon={<SecurityIcon />}
          label="Toggle Admin"
          onClick={() => toggleAdmin(params.id)}
          showInMenu
        />,
      ],
    },
  ];

  return <DataGrid columns={columns} rows={[{ id: 1 }]} />;
}

After:

interface ActionHandlers {
  deleteUser: (id: GridRowId) => void;
  toggleAdmin: (id: GridRowId) => void;
}

const ActionHandlersContext = React.createContext<ActionHandlers>({
  deleteUser: () => {},
  toggleAdmin: () => {},
});

function ActionsCell(props: GridRenderCellParams) {
  const { deleteUser, toggleAdmin } = React.useContext(ActionHandlersContext);

  return (
    <GridActionsCell {...props}>
      <GridActionsCell.Item
        icon={<DeleteIcon />}
        label="Delete"
        onClick={() => deleteUser(props.id)}
      />
      <GridActionsCell.Item
        icon={<SecurityIcon />}
        label="Toggle Admin"
        onClick={() => toggleAdmin(props.id)}
        showInMenu
      />
    </GridActionsCell>
  );
}

const columns: GridColDef[] = [{
  field: 'actions',
  type: 'actions',
  renderCell: (params) => <ActionsCell {...params} />,
}];

function Demo() {
  const actionsHandlers = React.useMemo<ActionHandlers>(
    () => ({
      deleteUser: (id) => {
        console.log('deleteUser', id);
      },
      toggleAdmin: (id) => {
        console.log('toggleAdmin', id);
      },
    }),
    [],
  );

  return (
    <ActionHandlersContext.Provider value={actionsHandlers}>
      <DataGrid columns={columns} rows={[{ id: 1 }]} />
    </ActionHandlersContext.Provider>
  );
}

The biggest benefit is that it's easy to use React Context for passing virtually anything to actions – renderCell returns a single element, as opposed to getActions: https://deploy-preview-15041--material-ui-x.netlify.app/x/react-data-grid/editing/#full-featured-crud

TODO:

  • description with examples

@cherniavskii cherniavskii added component: data grid This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature labels Oct 21, 2024
@mui-bot
Copy link

mui-bot commented Oct 21, 2024

Deploy preview: https://deploy-preview-15041--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against 42d33ab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[data grid] Entering in edit mode, reload column configuration
3 participants