Skip to content

Commit

Permalink
[Autocomplete] Change behavior of mouseDown event and add new test case
Browse files Browse the repository at this point in the history
The text box inside Autocomplete component responds to click event.
If the text box is empty it will toggle the list box.
If the text box is not empty it will only open the list box.
  • Loading branch information
marcosvega91 committed Apr 24, 2020
1 parent 898976e commit e1a6a03
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
27 changes: 24 additions & 3 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,7 @@ describe('<Autocomplete />', () => {
expect(handleChange.args[1][2]).to.equal('mouse');
});

it('should open list box even if the input contains an option value', () => {
it('should mantain list box open clicking on input when it is not empty', () => {
const handleChange = spy();
const { queryByRole, getByRole, getAllByRole } = render(
<Autocomplete
Expand All @@ -1622,12 +1622,33 @@ describe('<Autocomplete />', () => {
);
const textbox = getByRole('textbox');
expect(queryByRole('presentation')).to.equal(null);
fireEvent.mouseDown(textbox);
fireEvent.mouseDown(textbox); // Open listbox
expect(queryByRole('presentation')).to.not.equal(null);
const options = getAllByRole('option');
fireEvent.click(options[0]);
expect(queryByRole('presentation')).to.equal(null);
fireEvent.mouseDown(textbox);
fireEvent.mouseDown(textbox); // Open listbox
expect(queryByRole('presentation')).to.not.equal(null);
fireEvent.mouseDown(textbox); // Remain open listbox
expect(queryByRole('presentation')).to.not.equal(null);
});

it('should toggle list box when input is empty', () => {
const handleChange = spy();
const { queryByRole, getByRole } = render(
<Autocomplete
onHighlightChange={handleChange}
options={['one']}
renderInput={(params) => <TextField {...params} />}
/>,
);
const textbox = getByRole('textbox');
expect(queryByRole('presentation')).to.equal(null);
fireEvent.mouseDown(textbox); // Open listbox
expect(queryByRole('presentation')).to.not.equal(null);
fireEvent.mouseDown(textbox); // close listbox
expect(queryByRole('presentation')).to.equal(null);
fireEvent.mouseDown(textbox); // open listbox
expect(queryByRole('presentation')).to.not.equal(null);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,9 @@ export default function useAutocomplete(props) {
};

const handleInputMouseDown = (event) => {
handlePopupIndicator(event);
if (inputValue === '' || !open) {
handlePopupIndicator(event);
}
};

let dirty = freeSolo && inputValue.length > 0;
Expand Down

0 comments on commit e1a6a03

Please sign in to comment.