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] Add props to control cell mode #4210

Merged
merged 18 commits into from
May 2, 2022

Conversation

m4theushw
Copy link
Member

@m4theushw m4theushw commented Mar 16, 2022

Preview: https://deploy-preview-4210--material-ui-x.netlify.app/x/react-data-grid/editing/#controlled-mode

This PR adds props to start or stop the edit mode of a cell/row. The following props were added:

  • cellModesModel
  • rowModesModel
  • onCellModesModelChange
  • onRowModesModelChange

Different from other controllable states, here we don't react simply when the prop value changes. It has to change but also the mode needs to be different from the previous mode. These props work by storing the params that we should pass to startXXXEditMode and stopXXXEditMode when the mode is changed. For the implementation, I created a parallel state variable only to track the params. Now we have state.editRows, which tracks the props for the edit cells, and the state variable storing the params. I didn't merge both states because I don't want to recreate the model when, for exampe, only the value changes. To react to changes in the model, I moved everything from startXXXEditMode and stopXXXEditMode to helper methods updateStateToStartXXXEditMode and updateStateToStopXXXEditMode. The role of the public API methods now is to only update the model.

@mui-bot
Copy link

mui-bot commented Mar 16, 2022

These are the results for the performance tests:

Test case Unit Min Max Median Mean σ
Filter 100k rows ms 279.6 593 424 418.6 113.352
Sort 100k rows ms 507 1,001.6 638.6 781.9 181.5
Select 100k rows ms 174.3 198.2 187.7 186.78 7.834
Deselect 100k rows ms 134.8 339 185 202.5 73.515

Generated by 🚫 dangerJS against 46102f2

@flaviendelangle flaviendelangle added the on hold There is a blocker, we need to wait label Mar 17, 2022
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 18, 2022
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@m4theushw m4theushw self-assigned this Mar 23, 2022
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 29, 2022
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Mar 29, 2022
@m4theushw m4theushw force-pushed the control-cell-mode branch from 9e6016a to 1e331dd Compare April 6, 2022 20:30
@m4theushw m4theushw marked this pull request as ready for review April 6, 2022 20:46
@m4theushw m4theushw added feature: Editing Related to the data grid Editing feature and removed on hold There is a blocker, we need to wait labels Apr 6, 2022
```tsx
// Changes the mode of field=name from row with id=1 to "edit"
<DataGrid
cellModesModel={{ 1: { name: { mode: GridCellModes.Edit } } }}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look good because of PrismJS/prism#1548

Copy link
Member

@oliviertassinari oliviertassinari Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, I found the same Prisma issue.

Copy link
Member

@flaviendelangle flaviendelangle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What use case are we trying to cover with this control mode ?
I understand the technical part but I struggle to understand why people might need it.

@m4theushw
Copy link
Member Author

@flaviendelangle In the legacy API, there's the editRowsModel prop. Users from the Community version use it to control which cells are in edit mode. Despite its incompatibilities with some column types, for this task, it works. The props added here are to provide the same functionality in the new API: start and stop the edit mode declaratively.

@cherniavskii
Copy link
Member

Is it supposed to support multiple cells being edited at the same time?
I've noticed something like this:

Screen.Recording.2022-04-07.at.18.06.17.mov

@github-actions
Copy link

github-actions bot commented Apr 9, 2022

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 9, 2022
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 11, 2022
Copy link
Member

@joserodolfofreitas joserodolfofreitas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the feature designed to have multiple cells in edit mode?
If yes, perhaps we should have an example for that too.

Each cell and row has two modes: `edit` and `view`.
You can control the active mode using the props `cellModesModel` and `rowModesModel` (only works if `editMode="row"`).

The `cellModesModel` prop accepts an object where the key corresponds to the row ID containing the cell whose mode will be changed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we could exchange this whole paragraph, and simplify the description of the expected object by displaying the expected interface or a sample code.

Suggested change
The `cellModesModel` prop accepts an object where the key corresponds to the row ID containing the cell whose mode will be changed.
The `cellModesModel` prop accepts an object as the following:
```tsx
{
rowId: { fieldName: { mode: GridCellModes.Edit | GridCellModes.View }};
}
```
You can find a practical example below.

We can let the following example complement and confirm the usage of the feature's API.

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that this interface suggested is not TS and the code will not be properly formatted. The correct interface would be

export type GridCellModesModelProps =
  | ({ mode: GridCellModes.View } & Omit<GridStopCellEditModeParams, 'id' | 'field'>)
  | ({ mode: GridCellModes.Edit } & Omit<GridStartCellEditModeParams, 'id' | 'field'>);

export type GridCellModesModel = Record<GridRowId, Record<string, GridCellModesModelProps>>;

export type GridRowModesModelProps =
  | ({ mode: GridRowModes.View } & Omit<GridStopRowEditModeParams, 'id' | 'field'>)
  | ({ mode: GridRowModes.Edit } & Omit<GridStartRowEditModeParams, 'id' | 'field'>);

export type GridRowModesModel = Record<GridRowId, GridRowModesModelProps>;

but it doesn't make any sense.

Copy link
Member

@joserodolfofreitas joserodolfofreitas Apr 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. that interface is not really helpful in explaining it :).

Can we use jsx on this case to display the "template" object?

I think the paragraph describing the object is a bit convoluted. I tried simplifying it, but I don't think it's enough because the object itself is hard to explain while keeping the description accurate.

Suggested change
The `cellModesModel` prop accepts an object where the key corresponds to the row ID containing the cell whose mode will be changed.
The `cellModesModel` prop accepts an object containing the `mode` (and additional options) for a given column field, in a given row, as in the following example.
The options accepted are the same available in apiRef.current.startCellEditMode and apiRef.current.stopCellEditMode.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied the suggestion. We can only use JSX on JavaScript code. I think the example with the comments explain well the shape of the object.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 15, 2022
@m4theushw
Copy link
Member Author

Is the feature designed to have multiple cells in edit mode?

@joserodolfofreitas It's not the main purpose, since editMode="row" can be used for this. But yes, it allows multiple cells in edit mode, in the same way that using the API methods allowed too. The demo in https://deploy-preview-4210--material-ui-x.netlify.app/x/react-data-grid/editing/#controlled-mode shows this.

@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 22, 2022
@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Apr 22, 2022
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 29, 2022
Copy link
Member

@joserodolfofreitas joserodolfofreitas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to go 👌 (dx and doc-wise)

I've added links to referenced sections explaining the accepted options.

You can control the active mode using the props `cellModesModel` and `rowModesModel` (only works if `editMode="row"`).

The `cellModesModel` prop accepts an object containing the `mode` (and additional options) for a given column field, in a given row, as in the following example.
The options accepted are the same available in `apiRef.current.startCellEditMode` and `apiRef.current.stopCellEditMode`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The options accepted are the same available in `apiRef.current.startCellEditMode` and `apiRef.current.stopCellEditMode`.
The options accepted are the same available in `[apiRef.current.startCellEditMode](#start-editing)` and `[apiRef.current.stopCellEditMode](#stop-editing)`.

```

For row editing, the `rowModesModel` props work in a similar manner.
The options accepted are the same available in `apiRef.current.startRowEditMode` and `apiRef.current.stopRowEditMode`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The options accepted are the same available in `apiRef.current.startRowEditMode` and `apiRef.current.stopRowEditMode`.
The options accepted are the same available in `[apiRef.current.startRowEditMode](#start-editing)` and `[apiRef.current.stopRowEditMode](#stop-editing)`.

@m4theushw m4theushw merged commit c4012ca into mui:master May 2, 2022
@m4theushw m4theushw deleted the control-cell-mode branch May 2, 2022 20:15
alexfauquette pushed a commit to alexfauquette/mui-x that referenced this pull request Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: Editing Related to the data grid Editing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants