-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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] allowDuplicate option #18755
Comments
@thealjey Could you provide more detail on your use case? What prevents you from using two different objects with the same representation? |
When you select an item from the drop-down menu, it becomes highlighted, if you click on it again it gets removed instead of being added again as a duplicate (which is what I want). |
What do you think of using the |
What do you mean? |
Yeap, it wouldn't be a straightforward approach. I haven't benchmark how the other solutions solve this problem. If you could look at this, it would be awesome. |
@thealjey this issue/question is really old but still wanted to share my piece in case it helps anyone. So I unintentionally landed in the situation you are after and that was a problem for me rather than a desired effect, but I suppose you can benefit from it. This component checks for 'referential' equality when determining what is selected and what is not. So if you were to make sure your options are always generated on the fly rather than a constant list, and also make sure the option items are objects and not primitives, you can achieve what you want. For example:
you will get what you want. This is because when I hope this helps someone. |
Hi @jzaynal ! I'm trying to implement exactly this case (autocomplete with multiple chips/options that can be selected more than once), but I don't see how to use your hack to do it. What I am trying to accomplish is an autocomplete input to help the user "build" a formula, Excel alike. So, for example, the user start writing, the input gives him some options to select (like products), and operands (like +-*), and the user can play around building a formula, selecting Products and operands to form something like "Product1+Product2+10". That's why I need to allow duplicate options. |
I solved this by setting: isOptionEqualToValue={(option, value) => false} the value is still there in the onChange event, though you do get bunch of console warnings about it. |
Just wanted to add to the solution @jakeyizle mentioned. For the people using mui v4, you will have to use |
Thanks, @elarouche and @jakeyizle. That worked great for me, I also didn't need to use option and value, it worked as intended with |
Thanks @jakeyizle! |
I want to be able to add the same chip more than once.
Summary 💡
By setting the allowDuplicate prop to true the items in the dropdown list will not be highlighted and will always add a new item to the value array.
Examples 🌈
Motivation 🔦
I am making a GUI builder application for react components.
Items in the Autocomplete describe react children.
I need to be able to add the same child more than once.
The text was updated successfully, but these errors were encountered: