Skip to content
Draft
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
30 changes: 30 additions & 0 deletions packages/mui-material/src/Select/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ describe('<Select />', () => {
await sleep(450);
});

await user.pointer({ target: screen.getByRole('option', { name: 'Ten' }) });
await user.pointer({
keys: '[/MouseLeft]',
target: screen.getByRole('option', { name: 'Ten' }),
Expand All @@ -270,6 +271,33 @@ describe('<Select />', () => {
expect(screen.queryByRole('listbox', { hidden: false })).to.equal(null);
});

it('does not select an option when the opening mouseup lands on it after the drag delay without moving', async () => {
const onChange = spy();
const { user } = render(
<Select defaultValue={10} onChange={onChange}>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>,
);

const trigger = screen.getByRole('combobox');
await user.pointer({ keys: '[MouseLeft>]', target: trigger });

await act(async () => {
await sleep(450);
});

await user.pointer({
keys: '[/MouseLeft]',
target: screen.getByRole('option', { name: 'Twenty' }),
});

expect(trigger).to.have.text('Ten');
expect(onChange.callCount).to.equal(0);
expect(screen.queryByRole('listbox', { hidden: false })).not.to.equal(null);
});

it('selects an option when dragging from the trigger and releasing after the drag delay', async () => {
const onChange = spy();
const { user } = render(
Expand All @@ -286,6 +314,7 @@ describe('<Select />', () => {
await act(async () => {
await sleep(250);
});
await user.pointer({ target: screen.getByRole('option', { name: 'Twenty' }) });
await user.pointer({
keys: '[/MouseLeft]',
target: screen.getByRole('option', { name: 'Twenty' }),
Expand Down Expand Up @@ -335,6 +364,7 @@ describe('<Select />', () => {
await act(async () => {
await sleep(250);
});
await user.pointer({ target: screen.getByRole('option', { name: 'Twenty' }) });
await user.pointer({
keys: '[/MouseLeft]',
target: screen.getByRole('option', { name: 'Twenty' }),
Expand Down
14 changes: 14 additions & 0 deletions packages/mui-material/src/Select/SelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
const hasSelectedItemInListRef = React.useRef(false);
const openingMouseUpListenerCleanupRef = React.useRef(null);
const didPointerDownOnItemRef = React.useRef(false);
const didPointerMoveToItemRef = React.useRef(false);
const selectionRef = React.useRef({
allowSelectedMouseUp: false,
allowUnselectedMouseUp: false,
Expand Down Expand Up @@ -257,6 +258,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
const resetMouseUpSelection = React.useCallback(() => {
clearSelectionTimers();
didPointerDownOnItemRef.current = false;
didPointerMoveToItemRef.current = false;
selectionRef.current = {
allowSelectedMouseUp: false,
allowUnselectedMouseUp: false,
Expand Down Expand Up @@ -527,6 +529,10 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
return;
}

if (!didPointerMoveToItemRef.current) {
return;
}

const disallowSelectedMouseUp = !selectionRef.current.allowSelectedMouseUp && selected;
const disallowUnselectedMouseUp = !selectionRef.current.allowUnselectedMouseUp && !selected;

Expand Down Expand Up @@ -720,6 +726,14 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
didPointerDownOnItemRef.current = true;
child.props.onPointerDown?.(event);
},
onMouseMove: (event) => {
didPointerMoveToItemRef.current = true;
child.props.onMouseMove?.(event);
},
onPointerMove: (event) => {
didPointerMoveToItemRef.current = true;
child.props.onPointerMove?.(event);
},
onClick: handleItemClick(child),
onMouseUp: handleItemMouseUp(child, selected),
onKeyUp: (event) => {
Expand Down
Loading