-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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] getOptionLabel
callback is getting the value instead of option object
#31192
Comments
getOptionLabel
callback is getting the value instead of option object
Hi @hasan-aa , Why do you want to have
You can use the value of the selected option. See the following CodeSandbox: https://codesandbox.io/s/controllablestates-material-demo-forked-c4fw5y?file=/demo.js |
For my use-case, I wanted to have the option as a string because my onChange uses the value to update an object in redux state, and I don't want the label in there as well. |
Why was this closed and the suggested PR just rejected? I spent 2 days debugging what was breaking, only to find that Assuming I have an array of objects as Why am I therefore forced to save the whole object as the value? (just so The fix linked above should be reconsidered. |
Okay I am re-opening the PR. |
To support this request, I am using MUI with If this issue is not tackled, I will need to either:
I think the concept that |
A // outside of the component:
type Option = { value: string, name: string };
const options: Option[] = [ { name: 'Charles Dickens', value: 'charles-dickens' } ]
// in component:
const values = React.useMemo(() => options.map(o => o.value));
function getLabel(value) {
return options.find(o => o.value === value);
}
return (
<Autocomplete
options={values}
getOptionLabel={getLabel}
... Would this solve your problem? Having separate types for |
I think at least a note should be added to the documentation of |
@michaldudak Yes, but what we are saying is that this should not be the case. Value should not have to equal the option type. Value should be decoupled from options. Obviously there are various workarounds for this at the moment, the easiest one being just saving 2 states: one for saving the actual chosen object ( According to @Bearfinn this is already the implemented (and expected) behavior in the |
The documentation is misleading: Considering these words in the doc we expect that a bunch of string value + Should be: either make |
We would like to have the We can go ahead with updating the docs to mention that |
The most appropriate, yet longer, name for "getOptionLabel" would have been "getSelectedOptionLabel", because that is what this function actually does, and like many others, it caused me so much pain, especially when used with react-hook-form. |
Seeing as the 'docs' tag was added to this issue, I would like to stress that this issue should not be closed upon only updating the documentation. A nice heads up about the unexpected behavior in the docs would be nice, but it is only a band-aid solution until this problem is properly fixed. (See detailed problem description and previous fix attempts above.) |
One case where this breaks is when the field is cleared. In this case, the value is Why is |
This issue looks like a duplicate of #23708 but focus on the docs that is currently confusing. |
I am having a similar issue with the argument in the call back however this is when the value in the option object is an array, it is only giving me the array. I am running 5.11.2 e.g. options if option 1 is selected I am getting ["ABC", "DEF"] as the argument in getOptionLabel and not { id: 1, label: 'option 1', value: [ "ABC", 'DEF']} as I would expect from the api docs. I have workaround it for now but is is a little weird that the option object does exist in the correct shape to the spec. The workaround is a little inefficient as it means running a find on the available options again to get the label so would like to remove it. |
I have the same issue. My case: I expect to see correct name in Autocomplete input field , not undefined, because i define getOptionLabel and assume that option parameter is option and not value why value should have the same type as option if there is isOptionEqualToValue where i can say how value should be mapped to option. |
The same issue. based on the documentation, I would never have guessed that the 'label' in getOptionLabel is from 'values' and not from 'options'. I expect that the list of options is there to get information about options from it. Like if you use react-query for getting options, and react-hook-form for now I would need to reset form/field on every available options update. |
Agreed with everyone here and specifically @alenadvainova 's point
Converting the string back into an array using As you can empathise, any extra @ZeeshanTamboli @michaldudak As many others have mentioned, |
@gaurav21r This issue is being tracked in mui/base-ui#45. |
@michaldudak @ZeeshanTamboli still waiting on this, the getOptionLabel should have the selected option rather than the value to decouple the processed value from the displayed label. For instance, I do send the id only to my backend but display the name of the object. Right now this is a pain to get this working with autocomplete, by using the options as primary source for the autocomplete as opposed to the value that would solve the issue. |
I don't think there are plans to update the existing Autocomplete before switching the implementation to Base UI's Combobox (@aarongarciah, correct me if I'm wrong). |
@michaldudak correct #44863 (comment) |
Duplicates
Latest version
Current behavior 😯
When using different types for
options
andvalue
properties, thegetOptionLabel
method is passed value instead of option object.let's say I'm using following types:
I'm expecting getOptionLabel method always passed an Option type but that's not the case. That's why the picked option can not be displayed properly.
Expected behavior 🤔
Always pass and option to
getOptionLabel
as a param to be consistent.Steps to reproduce 🕹
Here's a demo:
https://codesandbox.io/s/controllablestates-material-demo-forked-0lekyg?file=/demo.js
Steps:
options
andvalue
props. (object and string)isOptionEqualToValue
andgetOptionLabel
callback propsContext 🔦
I'm trying to render options that are objects like {label:"Label", value:"some value"}, but I want to use the value of the selected option instead of the whole option object.
Your environment 🌎
No response
The text was updated successfully, but these errors were encountered: