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] filterSelectedOptions with multiple doesn't work when state is controlled via react-hook-form #45489

Open
jakobmerrild opened this issue Mar 5, 2025 · 0 comments
Assignees
Labels
component: autocomplete This is the name of the generic UI component, not the React module! status: waiting for maintainer These issues haven't been looked at yet by a maintainer

Comments

@jakobmerrild
Copy link

jakobmerrild commented Mar 5, 2025

Steps to reproduce

Steps:

  1. Open this link to live example: https://codesandbox.io/p/devbox/zealous-ioana-zjlfr4?from-embed=&workspaceId=ws_8Gvn86jdwuzyvAQSp15K4u
  2. Selecting "The Shawshank Redemption" doesn't cause it to be filtered out
  3. Selecting "The Shawshank Redemption" a second time doesn't cause it to be removed, it's just appended to the form values

Current behavior

When using the Autocomplete component with multiple values while controlling the state using react-hook-form you can

  1. Select the same option multiple times
  2. Still see selected options even though the filterSelectedOptions prop is provided

Expected behavior

  1. You shouldn't be able to select the same option more than once
  2. filterSelectedOptions should correctly filter out already selected options

Managed to workaround issue (1) by using the reason and details parameters from onChange here: https://codesandbox.io/p/devbox/xenodochial-fermat-wllkh3?from-embed=&workspaceId=ws_8Gvn86jdwuzyvAQSp15K4u

Context

I'm using react-hook-form to manage form state, with part of my form being a multi-select from a list of ~300 options.

I thought using multiple with Autocomplete would be perfect for this to allow the user to search among all the options easily, but it doesn't quite work exactly the way I want it to when also using react-hook-form to manage the state of the entire form.

Your environment

Tested in Chrome, but observed in Safari, and Firefox as well via Code Sandbox

npx @mui/envinfo
  System:
    OS: macOS 15.3.1
  Binaries:
    Node: 20.17.0 - ~/.nvm/versions/node/v20.17.0/bin/node
    npm: 10.8.3 - ~/.nvm/versions/node/v20.17.0/bin/npm
    pnpm: Not Found
  Browsers:
    Chrome: 133.0.6943.142
    Edge: Not Found
    Safari: 18.3
  npmPackages:
    @emotion/react: ^11.14.0 => 11.14.0 
    @emotion/styled: ^11.14.0 => 11.14.0 
    @mui/base:  5.0.0-beta.58 
    @mui/core-downloads-tracker:  6.4.6 
    @mui/icons-material: ^6.4.1 => 6.4.6 
    @mui/lab: 6.0.0-beta.10 => 6.0.0-beta.10 
    @mui/material: ^6.4.1 => 6.4.6 
    @mui/material-nextjs: ^6.3.1 => 6.4.3 
    @mui/private-theming:  6.4.6 
    @mui/styled-engine:  6.4.6 
    @mui/system:  6.4.6 
    @mui/types:  7.2.21 
    @mui/utils:  6.4.6 
    @mui/x-date-pickers: ^7.24.1 => 7.27.1 
    @mui/x-internals:  7.26.0 
    @toolpad/core: ^0.12.0 => 0.12.0 
    @toolpad/utils:  0.12.0 
    @types/react: ^19.0.8 => 19.0.10 
    react: ^19.0.0 => 19.0.0 
    react-dom: ^19.0.0 => 19.0.0 
    typescript: ^5.7.3 => 5.7.3 

Search keywords: react-hook-form autocomplete multiple filterSelectedOptions

@jakobmerrild jakobmerrild added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Mar 5, 2025
@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: autocomplete This is the name of the generic UI component, not the React module! status: waiting for maintainer These issues haven't been looked at yet by a maintainer
Projects
None yet
Development

No branches or pull requests

3 participants