From 064f41d60e4df3627267d74fbb0fd33711f37c6a Mon Sep 17 00:00:00 2001 From: Alex Barkin Date: Sat, 28 Mar 2020 19:08:12 -0400 Subject: [PATCH 1/2] Enable blurOnSelect for keyboard input --- .../components/autocomplete/Playground.js | 6 ++++++ .../components/autocomplete/Playground.tsx | 6 ++++++ .../src/useAutocomplete/useAutocomplete.js | 21 ++++++++++--------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/docs/src/pages/components/autocomplete/Playground.js b/docs/src/pages/components/autocomplete/Playground.js index bf208044a67ba1..2c39b1a7679fd2 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..096d0880521379 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..eb9e162c36f3c9 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,15 @@ 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 +780,6 @@ export default function useAutocomplete(props) { setHighlightedIndex(index, 'mouse'); }; - const isTouch = React.useRef(false); - const handleOptionTouchStart = () => { isTouch.current = true; }; @@ -779,14 +788,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; }; From 88c9c4a4bb4102cabd9ba4543d97e97a0204ef55 Mon Sep 17 00:00:00 2001 From: Alex Barkin Date: Sat, 28 Mar 2020 19:28:26 -0400 Subject: [PATCH 2/2] Format fixes and prettifying --- docs/src/pages/components/autocomplete/Playground.js | 10 +++++----- docs/src/pages/components/autocomplete/Playground.tsx | 10 +++++----- .../src/useAutocomplete/useAutocomplete.js | 1 - 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/docs/src/pages/components/autocomplete/Playground.js b/docs/src/pages/components/autocomplete/Playground.js index 2c39b1a7679fd2..f98a595d867dd5 100644 --- a/docs/src/pages/components/autocomplete/Playground.js +++ b/docs/src/pages/components/autocomplete/Playground.js @@ -109,11 +109,11 @@ export default function Playground() { renderInput={(params) => } /> } - /> + {...defaultProps} + id="blur-on-select" + blurOnSelect + renderInput={(params) => } + /> ); } diff --git a/docs/src/pages/components/autocomplete/Playground.tsx b/docs/src/pages/components/autocomplete/Playground.tsx index 096d0880521379..caf4c047aea996 100644 --- a/docs/src/pages/components/autocomplete/Playground.tsx +++ b/docs/src/pages/components/autocomplete/Playground.tsx @@ -107,11 +107,11 @@ export default function Playground() { renderInput={(params) => } /> } - /> + {...defaultProps} + id="blur-on-select" + blurOnSelect + renderInput={(params) => } + /> ); } diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js index eb9e162c36f3c9..cec372d547181d 100644 --- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js +++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js @@ -518,7 +518,6 @@ export default function useAutocomplete(props) { handleClose(event, reason); } - if ( blurOnSelect === true || (blurOnSelect === 'touch' && isTouch.current) ||