Skip to content

[autocomplete] Support values other than raw options #23708

Closed

Description

@dantman
  • I have searched the issues of this repository and believe that this is not a duplicate.

Summary 💡

Right now (at least if you use multiple) Autocomplete/useAutocomplete the value that comes back in onChange is always the raw option data. i.e. If you use options=[{value: 'a', label: 'A'}, {value: 'b', label: 'B'}] and select the "A" option then value will be [{value: 'a', label: 'A'}].

Ideally it would be possible to provide multiple=true options=[{value: 'a', label: 'A'}, {value: 'b', label: 'B'}] and get back values like ['a']. Autocomplete does feel like this is intended to be supported, because you can provide a getOptionSelected and it's used to identify selected options instead of doing any comparisons on the option itself. However when it comes to adding an item to a multiple array, all useAutocomplete does is newValue.push(option);. There is no way to control what part of option is actually used as the value.

I think a getOptionValue(option) function would fit the current options implementation the most.

Examples 🌈

<Autocomplete
  multiple
  getOptionSelected={(option, value) => option.value === value}
  getOptionValue={(option) => option.value}
  options={[{value: 'US', label: 'United States', {value: 'CA', label: 'Canada'}]}
  />

Motivation 🔦

MUI's <Select> easily supports this. It uses children instead, so all it takes is options.map(item => <MenuItem key={item.value} value={item.value}>{item.value}</MenuItem>) to do this in Select and pretty much every project I've worked on uses Select this way. It would be strongly preferred if it were easy to use useAutocomplete the same way.

Activity

  1. added
    scope: autocompleteChanges related to the autocomplete. This includes ComboBox.
    docsImprovements or additions to the documentation.
    good first issueGreat for first contributions. Enable to learn the contribution process.
    and removed on Nov 25, 2020
  2. oliviertassinari commented on Nov 25, 2020

    @oliviertassinari
  3. changed the title [-][Autocomplete:multiple] Support values other than raw options[/-] [+][Autocomplete] Support values other than raw options[/+] on Nov 25, 2020
  4. dantman commented on Nov 25, 2020

    @dantman
    ContributorAuthor

    Forcing the developer to manually manage the values list doesn't feel like a great alternative. Using Select, TextField, etc... isn't this complex even when using the {value, label} pattern.

    However, in your case, the simplest is probably to store the value as-is, and derive it with a map when you need to transform it. Why not use this approach?

    You mean store country: [{value: 'US', label: 'United States'}, {value: 'CA', label: 'Canada'}] in the form values when US+CA are selected and countries.map(country => country.value) it when doing an api call?

    Honestly it feels wrong to have localized labels in form data just because of limitations in useAutocomplete's implementation. And MUI doesn't work this way with any other field. Select, Radio, and Checkbox can easily be connected to form handling code and all support using a separate value and label for options. It doesn't feel right that if you have a Select implemented that way (with the same simple submit handler code as the rest of your fields) and it becomes too long to be a normal select, you have to rewrite the submit handler code to work differently than every other field, just because the field type used changed from Select to Autocomplete.

  5. oliviertassinari commented on Nov 25, 2020

    @oliviertassinari
    Member

    We have a similar pain in this direction on #21489. Ok, why not. we could:

    • use getOptionValue to feed the default behavior of getOptionSelected.
    • use getOptionValue to later implement native integration with a <form>.

    Feel free to work on it. At least, we have spotted a small issue with the generated documentation.

  6. added
    ready to takeHelp wanted. Guidance available. There is a high chance the change will be accepted
    and removed
    docsImprovements or additions to the documentation.
    good first issueGreat for first contributions. Enable to learn the contribution process.
    on Dec 10, 2020
  7. ZovcIfzm commented on Dec 12, 2020

    @ZovcIfzm
    Contributor

    Could I try and take a wack at this?

  8. ZovcIfzm commented on Dec 13, 2020

    @ZovcIfzm
    Contributor

    Hi! I'm completely new to open source contributions so I'm a bit confused, wouldn't this just be a one line change? Where we just add,

    getOptionValue = (option) => option.value ?? option,
    

    to Autocomplete.js
    And then maybe a test?

    I'm thinking since getOptionValue is a completely new function, nothing depends on it so all I would need to add is the function itself and a test.

    Please correct me if I'm wrong, thank you so much!

  9. oliviertassinari commented on Dec 13, 2020

    @oliviertassinari
    Member

    @ZovcIfzm If you are completely new, you might want to do one "good first issue" before moving to this "goo to take" issue.

    Regarding your question. Yes, we would need a new test case, to make sure it has the correct impacts on the component, e.g. change the onChange returned value, change how value <-> option comparison is done, etc.

  10. 34 remaining items

  11. Mohammad-Khayata-4441 commented on Sep 3, 2024

    @Mohammad-Khayata-4441
  12. aress31 commented on Jan 8, 2025

    @aress31
  13. changed the title [-][Autocomplete] Support values other than raw options[/-] [+][autocomplete] Support values other than raw options[/+] on Mar 1, 2025
  14. exorex commented on Apr 9, 2025

    @exorex

    @oliviertassinari This feature would be a major improvement. Supporting custom values beyond the raw options would unlock much more flexibility for advanced use cases.

  15. googamanga commented on Apr 13, 2025

    @googamanga
  16. Will-at-FreedomDev commented on Jun 5, 2025

    @Will-at-FreedomDev

    This is one of the biggest pain points for me every single time I work with MUI forms. Sigh, here we go again. Not sure how many more upvotes we need to make it obvious that getOptionValue is the right way to go. Sorry for the rant, I appreciate you all.

    :'(

    I incorrectly assumed that this was how this already worked. Something like this is what makes me enjoy working with Angular Material.

  17. brendonrapp commented on Jun 18, 2025

    @brendonrapp

    Straight from the Select component documentation:

    If you are looking for more advanced features, like combobox, multiselect, autocomplete, async or creatable support, head to the Autocomplete component. It's meant to be an improved version of the "react-select" and "downshift" packages.

    "react-select" has getOptionValue. Lacking this makes Autocomplete fall short of being the "improved" version it's claimed to be.

  18. added
    scope: autocompleteChanges related to the autocomplete. This includes ComboBox.
    and removed
    scope: autocompleteChanges related to the autocomplete. This includes ComboBox.
    on Aug 17, 2025
  19. hakon-dale commented on Aug 26, 2025

    @hakon-dale
  20. mj12albert commented on Apr 21, 2026

    @mj12albert
    Member

    Considering the upvotes, I'll look into adding this. In the meantime, if you have objects in the dataset and want to have string values, workarounds are possible:

  21. added
    has workaroundThere’s a bug, but users have a complete workaround, so no urgent fix or release is needed.
    on Apr 21, 2026
  22. github-actions commented on Oct 5, 2026

    @github-actions
    Contributor

    This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
    Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.


    @dantman Thanks again for creating this issue! If you have a moment, we would love to hear your thoughts on how we handled it with this short feedback form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    has workaroundThere’s a bug, but users have a complete workaround, so no urgent fix or release is needed.scope: autocompleteChanges related to the autocomplete. This includes ComboBox.type: new featureExpand the scope of the product to solve a new problem.waiting for 👍Waiting for upvotes. Open for community feedback and needs more interest to be worked on.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions