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] Drop autoFocus #2239

Merged
merged 16 commits into from
Aug 9, 2021
Merged

[DataGrid] Drop autoFocus #2239

merged 16 commits into from
Aug 9, 2021

Conversation

m4theushw
Copy link
Member

@m4theushw m4theushw commented Jul 30, 2021

We've been using autoFocus to set the focus when a cell enters the edit mode. The problem with this approach is that in #2098 multiple cells will be in edit mode at the same time, so using this prop on multiple elements is wrong. This PR modifies the current behavior to use the state to control which cell should receive focus.

Preview: https://deploy-preview-2239--material-ui-x.netlify.app/components/data-grid/demo/

@m4theushw m4theushw force-pushed the drop-autoFocus branch 2 times, most recently from cd9ed3e to 2f9b534 Compare July 30, 2021 22:23
@m4theushw m4theushw marked this pull request as ready for review July 30, 2021 22:39
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

Note that iOS is supposed to not support the autoFocus attribute: https://caniuse.com/?search=autofocus.

We have a regression on the keyboard navigation with the row checkbox selection feature. The last version that seems to behave correctly is v4.0.0-alpha.30: https://codesandbox.io/s/material-demo-forked-v9cqw. it's the input that is supposed to be focused when on the checkbox selection column, the arrow keys are supposed to work too. The correct behavior was introduced in #1421, but without any tests. I think an example of what we shouldn't do: bundle 5 fixes, add no tests.

packages/grid/x-grid/src/tests/rows.XGrid.test.tsx Outdated Show resolved Hide resolved
packages/grid/x-grid/src/tests/editRows.XGrid.test.tsx Outdated Show resolved Hide resolved
@flaviendelangle
Copy link
Member

Should we document the focus in this custom edit component demo ?

@m4theushw
Copy link
Member Author

Should we document the focus in this custom edit component demo ?

@flaviendelangle I prefer to keep it as it is. But in the row editing yes will we have to explain better how to create custom edit components.

@m4theushw
Copy link
Member Author

Note that iOS is supposed to not support the autoFocus attribute: https://caniuse.com/?search=autofocus.

We have a regression on the keyboard navigation with the row checkbox selection feature. The last version that seems to behave correctly is v4.0.0-alpha.30: https://codesandbox.io/s/material-demo-forked-v9cqw. it's the input that is supposed to be focused when on the checkbox selection column, the arrow keys are supposed to work too. The correct behavior was introduced in #1421, but without any tests. I think an example of what we shouldn't do: bundle 5 fixes, add no tests.

@oliviertassinari I fixed this in 20ef450. I had the same problem with the checkbox not checking, but in the row editing. I didn't think we would have this problem here, however we had. The solution is a bit hacky.

@m4theushw
Copy link
Member Author

@mui-org/x Could someone review this one? It would be nice to check if there're no regressions. It's the only thing blocking #2098, which is already done.

Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

Nothing blocking, only me explore any possible opportunities.

const handleDocumentClickAsync = (event) => {
// If the state is changed during the capture phase, onChange will not be fired
// By delaying the state update, there is time for other callbacks to be called first
// See https://github.com/facebook/react/issues/13424
Copy link
Member

@oliviertassinari oliviertassinari Aug 5, 2021

Choose a reason for hiding this comment

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

We rarely use event listeners in the capture phase in the core components. I'm always suspicious when we use them. So, I tried without, I couldn't help myself. The CI is green, the demos seems to work. What am I missing?

diff --git a/packages/grid/_modules_/grid/hooks/features/focus/useGridFocus.ts b/packages/grid/_modules_/grid/hooks/features/focus/useGridFocus.ts
index 22191bae..dbec238c 100644
--- a/packages/grid/_modules_/grid/hooks/features/focus/useGridFocus.ts
+++ b/packages/grid/_modules_/grid/hooks/features/focus/useGridFocus.ts
@@ -190,17 +190,9 @@ export const useGridFocus = (apiRef: GridApiRef, props: Pick<GridComponentProps,

   React.useEffect(() => {
     const doc = ownerDocument(apiRef.current.rootElementRef!.current as HTMLElement);
-    const handleDocumentClickAsync = (event) => {
-      // If the state is changed during the capture phase, onChange will not be fired
-      // By delaying the state update, there is time for other callbacks to be called first
-      // See https://github.com/facebook/react/issues/13424
-      clickTimeout.current = setTimeout(() => handleDocumentClick(event));
-    };
-    doc.addEventListener('click', handleDocumentClickAsync, true);
-
+    doc.addEventListener('click', handleDocumentClick);
     return () => {
-      clearTimeout(clickTimeout.current);
-      doc.removeEventListener('click', handleDocumentClickAsync, true);
+      doc.removeEventListener('click', handleDocumentClick);
     };
   }, [apiRef, handleDocumentClick]);

Off-topic. I personally find the current approach a bit backward. We set the focus state, then the state calls .focus(). Maybe the opposite might be easier to work with to me? We call .focus() where need. The grid listens to the focus/blur event to update its state if needed. The tricky part might be around virtualization and headers, I don't know.

Copy link
Member Author

Choose a reason for hiding this comment

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

We rarely use event listeners in the capture phase in the core components. I'm always suspicious when we use them. So, I tried without, I couldn't help myself. The CI is green, the demos seems to work. What am I missing?

True, we don't need to add the listener in the capture phase now. Probably I did this because the code from this PR is actually from #2098, which at the time it was opened #2179 was not merged yet, so stopping the propagation in a onCellClick would also break the focus.

Off-topic. I personally find the current approach a bit backward. We set the focus state, then the state calls .focus().

The tricky part for me is the Portal support. At the beginning we were doing this approach but we inverted.

return;
}

if (cellMode === 'edit' && isEditable) {
Copy link
Member

Choose a reason for hiding this comment

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

How can a cell be in edit mode while not editable?

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated. Answering your question, if apiRef.current.setCellMode is called in a cell that cannot be edited it will turn to edit mode. We need to add a check there.

packages/grid/_modules_/grid/components/cell/GridCell.tsx Outdated Show resolved Hide resolved
packages/grid/_modules_/grid/components/cell/GridCell.tsx Outdated Show resolved Hide resolved
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

Looks great

@m4theushw m4theushw merged commit c78db27 into mui:master Aug 9, 2021
@m4theushw m4theushw deleted the drop-autoFocus branch August 9, 2021 12:29
@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.

3 participants