[AutoComplete] does not update displayed value if options are received after the value

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

An autocomplete who has its options given to it after the value, does not update its displayed value, even though getOptionLabel returns the right value

Expected behavior 🤔

The displayed value should reflect the getOptionLabel function

Steps to reproduce 🕹

https://codesandbox.io/s/autcomplete-wrong-value-displayed-when-options-arrive-after-value-b9kkvm?file=/src/autocomplete.js

Steps:

  1. Open page, information about object is fetched and set
  2. Options are fetched and tend to arrive after the information about the object
  3. Options are set and given to AutoComplete
  4. AutoComplete displays initial value instead updating to the result of getOptionLabel

If we swap the delays and make the information about the object arrive after the options, the correct value is displayed. getOptionLabel returns the correct value at all times. The value is updated when interacting with the field

Context 🔦

It is technically a duplicate of #25096 (which I opened last year) but we just got around to upgrading to MUI 5 last week (in part hoping to have this fixed as noted in #27313) but after tests, the issue persists. After digging, I saw that this pull request did a regression discussed in #28098 and an additional fix was done.

Long story short, the reported behavior is still incorrect for us. getOptionLabel returns the right value but the initial value is displayed incorrectly on load of the page

Is there a way to force a value update? Any workaround?

Your environment 🌎

npx @mui/envinfo
  System:
    OS: Windows 10 10.0.19044
  Binaries:
    Node: 16.15.1 - C:\Program Files\nodejs\node.EXE
    Yarn: Not Found
    npm: 8.13.2 - ~\Desktop\isovision-web-ui-mui5\node_modules\.bin\npm.CMD
  Browsers:
    Chrome: 103.0.5060.114
    Edge: Spartan (44.19041.1266.0), Chromium (103.0.1264.37)
  npmPackages:
    @emotion/react: ^11.9.3 => 11.9.3
    @emotion/styled: ^11.9.3 => 11.9.3
    @mui/base:  5.0.0-alpha.87
    @mui/icons-material: ^5.8.4 => 5.8.4
    @mui/lab: ^5.0.0-alpha.88 => 5.0.0-alpha.88
    @mui/material: ^5.8.6 => 5.8.6
    @mui/private-theming:  5.8.6
    @mui/styled-engine:  5.8.0
    @mui/styles: ^5.8.6 => 5.8.6
    @mui/system:  5.8.6
    @mui/types:  7.1.4
    @mui/utils:  5.8.6
    @mui/x-date-pickers: ^5.0.0-alpha.7 => 5.0.0-alpha.7
    @types/react:  17.0.47
    react: ^17.0.2 => 17.0.2
    react-dom: ^17.0.2 => 17.0.2
    typescript:  4.7.4

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mnajdovacommented, Aug 24, 2022

We are not invoking onChange if the options change, and in my opinion this is correct behavior (as there wasn’t any interaction with the component). I would assume you can wait for all promises to resolve before setting the value, this way it should work as expected.

0reactions
Balourcommented, Nov 21, 2022

Confirmed as working in my use case, thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Autocomplete] does not update displayed value if options are ...
The issue is present in the latest release. I have searched the issues of this repository and believe that this is not a...
Read more >
reactjs - MUI Autocomplete not updating selected value when ...
Solution: For solution on your codesandbox you need 2 steps to fix your problem;. Make selected useStates initial values null inside App.js.
Read more >
A Guide on Material UI AutoComplete in React - Refine Dev
The component's value is obtained from a predetermined range of acceptable values/options. Here's how to structure your option values:.
Read more >
Fixing Material UI Autocomplete Search As You Type - Lockstep
Autocomplete will take the first object key: value as the default key for the options. The solution is always around the corner, try...
Read more >
React Autocomplete component - Material UI - MUI
The autocomplete is a normal text input enhanced by a panel of suggested options. The widget is useful for setting the value of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found