-
-
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] Warn when value and option doesn't match #18514
Comments
This comment has been minimized.
This comment has been minimized.
Control the component, we don't aim to support |
Thank you for your reply. I am newbie andI think dificulty come from here... but when I add state variable on autocomplete value, items are show on textarea but not selected on dropdown list (and I can selected them second time). |
You can find one controlled demo in https://material-ui.com/components/autocomplete/#playground. |
I have see this but when I try to manage values directely, dropdownlist is not updated with selected values passed by state as you can see on my exemple : Push button change list and add item preselected but in dropdown list this item is not selected and we can add it another time. |
@guimex22 The value needs to be referentially equal to the option to consider it selected. We have #18443 to customize this. However, I think that we have an opportunity to warn about this case. What do you think of this diff? diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index f833a0c0c..5cd34fa78 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -247,6 +247,35 @@ export default function useAutocomplete(props) {
popupOpen = freeSolo && filteredOptions.length === 0 ? false : popupOpen;
+ if (process.env.NODE_ENV !== 'production') {
+ if (value !== null && !freeSolo && options.length > 0) {
+ const missingValue = (multiple ? value : [value]).filter(
+ value2 => !options.some(option => getOptionSelected(option, value2)),
+ );
+
+ if (missingValue.length > 0) {
+ // eslint-disable-next-line react-hooks/rules-of-hooks
+ console.warn(
+ [
+ `Material-UI: you have provided an out-of-range value${
+ missingValue.length > 1 ? 's' : ''
+ } \`${
+ missingValue.length > 1
+ ? JSON.stringify(missingValue)
+ : JSON.stringify(missingValue[0])
+ }\` for the autocomplete ${id ? `(id="${id}") ` : ''}component.`,
+ '',
+ 'Consider providing a value that matches one of the available options.',
+ 'You can use the `getOptionSelected` prop to customize the equality test.',
+ ].join('\n'),
+ );
+ }
+ }
+ }
+
const focusTag = useEventCallback(tagToFocus => {
if (tagToFocus === -1) {
inputRef.current.focus(); Do you want to work on a pull request? :) |
@oliviertassinari , I'd like to work on this if the op doesn't have the time. Can I go ahead? |
@blueyedgeek Awesome, feel free to go ahead 👌 |
@oliviertassinari I'd like to jump on this if no one is currently active. |
@hefedev Great :) we will also need a test case. |
@hefedev, @oliviertassinari I had meant to work on this but some personal issues came up and I couldn't find the time to do it. Glad someone else was able to put in the required effort 👏🏾 |
I won't be able to work on this anymore. but for the new first timers please check below and make sure you need a test case for it. |
@oliviertassinari Can I take this issue? |
@igorbrasileiro Sure :) |
I think I'm having a problem now because of this. I'm using an object as value and options like this:
it is still working like before but now I'm having a spam of warnings. Any suggestion? |
@CHPrado Check the |
Thanks, this worked:
|
This comment has been minimized.
This comment has been minimized.
@oliviertassinari I'm in the same situation as @cdpautsch where we are fetching our possible options from a back-end service. When we filter and remove the previous list of options the previous selected option persists behind the scenes, I say behind scenes because you won't see it as an option from the new list in the dropdown. So here you are looking at the new options and don't select any of them but rather click outside(away from dropdown) that's when the previous selected option just pops up again in the TextField and in the console the warning that suggests getOptionsSelected; which we already have in place. So just to re-iterate on your guys' convo there's no way around this correct ↓
I understand that this was closed after @igorbrasileiro worked on it, if there's a new way to work with this I totally missed it, was this fixed? @oliviertassinari highly appreciate your time, thanks! |
@rmar72 Apply the same strategy as @cdpautsch to solve the issue, let the component hides the already selected options for you. |
@oliviertassinari appreciate the reply. How is this not an issue with the google maps api example? You are loading new options, getting rid of the previous selected option and these are updating the options given prop and selected value. |
@rmar72 Thanks for raising about the warning that http://material-ui.com/components/autocomplete#google-maps-place has. Could you open a new issue? |
@oliviertassinari , I have a problem when I want to init the value in the AutoComplete in that case I am init the value with a json, but the component working good and in my component changed for this way |
Is there any way to suppress this warning? |
@piotros By providing the missing option? :) |
@oliviertassinari this is not always possible ;) In my use case options changes on user actions but I want to have all selected values preserved. Take look at simple simulation of my case: https://stackblitz.com/edit/material-ui-autocomplete Autocomplete components acts as a multistep select. This works as I expect but this worning... :) |
@piotros Nice "hack" for building a "tree select" component with the Autocomplete 😁. Try this diff: return (
<Autocomplete
renderInput={params => <TextField {...params} />}
multiple
open={open}
getOptionLabel={({ label }) => label}
- options={options}
+ options={[...value, ...options]}
+ filterSelectedOptions
onChange={(e, val) => setValue(val)}
/>
); |
Thank you @oliviertassinari. Works perfect now! :) |
Hi, how do I stop this warning happening when the initial value is nothing selected and on clear? |
This comment has been minimized.
This comment has been minimized.
If anybody is struggling with this around, try combining this:
And this:
|
i have the same warning but i already added getOptionSelected, i make infinity console warning
the code:
the log from getOptionSelected :
Any idea please ? |
I personally added the |
I want to init first selection as empty string, but I got warning, any solution please?
here is my code: |
@jcable @nicholasbca
Multiselect has already been solved by @oliviertassinari |
I had the same problem with warnings for I ended up using this because I didn't want to enable
|
I have the same issue as @cdpautsch:
However I was able to get around the strictness of the Autocomplete API with the following code:
I originally was using |
This is my case. I have a component wrapping some form elements.
|
We had a similar issue where we were getting filtered options from the backend depending on the current input value. As such, the currently selected value may or may not have been present in the options returned by the backend which in turn triggered the warning everyone is talking about here. To solve this we did the following: <Autocomplete
options={[value, ...serverOptions]} // Currently selected value must be preset to not trigger the missing value warning.
filterOptions={() => serverOptions} // But we only show what BE returned to us, so there are no duplicates.
value={value}
onChange={(_, value) => onChange(value)}
onInputChange={(_, value) => setSearchQuery(value)} // Triggers BE to return filtered options.
/> This works exactly how we want it to work, including that the selected option is highlighted. |
Just wanted to add these warnings do more harm than good. |
I followed the suggestion from @ifeltsweet and I was able to make this warning disappear. For context, I had three |
this worked for me. I had a reset button and it was virtually impossible to reset the autocomplete without getting that hideous warning. Thanks so much ✨ |
Hi,
I am trying to change list options and default value from state. List option is ok but default value is never update.
I have add an exemple on this code box :
https://codesandbox.io/s/test-material-ui-autocomplete-700nh?fontsize=14&hidenavigation=1&theme=dark
Thank you for yours ideas...
The text was updated successfully, but these errors were encountered: