Skip to content

Commit 35f9143

Browse files
authored
[Autocomplete] Fix blurOnSelect consistency for keyboard (#20314)
1 parent d596401 commit 35f9143

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

docs/src/pages/components/autocomplete/Playground.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ export default function Playground() {
108108
disablePortal
109109
renderInput={(params) => <TextField {...params} label="disablePortal" margin="normal" />}
110110
/>
111+
<Autocomplete
112+
{...defaultProps}
113+
id="blur-on-select"
114+
blurOnSelect
115+
renderInput={(params) => <TextField {...params} label="blurOnSelect" margin="normal" />}
116+
/>
111117
</div>
112118
);
113119
}

docs/src/pages/components/autocomplete/Playground.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ export default function Playground() {
106106
disablePortal
107107
renderInput={(params) => <TextField {...params} label="disablePortal" margin="normal" />}
108108
/>
109+
<Autocomplete
110+
{...defaultProps}
111+
id="blur-on-select"
112+
blurOnSelect
113+
renderInput={(params) => <TextField {...params} label="blurOnSelect" margin="normal" />}
114+
/>
109115
</div>
110116
);
111117
}

packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ export default function useAutocomplete(props) {
490490
setValue(newValue);
491491
};
492492

493+
const isTouch = React.useRef(false);
494+
493495
const selectNewValue = (event, option, reasonProp = 'select-option', origin = 'options') => {
494496
let reason = reasonProp;
495497
let newValue = option;
@@ -526,6 +528,14 @@ export default function useAutocomplete(props) {
526528
if (!disableCloseOnSelect) {
527529
handleClose(event, reason);
528530
}
531+
532+
if (
533+
blurOnSelect === true ||
534+
(blurOnSelect === 'touch' && isTouch.current) ||
535+
(blurOnSelect === 'mouse' && !isTouch.current)
536+
) {
537+
inputRef.current.blur();
538+
}
529539
};
530540

531541
function validTagIndex(index, direction) {
@@ -780,8 +790,6 @@ export default function useAutocomplete(props) {
780790
setHighlightedIndex(index, 'mouse');
781791
};
782792

783-
const isTouch = React.useRef(false);
784-
785793
const handleOptionTouchStart = () => {
786794
isTouch.current = true;
787795
};
@@ -790,14 +798,6 @@ export default function useAutocomplete(props) {
790798
const index = Number(event.currentTarget.getAttribute('data-option-index'));
791799
selectNewValue(event, filteredOptions[index], 'select-option');
792800

793-
if (
794-
blurOnSelect === true ||
795-
(blurOnSelect === 'touch' && isTouch.current) ||
796-
(blurOnSelect === 'mouse' && !isTouch.current)
797-
) {
798-
inputRef.current.blur();
799-
}
800-
801801
isTouch.current = false;
802802
};
803803

0 commit comments

Comments
 (0)