Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Autocomplete] Fix blurOnSelect consistency for keyboard #20314

Merged
merged 2 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/src/pages/components/autocomplete/Playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ export default function Playground() {
disablePortal
renderInput={(params) => <TextField {...params} label="disablePortal" margin="normal" />}
/>
<Autocomplete
{...defaultProps}
id="blur-on-select"
blurOnSelect
renderInput={params => <TextField {...params} label="blurOnSelect" margin="normal" />}
/>
</div>
);
}
Expand Down
6 changes: 6 additions & 0 deletions docs/src/pages/components/autocomplete/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export default function Playground() {
disablePortal
renderInput={(params) => <TextField {...params} label="disablePortal" margin="normal" />}
/>
<Autocomplete
{...defaultProps}
id="blur-on-select"
blurOnSelect
renderInput={params => <TextField {...params} label="blurOnSelect" margin="normal" />}
/>
</div>
);
}
Expand Down
21 changes: 11 additions & 10 deletions packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -769,8 +780,6 @@ export default function useAutocomplete(props) {
setHighlightedIndex(index, 'mouse');
};

const isTouch = React.useRef(false);

const handleOptionTouchStart = () => {
isTouch.current = true;
};
Expand All @@ -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;
};

Expand Down