-
-
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
[DataGrid] Focus gets reset to GridActionCellItem when clicking TextField inside Dialog #3158
Comments
This is a side effect of #2862. Since there's a As a workaround, removing diff --git a/packages/grid/_modules_/grid/components/cell/GridActionsCellItem.tsx b/packages/grid/_modules_/grid/components/cell/GridActionsCellItem.tsx
index 98c39c04e..62e7ac0bb 100644
--- a/packages/grid/_modules_/grid/components/cell/GridActionsCellItem.tsx
+++ b/packages/grid/_modules_/grid/components/cell/GridActionsCellItem.tsx
@@ -13,26 +13,18 @@ export type GridActionsCellItemProps = {
);
const GridActionsCellItem = (props: GridActionsCellItemProps) => {
- const { label, icon, showInMenu, onClick, ...other } = props;
-
- const handleClick = (event: any) => {
- event.stopPropagation();
-
- if (onClick) {
- onClick(event);
- }
- };
+ const { label, icon, showInMenu, ...other } = props;
if (!showInMenu) {
return (
- <IconButton size="small" aria-label={label} {...(other as any)} onClick={handleClick}>
+ <IconButton size="small" aria-label={label} {...(other as any)}>
{React.cloneElement(icon!, { fontSize: 'small' })}
</IconButton>
);
}
return (
- <MenuItem {...(other as any)} onClick={onClick}>
+ <MenuItem {...(other as any)}>
{icon && <ListItemIcon>{icon}</ListItemIcon>}
{label}
</MenuItem>
diff --git a/packages/grid/_modules_/grid/hooks/features/selection/useGridSelection.ts b/packages/grid/_modules_/grid/hooks/features/selection/useGridSelection.ts
index d427678f1..2a7e235c9 100644
--- a/packages/grid/_modules_/grid/hooks/features/selection/useGridSelection.ts
+++ b/packages/grid/_modules_/grid/hooks/features/selection/useGridSelection.ts
@@ -64,6 +64,7 @@ export const useGridSelection = (
>,
): void => {
const logger = useGridLogger(apiRef, 'useGridSelection');
+ const lastClickedCell = React.useRef<GridCellParams | null>(null);
const propSelectionModel = React.useMemo(() => {
if (props.selectionModel == null) {
@@ -263,6 +264,14 @@ export const useGridSelection = (
return;
}
+ const { current: clickedCellParams } = lastClickedCell;
+ lastClickedCell.current = null;
+
+ // Ignore if before the row click a cell of `actions` type was clicked
+ if (clickedCellParams?.colDef.type === 'actions') {
+ return;
+ }
+
const hasCtrlKey = event.metaKey || event.ctrlKey;
if (event.shiftKey && (canHaveMultipleSelection || checkboxSelection)) {
@@ -292,6 +301,10 @@ export const useGridSelection = (
],
);
+ const handleCellClick = React.useCallback((params: GridCellParams) => {
+ lastClickedCell.current = params;
+ }, []);
+
const preventSelectionOnShift = React.useCallback(
(params: GridCellParams, event: React.MouseEvent) => {
if (canHaveMultipleSelection && event.shiftKey) {
@@ -328,6 +341,7 @@ export const useGridSelection = (
useGridApiEventHandler(apiRef, GridEvents.visibleRowsSet, removeOutdatedSelection);
useGridApiEventHandler(apiRef, GridEvents.rowClick, handleRowClick);
+ useGridApiEventHandler(apiRef, GridEvents.cellClick, handleCellClick);
useGridApiEventHandler(
apiRef,
GridEvents.rowSelectionCheckboxChange, |
Thanks for the quick response and explanation @m4theushw :) I would appreciate it if you guys could add the fix in one of the pull requests (like #2725) as I lack the time to properly contribute to this issue. 👍 Also simply removing |
@m4theushw your code only works if clicking on the whole cell disables the selection. |
I'm also facing this issue @v5.2.0 |
I'm also having this issue after I switched my list pages to use the DataGrid. My mui version is 5.1.1. I have no idea what to do about it, and it's pretty glaring to the user. You click in the text box within the dialog and it shifts focus back to the grid, then you click the text field again and it removes focus from grid back to text field and the focus stays in the text field. Note- if I put a button above the grid and trigger the dialog that way there's no problem, it only happens when they click the edit icon in the row and trigger the dialog that way. I'm using getActions: (params: GridRowParams) and rendering a GridActionCellItem with EditIcon like in the examples:
Is there a work around I can use in my code, even if it's not ideal? I don't want to check this in like this. Thank you! |
I've found a workaround today for this issue by simply adding Example: https://codesandbox.io/s/zealous-moon-equgn?file=/src/ArticleListDialog.js:1905-1941 <TextField
key={index}
id={field.id}
label={field.label}
type={field.type}
name={field.id}
required
value={values[field.id]}
margin="normal"
variant="standard"
fullWidth
InputLabelProps={{
shrink: true
}}
+ onClick={(e) => e.stopPropagation()}
onChange={(e) => {
e.stopPropagation();
handleInputValue(e);
}}
/> |
@marcopixel that worked for me, thank you so much!! Saved my day. if you later encounter any consequence of making that change please post it here, as will I. |
I'm also having trouble using forms in the Dialog due to this issue. |
Never mind, I upgraded to grid v5.6.1 and now the issue is fixed! Thanks !! |
I'm closing since the issue has already been fixed and released, probably by #3929. |
Duplicates
Latest version
Current behavior 😯
If you open a dialog via
<GridActionsCellItem onClick={}>
and then click on one of the<TextField>
components inside the dialog window, you will loose focus on the first click and after that it works as expected.2021-11-11.11-18-44.mp4
Expected behavior 🤔
It should focus on the
<TextField>
component instead to allow for text input.Steps to reproduce 🕹
Repro: https://codesandbox.io/s/modest-borg-6i0r2?file=/src/ArticleListDialog.js
Steps:
<TextField>
inside the dialog windowContext 🔦
I want to execute actions based on the items inside the DataGrid and i need to fill out additional data like date, email or domain name.
This seems to be related to #1295, as it also tries to focus the DataGridCell behind it (you can see the focus when closing the window).
Your environment 🌎
`npx @mui/envinfo`
Order ID 💳 (optional)
No response
The text was updated successfully, but these errors were encountered: