diff --git a/docs/src/pages/components/autocomplete/Playground.js b/docs/src/pages/components/autocomplete/Playground.js index bf208044a67ba1..f98a595d867dd5 100644 --- a/docs/src/pages/components/autocomplete/Playground.js +++ b/docs/src/pages/components/autocomplete/Playground.js @@ -108,6 +108,12 @@ export default function Playground() { disablePortal renderInput={(params) => } /> + } + /> ); } diff --git a/docs/src/pages/components/autocomplete/Playground.tsx b/docs/src/pages/components/autocomplete/Playground.tsx index 44c68000d1c57c..caf4c047aea996 100644 --- a/docs/src/pages/components/autocomplete/Playground.tsx +++ b/docs/src/pages/components/autocomplete/Playground.tsx @@ -106,6 +106,12 @@ export default function Playground() { disablePortal renderInput={(params) => } /> + } + /> ); } diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js index 5e058d12693643..cec372d547181d 100644 --- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js +++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js @@ -479,6 +479,8 @@ export default function useAutocomplete(props) { setValue(newValue); }; + const isTouch = React.useRef(false); + const selectNewValue = (event, option, reasonProp = 'select-option', origin = 'options') => { let reason = reasonProp; let newValue = option; @@ -515,6 +517,14 @@ export default function useAutocomplete(props) { if (!disableCloseOnSelect) { handleClose(event, reason); } + + if ( + blurOnSelect === true || + (blurOnSelect === 'touch' && isTouch.current) || + (blurOnSelect === 'mouse' && !isTouch.current) + ) { + inputRef.current.blur(); + } }; function validTagIndex(index, direction) { @@ -769,8 +779,6 @@ export default function useAutocomplete(props) { setHighlightedIndex(index, 'mouse'); }; - const isTouch = React.useRef(false); - const handleOptionTouchStart = () => { isTouch.current = true; }; @@ -779,14 +787,6 @@ export default function useAutocomplete(props) { const index = Number(event.currentTarget.getAttribute('data-option-index')); selectNewValue(event, filteredOptions[index], 'select-option'); - if ( - blurOnSelect === true || - (blurOnSelect === 'touch' && isTouch.current) || - (blurOnSelect === 'mouse' && !isTouch.current) - ) { - inputRef.current.blur(); - } - isTouch.current = false; };