Skip to content

[Select] Implement pointer cancellation #45789

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
69 changes: 69 additions & 0 deletions packages/mui-material/src/Select/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,75 @@ describe('<Select />', () => {
skip: ['componentProp', 'componentsProp', 'themeVariants', 'themeStyleOverrides'],
}));

describe('Pointer Cancellation', () => {
beforeEach(function beforeEachCallback() {
// Run these tests only in browser because JSDOM doesn't have getBoundingClientRect() API
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
});

it('should close the menu when mouse is outside the select', () => {
render(
<Select value="" MenuProps={{ slotProps: { backdrop: { 'data-testid': 'backdrop' } } }}>
<MenuItem value="">none</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
</Select>,
);
const trigger = screen.getByRole('combobox');

// Open the menu
fireEvent.mouseDown(trigger);
expect(screen.getByRole('listbox')).not.to.equal(null);

// Simulate mouse up outside the menu. The mouseup target is the backdrop when the menu is opened.
fireEvent.mouseUp(screen.getByTestId('backdrop'), { clientX: 60, clientY: 10 });

// Menu should be closed now
expect(screen.queryByRole('listbox', { hidden: false })).to.equal(null);
});

it('should not close the menu when mouse is inside the trigger', () => {
render(
<Select value="">
<MenuItem value="">none</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
</Select>,
);
const trigger = screen.getByRole('combobox');

// Open the menu
fireEvent.mouseDown(trigger);
expect(screen.getByRole('listbox')).not.to.equal(null);

// Simulate mouse up inside the trigger
fireEvent.mouseUp(trigger, { clientX: 20, clientY: 20 });

// Menu should still be open
expect(screen.queryByRole('listbox', { hidden: false })).not.to.equal(null);
});

it('should not close the menu when releasing on a menu item', () => {
render(
<Select value="">
<MenuItem value="">none</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
</Select>,
);
const trigger = screen.getByRole('combobox');

// Open the menu
fireEvent.mouseDown(trigger);
const options = screen.getAllByRole('option');

// Simulate mouse up on a menu item
fireEvent.mouseUp(options[0]);

// Menu should still be open
expect(screen.getByRole('listbox')).not.to.equal(null);
});
});

describe('prop: inputProps', () => {
it('should be able to provide a custom classes property', () => {
render(
Expand Down
32 changes: 32 additions & 0 deletions packages/mui-material/src/Select/SelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {

const inputRef = React.useRef(null);
const displayRef = React.useRef(null);
const paperRef = React.useRef(null);
const [displayNode, setDisplayNode] = React.useState(null);
const { current: isOpenControlled } = React.useRef(openProp != null);
const [menuMinWidthState, setMenuMinWidthState] = React.useState();
Expand Down Expand Up @@ -233,6 +234,36 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
event.preventDefault();
displayRef.current.focus();

const doc = ownerDocument(event.currentTarget);

function handleMouseUp(mouseEvent) {
if (!displayRef.current) {
return;
}

// mouse is over the options/menuitem, don't close the menu
if (paperRef.current.contains(mouseEvent.target)) {
return;
}

const triggerElement = displayRef.current.getBoundingClientRect();

// mouse is inside the trigger, don't close the menu
if (
mouseEvent.clientX >= triggerElement.left &&
mouseEvent.clientX <= triggerElement.right &&
mouseEvent.clientY >= triggerElement.top &&
mouseEvent.clientY <= triggerElement.bottom
) {
return;
}
Comment on lines +249 to +259
Copy link
Member

Choose a reason for hiding this comment

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

You might wonder why this logic is needed — couldn’t we just check displayRef.current.contains(mouseEvent.target) above? It is because when the menu is open, the mouseup target is always the backdrop, whether the mouse is released inside or outside the trigger. The only exception is when the mouse is released inside the menu — then the target is the menu.

I think this happens because the backdrop overlays the trigger to block outside clicks.

You can see it here: https://stackblitz.com/edit/5lnkezz5


// close the menu
update(false, mouseEvent);
}

doc.addEventListener('mouseup', handleMouseUp, { once: true });

update(true, event);
};

Expand Down Expand Up @@ -571,6 +602,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
...listProps,
},
paper: {
ref: paperRef,
...paperProps,
style: {
minWidth: menuMinWidth,
Expand Down