From 62bb42d4023d949bb0d0fc4f0048e3b08fcbc944 Mon Sep 17 00:00:00 2001 From: Monbrey Date: Tue, 17 Dec 2019 07:17:47 +1100 Subject: [PATCH 1/3] Prevent focusing the input on clear --- .../src/useAutocomplete/useAutocomplete.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js index ca8afabdf98834..85636b90ab2cce 100644 --- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js +++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js @@ -108,6 +108,7 @@ export default function useAutocomplete(props) { setDefaultId(`mui-autocomplete-${Math.round(Math.random() * 1e5)}`); }, []); + const ignoreFocus = React.useRef(false); const firstFocus = React.useRef(true); const inputRef = React.useRef(null); const listboxRef = React.useRef(null); @@ -509,10 +510,8 @@ export default function useAutocomplete(props) { }; const handleClear = event => { + ignoreFocus.current = true; handleValue(event, multiple ? [] : null); - if (disableOpenOnFocus) { - handleClose(); - } setInputValue(''); }; @@ -614,7 +613,7 @@ export default function useAutocomplete(props) { const handleFocus = event => { setFocused(true); - if (!disableOpenOnFocus) { + if (!disableOpenOnFocus && !ignoreFocus.current) { handleOpen(event); } }; @@ -622,6 +621,7 @@ export default function useAutocomplete(props) { const handleBlur = event => { setFocused(false); firstFocus.current = true; + ignoreFocus.current = false; if (debug && inputValue !== '') { return; @@ -706,8 +706,6 @@ export default function useAutocomplete(props) { }); const handlePopupIndicator = event => { - inputRef.current.focus(); - if (open) { handleClose(event); } else { @@ -715,13 +713,14 @@ export default function useAutocomplete(props) { } }; + // Prevent input blur when interacting with the combobox const handleMouseDown = event => { if (event.target.nodeName !== 'INPUT') { - // Prevent blur event.preventDefault(); } }; + // Focus the input when first interacting with the combobox const handleClick = () => { if ( firstFocus.current && From 25c41ead8d441f2111c42fd348c629ecb6fb42c6 Mon Sep 17 00:00:00 2001 From: Monbrey Date: Tue, 17 Dec 2019 08:47:08 +1100 Subject: [PATCH 2/3] Test case --- .../src/Autocomplete/Autocomplete.test.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js index 62b4b928d402da..2b4fc069fd7849 100644 --- a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js +++ b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js @@ -205,6 +205,27 @@ describe('', () => { expect(handleOpen.callCount).to.equal(1); }); + it('does not open on clear', () => { + const handleOpen = spy(); + const handleChange = spy(); + const { container } = render ( + } + />, + ) + + const clear = container.querySelector('button') + fireEvent.click(clear); + + expect(handleOpen.callCount).to.equal(0); + expect(handleChange.callCount).to.equal(1); + }); + ['ArrowDown', 'ArrowUp'].forEach(key => { it(`opens on ${key} when focus is on the textbox without moving focus`, () => { const handleOpen = spy(); From e3309158bf74ebbc239103b0738d029d64f1a261 Mon Sep 17 00:00:00 2001 From: Monbrey Date: Tue, 17 Dec 2019 09:00:58 +1100 Subject: [PATCH 3/3] Prettier formatting --- .../material-ui-lab/src/Autocomplete/Autocomplete.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js index 2b4fc069fd7849..01dc865ed01fe4 100644 --- a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js +++ b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js @@ -208,18 +208,18 @@ describe('', () => { it('does not open on clear', () => { const handleOpen = spy(); const handleChange = spy(); - const { container } = render ( + const { container } = render( } + renderInput={params => } />, - ) + ); - const clear = container.querySelector('button') + const clear = container.querySelector('button'); fireEvent.click(clear); expect(handleOpen.callCount).to.equal(0);