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] Don't listen in the capture phase #1156

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/grid/_modules_/grid/hooks/root/useEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ export function useEvents(gridRootRef: React.RefObject<HTMLDivElement>, apiRef:
const keyUpHandler = getHandler(GRID_KEYUP);
const gridRootElem = gridRootRef.current;

gridRootElem.addEventListener(GRID_CLICK, onClickHandler, { capture: true });
gridRootElem.addEventListener(GRID_MOUSE_HOVER, onHoverHandler, { capture: true });
gridRootElem.addEventListener(GRID_CLICK, onClickHandler);
gridRootElem.addEventListener(GRID_MOUSE_HOVER, onHoverHandler);
gridRootElem.addEventListener(GRID_FOCUS_OUT, onFocusOutHandler);

gridRootElem.addEventListener(GRID_KEYDOWN, keyDownHandler);
Expand All @@ -199,8 +199,8 @@ export function useEvents(gridRootRef: React.RefObject<HTMLDivElement>, apiRef:
return () => {
logger.debug('Clearing all events listeners');
api.publishEvent(GRID_UNMOUNT);
gridRootElem.removeEventListener(GRID_CLICK, onClickHandler, { capture: true });
gridRootElem.removeEventListener(GRID_MOUSE_HOVER, onHoverHandler, { capture: true });
gridRootElem.removeEventListener(GRID_CLICK, onClickHandler);
Copy link
Member

Choose a reason for hiding this comment

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

the event bubble up so if you attach them to the root then they won't fire when you click an element within it.

Copy link
Member Author

@oliviertassinari oliviertassinari Mar 3, 2021

Choose a reason for hiding this comment

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

I don't understand the reasoning. This argument would be valid if we were listening to an event that doesn't bubble, like the scroll event or focus. click/mouseover bubble.

Regarding the outcome, the demos in https://deploy-preview-1156--material-ui-x.netlify.app/components/data-grid/selection/#data-grid-selection seems to work correctly and the tests are green.

Copy link
Member

Choose a reason for hiding this comment

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

checkout #1158

Copy link
Member Author

@oliviertassinari oliviertassinari Mar 3, 2021

Choose a reason for hiding this comment

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

Before we move forward with #1158, could you report one concrete bug with the current changes? This could then be added as a regression test case for the future. The notion seems important enough so we sync with reality.

Copy link
Member Author

Choose a reason for hiding this comment

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

the event bubble up so if you attach them to the root then they won't fire when you click an element within it.

A counterexample that demonstrates this affirmation is false: https://codesandbox.io/s/nifty-bush-brmqw?file=/src/App.tsx

Copy link
Member

Choose a reason for hiding this comment

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

Yes I agree I got really confused here with the bubbling. Just need to check the params are still ok but I think they should be

Copy link
Member Author

@oliviertassinari oliviertassinari Mar 3, 2021

Choose a reason for hiding this comment

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

I personally think that #1158 would be even better than #1156. It removes the capture phase, which should be more intuitive for developers and integrate with the synthetic event system of React, perfect for them for using custom event logic 👌

Copy link
Member

Choose a reason for hiding this comment

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

The nice thing about #1158 is that it could use the helper to generate the params...
The bad thing is atm we generate a new function at every render so react.memo is not possible

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, what do you recommend as the best next step?

Copy link
Member

Choose a reason for hiding this comment

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

I will refactor a bit more and merge #1158 with the comments above

gridRootElem.removeEventListener(GRID_MOUSE_HOVER, onHoverHandler);
gridRootElem.removeEventListener(GRID_FOCUS_OUT, onFocusOutHandler);
gridRootElem.removeEventListener(GRID_KEYDOWN, keyDownHandler);
gridRootElem.removeEventListener(GRID_KEYUP, keyUpHandler);
Expand Down