Material-ui: [Autocomplete] onChange undefined intermittently when options updated

Created on 27 Nov 2019  路  4Comments  路  Source: mui-org/material-ui

  • [x] The issue is present in the latest release.
  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 馃槸


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).

Expected Behavior 馃


Autocomplete should select the selected item.

Steps to Reproduce 馃暪


Here's a codesandbox.io reproduction: https://codesandbox.io/s/autocomplete-and-async-suggestions-rrp70

Steps:

  1. Select 1 from the drop down (an item gets added to the options every second):
    image

  2. Keep selecting 1:
    image

  3. Eventually, onChange will select undefined:
    image

Context 馃敠


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.

Your Environment 馃寧

| Tech | Version |
| ----------- | ------- |
| Material-UI | v4.6.1 (lab 4.0.0-alpha.32) |
| React | 16.12.0 |
| Browser | Chrome |
| TypeScript | No |

bug 馃悰 Autocomplete good first issue

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pola88 picture pola88  路  3Comments

reflog picture reflog  路  3Comments

FranBran picture FranBran  路  3Comments

sys13 picture sys13  路  3Comments

ghost picture ghost  路  3Comments