mui / material-ui

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.
https://mui.com/material-ui/
MIT License
93.61k stars 32.21k forks source link

[material-ui][Autocomplete] How to use autocomplete (multiple) with an API? #43227

Closed svidskiy closed 2 months ago

svidskiy commented 2 months ago

Steps to reproduce

I am getting options from the API. If I select an option and then search for another one, I get a warning from isOptionEqualToValue because that option no longer exists. How can I solve this?

image

Current behavior

No response

Expected behavior

No response

Context

No response

Your environment

npx @mui/envinfo ``` System: OS: Windows 11 10.0.22631 Binaries: Node: 18.18.0 - C:\Program Files\nodejs\node.EXE npm: 10.5.2 - C:\Program Files\nodejs\npm.CMD pnpm: 9.6.0 - C:\Program Files\nodejs\pnpm.CMD Browsers: Chrome: Not Found Edge: Chromium (127.0.2651.74) npmPackages: @emotion/react: ^11.11.4 => 11.11.4 @emotion/styled: ^11.11.5 => 11.11.5 @mui/base: 5.0.0-beta.40 @mui/core-downloads-tracker: 5.15.17 @mui/icons-material: ^5.15.17 => 5.15.17 @mui/lab: ^5.0.0-alpha.170 => 5.0.0-alpha.170 @mui/material: ^5.15.17 => 5.15.17 @mui/private-theming: 5.15.14 @mui/styled-engine: 5.15.14 @mui/system: 5.15.15 @mui/types: 7.2.14 @mui/utils: 5.15.14 @mui/x-charts: ^7.3.2 => 7.3.2 @mui/x-data-grid: 7.3.2 @mui/x-data-grid-generator: ^6.20.0 => 6.20.0 @mui/x-data-grid-premium: 6.20.0 @mui/x-data-grid-pro: ^7.3.2 => 7.3.2 @mui/x-date-pickers: ^7.4.0 => 7.4.0 @mui/x-date-pickers-pro: ^7.4.0 => 7.4.0 @mui/x-license: 7.2.0 @mui/x-license-pro: 6.10.2 @types/react: ^18.2.66 => 18.2.79 react: ^18.2.0 => 18.2.0 react-dom: ^18.2.0 => 18.2.0 typescript: ^5.2.2 => 5.4.5 ```

Search keywords: autocomplete

mnajdova commented 2 months ago

Did you tried using the free solo option: https://mui.com/material-ui/react-autocomplete/#free-solo?

svidskiy commented 2 months ago

Did you tried using the free solo option: https://mui.com/material-ui/react-autocomplete/#free-solo?

Yes, but it allows the user to enter their own options in addition to the provided choices. Since I am using multiple, user can add their own options. And this is incorrect for my logic. And even if I use this and disable the user's custom option, it seems to me that it contradicts the logic of freeSolo, doesn't it?

Example

ZeeshanTamboli commented 2 months ago

@svidskiy, can you provide a reproduction where the warning occurs? If you're referring to this one, I don't see the warning in the console.

svidskiy commented 2 months ago

@svidskiy, can you provide a reproduction where the warning occurs? If you're referring to this one, I don't see the warning in the console.

There is no warning due to the use of freeSolo, but the user can add their own options. My initial goal is to eliminate warning and ensure that the user can only choose from the options provided.

https://github.com/user-attachments/assets/aaf386eb-5f50-4ebc-ba43-b75d72cd7a0d

Here is the updated example

I also saw that onChange returns the argument reason, which has 4 possible values, one of which is createOption. I can block the addition of an option, and problem will be solved, but I think it's not the right approach.

ZeeshanTamboli commented 2 months ago

This issue is duplicate of #29727. I'll try to create a PR soon to remove this warning altogether.