Skip to content

Commit

Permalink
[Autocomplete] Fix listbox opened unexpectedly when component is `dis…
Browse files Browse the repository at this point in the history
…abled` (#38611)
  • Loading branch information
mj12albert authored Aug 29, 2023
1 parent b14e8b8 commit a4d9cb1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/mui-base/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ export function useAutocomplete(props) {
};

const handleInputMouseDown = (event) => {
if (inputValue === '' || !open) {
if (!disabledProp && (inputValue === '' || !open)) {
handlePopupIndicator(event);
}
};
Expand Down
65 changes: 65 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
strictModeDoubleLoggingSuppressed,
} from 'test/utils';
import { spy } from 'sinon';
import userEvent from '@testing-library/user-event';
import Box from '@mui/system/Box';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import TextField from '@mui/material/TextField';
Expand Down Expand Up @@ -1385,6 +1386,70 @@ describe('<Autocomplete />', () => {
expect(queryByTitle('Open').disabled).to.equal(true);
});

it('clicks should not toggle the listbox open state when disabled', () => {
const { getByTestId, queryByRole } = render(
<Autocomplete
disabled
options={['one', 'two', 'three']}
renderInput={(params) => (
<TextField
{...params}
InputProps={{
...params.InputProps,
'data-testid': 'test-input-root',
}}
/>
)}
/>,
);
const textbox = queryByRole('combobox');
const listbox = queryByRole('listbox', { hidden: true });

expect(textbox).to.have.attribute('aria-expanded', 'false');
expect(listbox).to.equal(null);

const inputBase = getByTestId('test-input-root');
fireEvent.click(inputBase);

expect(textbox).to.have.attribute('aria-expanded', 'false');
expect(listbox).to.equal(null);
});

it('mouseup should not toggle the listbox open state when disabled', async () => {
const { container, queryByRole } = render(
<Autocomplete
disabled
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} />}
/>,
);

const textbox = queryByRole('combobox');
const listbox = queryByRole('listbox', { hidden: true });

expect(textbox).to.have.attribute('aria-expanded', 'false');
expect(listbox).to.equal(null);

// userEvent will fail at releasing MouseLeft if we target the
// <button> since it has "pointer-events: none"
const popupIndicator = container.querySelector(`.${classes.endAdornment}`);

// TODO v6: refactor using userEvent.setup() which doesn't work until we drop
// iOS Safari 12.x support, see: https://github.com/mui/material-ui/pull/38325
await userEvent.pointer([
// this sequence does not work with fireEvent
// 1. point the cursor somewhere in the textbox and hold down MouseLeft
{ keys: '[MouseLeft>]', target: textbox },
// 2. move the cursor over the popupIndicator
{ pointerName: 'mouse', target: popupIndicator },
// 3. release MouseLeft
{ keys: '[/MouseLeft]' },
]);

expect(textbox).to.have.attribute('aria-expanded', 'false');
expect(listbox).to.equal(null);
});

it('should not render the clear button', () => {
const { queryByTitle } = render(
<Autocomplete
Expand Down

0 comments on commit a4d9cb1

Please sign in to comment.