Skip to content
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] Fix1295 backspace #1322

Closed
wants to merge 12 commits into from
Closed

Conversation

dtassone
Copy link
Member

fix #1295, backspace issue only, lead to more issue with portals

This PR is based on #1319 as they are all related...

cellRef.current!.focus();
}
});
}, [hasFocus]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix #1295, basically we trigger focus only when hasFocus changed

Copy link
Member

@oliviertassinari oliviertassinari Mar 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a symptom, not the root cause. From what I understand we should filter all the events that are coming from a portal in

https://github.com/mui-org/material-ui-x/blob/728bad3ef1f481a84a042d9bb44da970b56b5c87/packages/grid/_modules_/grid/components/GridRow.tsx#L35

diff --git a/packages/grid/_modules_/grid/components/GridRow.tsx b/packages/grid/_modules_/grid/components/GridRow.tsx
index 27135564..f1a3f0be 100644
--- a/packages/grid/_modules_/grid/components/GridRow.tsx
+++ b/packages/grid/_modules_/grid/components/GridRow.tsx
@@ -27,8 +27,14 @@ export const GridRow: React.FC<RowProps> = ({ selected, id, className, rowIndex,
   const rowHeight = useGridSelector(apiRef, gridDensityRowHeightSelector);

   const publish = React.useCallback(
-    (eventName: string) => (event: React.MouseEvent) =>
+    (eventName: string) => (event: React.MouseEvent) => {
+      // Ignore portal
+      if (!event.currentTarget.contains(event.target)) {
+        return;
+      }
+
       apiRef!.current.publishEvent(eventName, apiRef?.current.getRowParams(id), event),
+    },
     [apiRef, id],
   );

what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you would need to stopPropagation for all events that are fired from an element inside the cell. So it doesn't bubble to the rowClick... So if you have a renderCell with a div then it wouldn't propagate which seems wrong.
Or we could stop propagation for events coming from an input, but then if you click the dialog div it will bubble up to the row and so on
I think it should be up to users to stop propagation which gives the best flexibility.

There is a big react issue for propagation of events in portals and I suggest that we don't support portals in cells for now. Or at least that the propagation of events should be handled by users.
facebook/react#11387

I suggest to close this PR as the fix I proposed originally might not be ideal as if the cell gets rendered a 2nd time and the focus has not changed then it will loose focus, which is why there is no dependency in the useEffect

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the cell gets rendered a 2nd time and the focus has not changed then it will loose focus, which is why there is no dependency in the useEffect

Will it lose focus because the dom node is different?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it redraws ie if there is an updated value

Copy link
Member

@oliviertassinari oliviertassinari Mar 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would a redraw impact the focus? Maybe you are referring to what happens when we toggle between the read and edit mode. In which case, maybe we can only trigger a focus when the edit/read mode change?

@oliviertassinari
Copy link
Member

oliviertassinari commented Mar 31, 2021

This PR is based on #1319 as they are all related...

I would argue they are not related. They can and should be fixed in isolation, one stone at a time.

@dtassone dtassone closed this Mar 31, 2021
@oliviertassinari oliviertassinari added the component: data grid This is the name of the generic UI component, not the React module! label Jan 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataGrid] Loose focus when opening a textbox portal inside a cell
2 participants