-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[DataGrid] Update cell editable state if editable prop is updated in the column definition
#20147
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/x-data-grid/src/tests/editing.DataGrid.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import * as React from 'react'; | ||
| import { createRenderer, screen } from '@mui/internal-test-utils'; | ||
| import { DataGrid, gridClasses, GridColDef, GridRowsProp } from '@mui/x-data-grid'; | ||
| import { getCell } from 'test/utils/helperFn'; | ||
|
|
||
| describe('<DataGrid /> - Cell editable state', () => { | ||
| const { render } = createRenderer(); | ||
|
|
||
| function expectCellEditable(rowIndex: number, colIndex: number) { | ||
| expect(getCell(rowIndex, colIndex)).to.have.class(gridClasses['cell--editable']); | ||
| } | ||
|
|
||
| function expectCellNotEditable(rowIndex: number, colIndex: number) { | ||
| expect(getCell(rowIndex, colIndex)).not.to.have.class(gridClasses['cell--editable']); | ||
| } | ||
|
|
||
| // based on https://github.com/mui/mui-x/issues/19732 | ||
| it('should update cell editable state when `isCellEditable` prop changes', async () => { | ||
| const rows: GridRowsProp = [ | ||
| { id: 1, name: 'A', age: 20 }, | ||
| { id: 2, name: 'B', age: 21 }, | ||
| ]; | ||
|
|
||
| const columns: GridColDef[] = [ | ||
| { field: 'name', editable: true, width: 150 }, | ||
| { field: 'age', type: 'number', editable: true, width: 150 }, | ||
| ]; | ||
|
|
||
| function GridWithState() { | ||
| const [editable, setEditable] = React.useState(true); | ||
| const [editMode, setEditMode] = React.useState<'row' | 'cell'>('cell'); | ||
|
|
||
| return ( | ||
| <div style={{ width: 400, height: 400 }}> | ||
| <button onClick={() => setEditable((s) => !s)}>toggle-editable</button> | ||
| <button onClick={() => setEditMode((s) => (s === 'row' ? 'cell' : 'row'))}> | ||
| toggle-mode | ||
| </button> | ||
| <DataGrid | ||
| rows={rows} | ||
| columns={columns} | ||
| editMode={editMode} | ||
| isCellEditable={(params) => params.row.age % 2 === 0 && editable && editMode === 'cell'} | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| const { user } = render(<GridWithState />); | ||
|
|
||
| // Initially: editMode = 'cell', editable = true → row 0 cells editable | ||
| expectCellEditable(0, 0); | ||
| expectCellEditable(0, 1); | ||
| expectCellNotEditable(1, 0); | ||
|
|
||
| // Toggle editable → should remove editable class immediately | ||
| await user.click(screen.getByRole('button', { name: 'toggle-editable' })); | ||
| expectCellNotEditable(0, 0); | ||
| expectCellNotEditable(0, 1); | ||
|
|
||
| // Toggle mode to 'cell' again (it was set to false only) by switching mode away and back | ||
| await user.click(screen.getByRole('button', { name: 'toggle-mode' })); | ||
| // Now editMode = 'row' → should not be editable regardless of row parity | ||
| expectCellNotEditable(0, 0); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'toggle-mode' })); | ||
| // Back to 'cell' but editable state is false → still not editable | ||
| expectCellNotEditable(0, 0); | ||
| }); | ||
|
|
||
| // based on https://github.com/mui/mui-x/issues/20143 | ||
| it('should update cell editable state when `colDef.editable` changes', async () => { | ||
| const rows: GridRowsProp = [{ id: 1, firstName: 'Jon', lastName: 'Snow', age: 14 }]; | ||
|
|
||
| function Component() { | ||
| const [isEditable, setIsEditable] = React.useState(true); | ||
| const cols: GridColDef[] = [ | ||
| { field: 'id', width: 100 }, | ||
| { field: 'firstName', width: 150, editable: isEditable }, | ||
| { field: 'lastName', width: 150, editable: true }, | ||
| ]; | ||
|
|
||
| return ( | ||
| <div style={{ width: 500, height: 400 }}> | ||
| <button onClick={() => setIsEditable((p) => !p)}>toggle-coldef</button> | ||
| <DataGrid rows={rows} columns={cols} isCellEditable={() => true} /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| const { user } = render(<Component />); | ||
|
|
||
| // firstName column is editable initially | ||
| expectCellEditable(0, 1); | ||
|
|
||
| // Toggle colDef.editable to false → class should be removed immediately | ||
| await user.click(screen.getByRole('button', { name: 'toggle-coldef' })); | ||
| expectCellNotEditable(0, 1); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, this was my fix initially, but then I thought I could as well use it 🫠
Thanks for fixing it and adding tests!