Autocomplete sometimes returns undefined as the value in onChange when selecting options from a list that updates (the option is present in both previous and current list).
Autocomplete should select the selected item.
Here's a codesandbox.io reproduction: https://codesandbox.io/s/autocomplete-and-async-suggestions-rrp70
Steps:
Select 1 from the drop down (an item gets added to the options every second):

Keep selecting 1:

Eventually, onChange will select undefined:

I have a list of IoT devices and a autocomplete to select the IoT device by id say. As new IoT devices come online the list is updated asynchronously and device ids are added so there's no well-defined loading phase.
| Tech | Version |
| ----------- | ------- |
| Material-UI | v4.6.1 (lab 4.0.0-alpha.32) |
| React | 16.12.0 |
| Browser | Chrome |
| TypeScript | No |
Similar behaviour occurs when using the "multiple", "filterSelectedOptions", "disableCloseOnSelect" props together, and selecting multiple options without moving the mouse after selection.
codesandbox: https://codesandbox.io/s/material-demo-f5sfu
@jellyedwards @Dror88 The proposed fix in #18456 solves this problem.
However, it does surface another concerning problem: the keyboard navigation is fucked up (the focus is reset to the first index every second). It seems that the following would work better:
diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index 12ca84907..fe7d5ac03 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -371,7 +371,7 @@ export default function useAutocomplete(props) {
React.useEffect(() => {
changeHighlightedIndex('reset', 'next');
- }, [filteredOptions.length]); // eslint-disable-line react-hooks/exhaustive-deps
+ }, [inputValue]); // eslint-disable-line react-hooks/exhaustive-deps
const handleOpen = event => {
if (open) {
Do you confirm? Do you want to explore a pull request :)? Thanks
Hey @oliviertassinari it looks like both of your fixes will sort it. I'll try to do a PR & add a test or two.
Sorry forgot to say thanks! @oliviertassinari