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

[Select] Dispatch click event with same target of the mousedown event #25630

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
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
17 changes: 13 additions & 4 deletions packages/material-ui/src/Select/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ describe('<Select />', () => {
const trigger = getByRole('button');

fireEvent.mouseDown(trigger);
fireEvent.click(trigger);
Copy link
Member Author

@m4theushw m4theushw Apr 5, 2021

Choose a reason for hiding this comment

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

I had to add this click to every fireEvent.mouseDown so that the previous added event listener is removed.

Copy link
Member

Choose a reason for hiding this comment

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

This is one indicator that this change is problematic. What happens if there is no click registered on the trigger due to interactions with other elements?

Copy link
Member Author

Choose a reason for hiding this comment

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

There's no problem if the click occurs in other element, here it could even be in the document.body. I just need to issue it to clear the previous added listener or it will cause unexpected behavior in the next fireEvent.mouseDown below.


expect(handleBlur.callCount).to.equal(0);
expect(getByRole('listbox')).not.to.equal(null);
Expand Down Expand Up @@ -246,7 +247,9 @@ describe('<Select />', () => {
<MenuItem value="2" />
</Select>,
);
fireEvent.mouseDown(getByRole('button'));
const button = getByRole('button');
fireEvent.mouseDown(button);
fireEvent.click(button);
getAllByRole('option')[1].click();

expect(onChangeHandler.calledOnce).to.equal(true);
Expand All @@ -265,7 +268,9 @@ describe('<Select />', () => {
</Select>,
);

fireEvent.mouseDown(getByRole('button'));
const button = getByRole('button');
fireEvent.mouseDown(button);
fireEvent.click(button);
getAllByRole('option')[1].click();

expect(eventLog).to.deep.equal(['CHANGE_EVENT', 'CLOSE_EVENT']);
Expand Down Expand Up @@ -724,7 +729,9 @@ describe('<Select />', () => {
}
const { getByRole, queryByRole } = render(<ControlledWrapper />);

fireEvent.mouseDown(getByRole('button'));
const button = getByRole('button');
fireEvent.mouseDown(button);
fireEvent.click(button);
expect(getByRole('listbox')).not.to.equal(null);

act(() => {
Expand Down Expand Up @@ -921,7 +928,9 @@ describe('<Select />', () => {
});
const { getByRole, getAllByRole } = render(<ControlledSelectInput onChange={onChange} />);

fireEvent.mouseDown(getByRole('button'));
const button = getByRole('button');
fireEvent.mouseDown(button);
fireEvent.click(button);
const options = getAllByRole('option');
fireEvent.click(options[2]);

Expand Down
36 changes: 36 additions & 0 deletions packages/material-ui/src/Select/SelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
const [menuMinWidthState, setMenuMinWidthState] = React.useState();
const [openState, setOpenState] = React.useState(false);
const handleRef = useForkRef(ref, inputRefProp);
const handleClick = React.useRef(null);
const menuRef = React.useRef(null);
const disablePointerEvents = React.useRef(false);

const handleDisplayRef = React.useCallback((node) => {
displayRef.current = node;
Expand All @@ -83,6 +86,14 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
}
}, []);

const handleMenuRef = React.useCallback((node) => {
menuRef.current = node;

if (node && disablePointerEvents.current) {
node.style.pointerEvents = 'none';
}
}, []);

React.useImperativeHandle(
handleRef,
() => ({
Expand All @@ -95,6 +106,15 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
[value],
);

React.useEffect(() => {
const doc = ownerDocument(inputRef.current);
return () => {
if (handleClick.current) {
doc.removeEventListener('click', handleClick.current);
}
};
}, []);

React.useEffect(() => {
if (autoFocus) {
displayRef.current.focus();
Expand Down Expand Up @@ -141,6 +161,21 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
event.preventDefault();
displayRef.current.focus();

const doc = ownerDocument(inputRef.current);
if (handleClick.current) {
doc.removeEventListener('click', handleClick.current);
}

// Disable menu pointer events between `mousedown` and `click`.
disablePointerEvents.current = true;
handleClick.current = () => {
disablePointerEvents.current = false;
menuRef.current.style.pointerEvents = 'auto';
doc.removeEventListener('click', handleClick.current);
handleClick.current = null;
};
doc.addEventListener('click', handleClick.current);

update(true, event);
};

Expand Down Expand Up @@ -426,6 +461,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
})}
/>
<Menu
ref={handleMenuRef}
id={`menu-${name || ''}`}
anchorEl={displayNode}
open={open}
Expand Down
2 changes: 2 additions & 0 deletions packages/material-ui/test/integration/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('<Select> integration', () => {
// Let's open the select component
// in the browser user click also focuses
fireEvent.mouseDown(trigger);
fireEvent.click(trigger);

const options = getAllByRole('option');
expect(options[1]).toHaveFocus();
Expand All @@ -82,6 +83,7 @@ describe('<Select> integration', () => {
// Let's open the select component
// in the browser user click also focuses
fireEvent.mouseDown(trigger);
fireEvent.click(trigger);

const options = getAllByRole('option');
expect(options[1]).toHaveFocus();
Expand Down
21 changes: 21 additions & 0 deletions test/e2e/fixtures/ClickAwayListener/WithSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as React from 'react';
import Select from '@material-ui/core/Select';
import MenuItem from '@material-ui/core/MenuItem';
import ClickAwayListener from '@material-ui/core/ClickAwayListener';

export default function WithSelect() {
const [counter, setCounter] = React.useState(0);

return (
<React.Fragment>
<div id="onClickAway">{counter}</div>
<ClickAwayListener onClickAway={() => setCounter(counter + 1)}>
<Select value="">
<MenuItem value={10}>One</MenuItem>
<MenuItem value={20}>Two</MenuItem>
<MenuItem value={30}>Three</MenuItem>
</Select>
</ClickAwayListener>
</React.Fragment>
);
}
12 changes: 12 additions & 0 deletions test/e2e/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,16 @@ describe('e2e', () => {
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('ok');
});
});

describe('<ClickAwayListener />', () => {
it('should not call onClickAway when opening a select', async () => {
await renderFixture('ClickAwayListener/WithSelect');
expect(await page.textContent('#onClickAway')).to.equal('0');
await page.click('body');
expect(await page.textContent('#onClickAway')).to.equal('1');
await page.click('[role="button"]');
await page.click('"One"');
expect(await page.textContent('#onClickAway')).to.equal('1');
});
});
});