-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DataGrid] Add support for dialogs in menu actions (#11909)
- Loading branch information
1 parent
00b6b27
commit 3b2d066
Showing
6 changed files
with
293 additions
and
66 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
docs/data/data-grid/column-definition/ActionsWithModalGrid.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,92 @@ | ||
import * as React from 'react'; | ||
import { DataGrid, GridActionsCellItem } from '@mui/x-data-grid'; | ||
import DeleteIcon from '@mui/icons-material/Delete'; | ||
import { randomUserName } from '@mui/x-data-grid-generator'; | ||
import Dialog from '@mui/material/Dialog'; | ||
import DialogTitle from '@mui/material/DialogTitle'; | ||
import DialogContent from '@mui/material/DialogContent'; | ||
import DialogContentText from '@mui/material/DialogContentText'; | ||
import DialogActions from '@mui/material/DialogActions'; | ||
import Button from '@mui/material/Button'; | ||
|
||
const initialRows = [ | ||
{ id: 1, name: randomUserName() }, | ||
{ id: 2, name: randomUserName() }, | ||
{ id: 3, name: randomUserName() }, | ||
]; | ||
|
||
function DeleteUserActionItem({ deleteUser, ...props }) { | ||
const [open, setOpen] = React.useState(false); | ||
|
||
return ( | ||
<React.Fragment> | ||
<GridActionsCellItem {...props} onClick={() => setOpen(true)} /> | ||
<Dialog | ||
open={open} | ||
onClose={() => setOpen(false)} | ||
aria-labelledby="alert-dialog-title" | ||
aria-describedby="alert-dialog-description" | ||
> | ||
<DialogTitle id="alert-dialog-title">Delete this user?</DialogTitle> | ||
<DialogContent> | ||
<DialogContentText id="alert-dialog-description"> | ||
This action cannot be undone. | ||
</DialogContentText> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={() => setOpen(false)}>Cancel</Button> | ||
<Button | ||
onClick={() => { | ||
setOpen(false); | ||
deleteUser(); | ||
}} | ||
color="warning" | ||
autoFocus | ||
> | ||
Delete | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
export default function ActionsWithModalGrid() { | ||
const [rows, setRows] = React.useState(initialRows); | ||
|
||
const deleteUser = React.useCallback( | ||
(id) => () => { | ||
setTimeout(() => { | ||
setRows((prevRows) => prevRows.filter((row) => row.id !== id)); | ||
}); | ||
}, | ||
[], | ||
); | ||
|
||
const columns = React.useMemo( | ||
() => [ | ||
{ field: 'name', type: 'string' }, | ||
{ | ||
field: 'actions', | ||
type: 'actions', | ||
width: 80, | ||
getActions: (params) => [ | ||
<DeleteUserActionItem | ||
label="Delete" | ||
showInMenu | ||
icon={<DeleteIcon />} | ||
deleteUser={deleteUser(params.id)} | ||
closeMenuOnClick={false} | ||
/>, | ||
], | ||
}, | ||
], | ||
[deleteUser], | ||
); | ||
|
||
return ( | ||
<div style={{ height: 300, width: '100%' }}> | ||
<DataGrid columns={columns} rows={rows} /> | ||
</div> | ||
); | ||
} |
103 changes: 103 additions & 0 deletions
103
docs/data/data-grid/column-definition/ActionsWithModalGrid.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,103 @@ | ||
import * as React from 'react'; | ||
import { | ||
DataGrid, | ||
GridActionsCellItem, | ||
GridRowId, | ||
GridColDef, | ||
GridActionsCellItemProps, | ||
} from '@mui/x-data-grid'; | ||
import DeleteIcon from '@mui/icons-material/Delete'; | ||
import { randomUserName } from '@mui/x-data-grid-generator'; | ||
import Dialog from '@mui/material/Dialog'; | ||
import DialogTitle from '@mui/material/DialogTitle'; | ||
import DialogContent from '@mui/material/DialogContent'; | ||
import DialogContentText from '@mui/material/DialogContentText'; | ||
import DialogActions from '@mui/material/DialogActions'; | ||
import Button from '@mui/material/Button'; | ||
|
||
const initialRows = [ | ||
{ id: 1, name: randomUserName() }, | ||
{ id: 2, name: randomUserName() }, | ||
{ id: 3, name: randomUserName() }, | ||
]; | ||
|
||
function DeleteUserActionItem({ | ||
deleteUser, | ||
...props | ||
}: GridActionsCellItemProps & { deleteUser: () => void }) { | ||
const [open, setOpen] = React.useState(false); | ||
|
||
return ( | ||
<React.Fragment> | ||
<GridActionsCellItem {...props} onClick={() => setOpen(true)} /> | ||
<Dialog | ||
open={open} | ||
onClose={() => setOpen(false)} | ||
aria-labelledby="alert-dialog-title" | ||
aria-describedby="alert-dialog-description" | ||
> | ||
<DialogTitle id="alert-dialog-title">Delete this user?</DialogTitle> | ||
<DialogContent> | ||
<DialogContentText id="alert-dialog-description"> | ||
This action cannot be undone. | ||
</DialogContentText> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={() => setOpen(false)}>Cancel</Button> | ||
<Button | ||
onClick={() => { | ||
setOpen(false); | ||
deleteUser(); | ||
}} | ||
color="warning" | ||
autoFocus | ||
> | ||
Delete | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
type Row = (typeof initialRows)[number]; | ||
|
||
export default function ActionsWithModalGrid() { | ||
const [rows, setRows] = React.useState<Row[]>(initialRows); | ||
|
||
const deleteUser = React.useCallback( | ||
(id: GridRowId) => () => { | ||
setTimeout(() => { | ||
setRows((prevRows) => prevRows.filter((row) => row.id !== id)); | ||
}); | ||
}, | ||
[], | ||
); | ||
|
||
const columns = React.useMemo<GridColDef<Row>[]>( | ||
() => [ | ||
{ field: 'name', type: 'string' }, | ||
{ | ||
field: 'actions', | ||
type: 'actions', | ||
width: 80, | ||
getActions: (params) => [ | ||
<DeleteUserActionItem | ||
label="Delete" | ||
showInMenu | ||
icon={<DeleteIcon />} | ||
deleteUser={deleteUser(params.id)} | ||
closeMenuOnClick={false} | ||
/>, | ||
], | ||
}, | ||
], | ||
[deleteUser], | ||
); | ||
|
||
return ( | ||
<div style={{ height: 300, width: '100%' }}> | ||
<DataGrid columns={columns} rows={rows} /> | ||
</div> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
docs/data/data-grid/column-definition/ActionsWithModalGrid.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 @@ | ||
<DataGrid columns={columns} rows={rows} /> |
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
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
Oops, something went wrong.