Closed RyleySill93 closed 1 month ago
Hey @RyleySill93 these issues are to be handled separately:
Dialog
is being rendered within the cell and the keyDown
event bubbles up to the cell. To prevent this (and improve general performance) you would be better off to render the Dialog
component outside of the DataGrid
and handle changes through the API.export default function DataGridDemo() {
const [openId, setOpenId] = React.useState(null);
const CustomCell = (value) => (
<Button variant="contained" onClick={() => setOpenId(value.id)}>
button
</Button>
);
const columns: GridColDef<(typeof rows)[number]>[] = [
{
field: 'id',
headerName: 'ID',
width: 150,
editable: true,
renderCell: CustomCell,
},
];
return (
<Box sx={{ height: 400, width: '100%' }}>
<DataGrid
rows={rows}
columns={columns}
initialState={{
pagination: {
paginationModel: {
pageSize: 5,
},
},
}}
pageSizeOptions={[5]}
checkboxSelection
disableRowSelectionOnClick
/>
<Dialog onClose={() => setOpenId(null)} open={!!openId}>
<Box p={5}>
<TextField />
</Box>
</Dialog>
</Box>
);
}
@romgrk could you have a look at the second case! I feel I might be missing something here
The focus management logic for cells is here. The cell edit stop event runs on keydown
, and that logic moves the focus to the button in the next cell. The keypress
event that follows then clicks on the button because the key is Enter
.
I would suggest running event.preventDefault()
on the cellEditStop
event for those cells. It's a bit messy but cell editing wasn't meant to also have interactive display-mode cells (the buttons).
https://codesandbox.io/p/sandbox/goofy-edison-9rr7dm?file=%2Fsrc%2FDemo.tsx%3A3%2C37
The issue has been inactive for 7 days and has been automatically closed.
Steps to reproduce
Link to live example: https://codesandbox.io/s/interesting-bell-nhwlvx?file=/src/Demo.tsx
Grid cell stealing focus from dialog issue:
Buttons on click is fired when hitting "enter" into the cell it's rendered within:
Current behavior
No response
Expected behavior
No response
Context
For the stealing focus issue, I'm rendering a complex modal with many inputs so I don't want to spam
e.stopPropagation()
everywhere - ideally I'd be able to programmatically unfocus the cell when I open the modal so any events happening within the modal aren't forwarded to the cell.Your environment
``` Don't forget to mention which browser you used. Output from `npx @mui/envinfo` goes here. ```npx @mui/envinfo
Search keywords: DataGrid steal focus onClick enter button cell Order ID: 75803