-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[docs] Add recipe for single-click editing #8365
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6c1fbc9
[docs] Add recipe for single-click editing
m4theushw 4400d96
Merge branch 'master' into single-click-edit-recipe
m4theushw 06b5d3e
Ignore clicks from Portals
m4theushw c0b125f
Merge branch 'master' into single-click-edit-recipe
m4theushw 64468b8
Merge branch 'master' into single-click-edit-recipe
m4theushw 122ab54
Minor improvements
m4theushw c29c34c
Fix links
m4theushw 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 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
122 changes: 122 additions & 0 deletions
122
docs/data/data-grid/recipes-editing/SingleClickEditing.js
This file contains 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,122 @@ | ||
import * as React from 'react'; | ||
import { DataGrid, GridCellModes } from '@mui/x-data-grid'; | ||
import { | ||
randomCreatedDate, | ||
randomTraderName, | ||
randomUpdatedDate, | ||
} from '@mui/x-data-grid-generator'; | ||
|
||
const columns = [ | ||
{ field: 'name', headerName: 'Name', width: 180, editable: true }, | ||
{ field: 'age', headerName: 'Age', type: 'number', editable: true }, | ||
{ | ||
field: 'dateCreated', | ||
headerName: 'Date Created', | ||
type: 'date', | ||
width: 180, | ||
editable: true, | ||
}, | ||
{ | ||
field: 'lastLogin', | ||
headerName: 'Last Login', | ||
type: 'dateTime', | ||
width: 220, | ||
editable: true, | ||
}, | ||
]; | ||
|
||
const rows = [ | ||
{ | ||
id: 1, | ||
name: randomTraderName(), | ||
age: 25, | ||
dateCreated: randomCreatedDate(), | ||
lastLogin: randomUpdatedDate(), | ||
}, | ||
{ | ||
id: 2, | ||
name: randomTraderName(), | ||
age: 36, | ||
dateCreated: randomCreatedDate(), | ||
lastLogin: randomUpdatedDate(), | ||
}, | ||
{ | ||
id: 3, | ||
name: randomTraderName(), | ||
age: 19, | ||
dateCreated: randomCreatedDate(), | ||
lastLogin: randomUpdatedDate(), | ||
}, | ||
{ | ||
id: 4, | ||
name: randomTraderName(), | ||
age: 28, | ||
dateCreated: randomCreatedDate(), | ||
lastLogin: randomUpdatedDate(), | ||
}, | ||
{ | ||
id: 5, | ||
name: randomTraderName(), | ||
age: 23, | ||
dateCreated: randomCreatedDate(), | ||
lastLogin: randomUpdatedDate(), | ||
}, | ||
]; | ||
|
||
export default function SingleClickEditing() { | ||
const [cellModesModel, setCellModesModel] = React.useState({}); | ||
|
||
const handleCellClick = React.useCallback((params, event) => { | ||
if (!params.isEditable) { | ||
return; | ||
} | ||
|
||
// Ignore portal | ||
if (!event.currentTarget.contains(event.target)) { | ||
return; | ||
} | ||
|
||
setCellModesModel((prevModel) => { | ||
return { | ||
// Revert the mode of the other cells from other rows | ||
...Object.keys(prevModel).reduce( | ||
(acc, id) => ({ | ||
...acc, | ||
[id]: Object.keys(prevModel[id]).reduce( | ||
(acc2, field) => ({ | ||
...acc2, | ||
[field]: { mode: GridCellModes.View }, | ||
}), | ||
{}, | ||
), | ||
}), | ||
{}, | ||
), | ||
[params.id]: { | ||
// Revert the mode of other cells in the same row | ||
...Object.keys(prevModel[params.id] || {}).reduce( | ||
(acc, field) => ({ ...acc, [field]: { mode: GridCellModes.View } }), | ||
{}, | ||
), | ||
[params.field]: { mode: GridCellModes.Edit }, | ||
}, | ||
}; | ||
}); | ||
}, []); | ||
|
||
const handleCellModesModelChange = React.useCallback((newModel) => { | ||
setCellModesModel(newModel); | ||
}, []); | ||
|
||
return ( | ||
<div style={{ height: 300, width: '100%' }}> | ||
<DataGrid | ||
rows={rows} | ||
columns={columns} | ||
cellModesModel={cellModesModel} | ||
onCellModesModelChange={handleCellModesModelChange} | ||
onCellClick={handleCellClick} | ||
/> | ||
</div> | ||
); | ||
} |
135 changes: 135 additions & 0 deletions
135
docs/data/data-grid/recipes-editing/SingleClickEditing.tsx
This file contains 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,135 @@ | ||
import * as React from 'react'; | ||
import { | ||
DataGrid, | ||
GridCellModes, | ||
GridCellModesModel, | ||
GridCellParams, | ||
GridRowsProp, | ||
GridColDef, | ||
} from '@mui/x-data-grid'; | ||
import { | ||
randomCreatedDate, | ||
randomTraderName, | ||
randomUpdatedDate, | ||
} from '@mui/x-data-grid-generator'; | ||
|
||
const columns: GridColDef[] = [ | ||
{ field: 'name', headerName: 'Name', width: 180, editable: true }, | ||
{ field: 'age', headerName: 'Age', type: 'number', editable: true }, | ||
{ | ||
field: 'dateCreated', | ||
headerName: 'Date Created', | ||
type: 'date', | ||
width: 180, | ||
editable: true, | ||
}, | ||
{ | ||
field: 'lastLogin', | ||
headerName: 'Last Login', | ||
type: 'dateTime', | ||
width: 220, | ||
editable: true, | ||
}, | ||
]; | ||
|
||
const rows: GridRowsProp = [ | ||
{ | ||
id: 1, | ||
name: randomTraderName(), | ||
age: 25, | ||
dateCreated: randomCreatedDate(), | ||
lastLogin: randomUpdatedDate(), | ||
}, | ||
{ | ||
id: 2, | ||
name: randomTraderName(), | ||
age: 36, | ||
dateCreated: randomCreatedDate(), | ||
lastLogin: randomUpdatedDate(), | ||
}, | ||
{ | ||
id: 3, | ||
name: randomTraderName(), | ||
age: 19, | ||
dateCreated: randomCreatedDate(), | ||
lastLogin: randomUpdatedDate(), | ||
}, | ||
{ | ||
id: 4, | ||
name: randomTraderName(), | ||
age: 28, | ||
dateCreated: randomCreatedDate(), | ||
lastLogin: randomUpdatedDate(), | ||
}, | ||
{ | ||
id: 5, | ||
name: randomTraderName(), | ||
age: 23, | ||
dateCreated: randomCreatedDate(), | ||
lastLogin: randomUpdatedDate(), | ||
}, | ||
]; | ||
|
||
export default function SingleClickEditing() { | ||
const [cellModesModel, setCellModesModel] = React.useState<GridCellModesModel>({}); | ||
|
||
const handleCellClick = React.useCallback( | ||
(params: GridCellParams, event: React.MouseEvent) => { | ||
if (!params.isEditable) { | ||
return; | ||
} | ||
|
||
// Ignore portal | ||
if (!event.currentTarget.contains(event.target as Element)) { | ||
return; | ||
} | ||
|
||
setCellModesModel((prevModel) => { | ||
return { | ||
// Revert the mode of the other cells from other rows | ||
...Object.keys(prevModel).reduce( | ||
(acc, id) => ({ | ||
...acc, | ||
[id]: Object.keys(prevModel[id]).reduce( | ||
(acc2, field) => ({ | ||
...acc2, | ||
[field]: { mode: GridCellModes.View }, | ||
}), | ||
{}, | ||
), | ||
}), | ||
{}, | ||
), | ||
[params.id]: { | ||
// Revert the mode of other cells in the same row | ||
...Object.keys(prevModel[params.id] || {}).reduce( | ||
(acc, field) => ({ ...acc, [field]: { mode: GridCellModes.View } }), | ||
{}, | ||
), | ||
[params.field]: { mode: GridCellModes.Edit }, | ||
}, | ||
}; | ||
}); | ||
}, | ||
[], | ||
); | ||
|
||
const handleCellModesModelChange = React.useCallback( | ||
(newModel: GridCellModesModel) => { | ||
setCellModesModel(newModel); | ||
}, | ||
[], | ||
); | ||
|
||
return ( | ||
<div style={{ height: 300, width: '100%' }}> | ||
<DataGrid | ||
rows={rows} | ||
columns={columns} | ||
cellModesModel={cellModesModel} | ||
onCellModesModelChange={handleCellModesModelChange} | ||
onCellClick={handleCellClick} | ||
/> | ||
</div> | ||
); | ||
} |
7 changes: 7 additions & 0 deletions
7
docs/data/data-grid/recipes-editing/SingleClickEditing.tsx.preview
This file contains 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,7 @@ | ||
<DataGrid | ||
rows={rows} | ||
columns={columns} | ||
cellModesModel={cellModesModel} | ||
onCellModesModelChange={handleCellModesModelChange} | ||
onCellClick={handleCellClick} | ||
/> |
This file contains 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
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.
Wouldn't excluding the rows/cells from the
cellModesModel
exit the editing mode?In https://deploy-preview-8365--material-ui-x.netlify.app/x/react-data-grid/editing/#controlled-mode it says:
But I have tried removing them from the model and didn't notice any difference:
https://codesandbox.io/s/festive-raman-qz19hf?file=/demo.tsx
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.
Yes, only removing the row is enough if only a cell is allowed to be editable at a time. If multiple cells can be edited at the same time, then we can't remove the row. This demo implements the most broader scenario, allowing further customization.