-
-
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]filter not work correctly after selecting option once #19797
Comments
I think this line in useAutocomplete causes this issue. const inputValueIsSelectedValue =
!multiple && value != null && inputValue === getOptionLabel(value);
{ inputValue: inputValueIsSelectedValue ? '' : inputValue }, After selected the option, |
Why should it behave differently? What's your use case? |
Same input should display same option IMO. |
And we'd like to use Autocomplete in |
I'm closing as the behavior looks expected. We would be happy to reconsider it when we have a compelling use case for a different behavior. |
@oliviertassinari I was trying to just load options once and achieve the same effect so I opened this issue. |
Exactly |
@oliviertassinari |
I find this behaviour very strange. I would expect the current selected option to show in the dropdown options. It is very strange for it not to show. I am using Autocomplete for compiling an address on a form. The "country" input has the list of all the countries in the world. I am using match-sorter to filter the options, since the list of countries is an array of objects with some information about each country (ISO 3166-1 alpha-2, ISO 3166-1 alpha-3, localized name in Italian, English name, etc.). If the current value in the backend database is stored as "Italia", I get the correct option from the array of objects by filtering the options array in the In order to overcome this, I'm storing the current value in another hidden input field, then before returning the match-sorter I check whether inputValue is empty or not: if it's empty I force it's value back to the value of the hidden input field. This way I get my expected behaviour back: current selected option is showing as current selected option in the dropdown, and stays as the current selected option if I simply tab away. |
@JohnRDOrazio There is too much information in your previous comment, we can't act on it. Please ask for help on StackOverflow.
It sounds like you didn't provide a |
I did provide a |
@JohnRDOrazio |
I find that to be an odd behaviour, resulting in unexpected results for users. Why should no filters be applied when there is an existing value in the input? No filters should be applied only when there is no value, not when there is an existing value, in my opinion. I have updated the example with a second autocomplete, demonstrating my fix for an expected behaviour: If you click in the second autocomplete you will see that "Italia" is the current selected option, and if you click anywhere outside, when the dropdown closes the value in the input will stay "Italia". Whereas in the first example, since "Afghanistan" is the first selected option, if you click anywhere outside, when the dropdown closes "Afghanistan" will become the newly selected option, whereas the user probably didn't want to select any option different from the current value of the input. |
I'm facing almost the same issue as @thundermiracle and @JohnRDOrazio. I'm using the Autocomplete on some forms and with this behaviour, things goes a bit confusing for the users. One of my cases is on phone operators. If the user select an option, removes one letter and press that letter again, all values appears. It would be better if shows only the option that matches with that one that they selected. |
I have this problem as well. Use case
WorkaroundWith trial and error I found a solution that works as I want. But the fact it works seems quite coincidental. I'm using const WorkaroundComponent = () => {
const [value, setValue] = useState<string>("");
return (
<Autocomplete options={[{name: "aa"}, {name: "aab"}, {name: "baab"}, {name: "not interesting result"}]}
renderOption={({name}) => (name)}
getOptionLabel={({name}) => (name)}
value={null}
onBlur={() => {
let theValue = value;
setValue("");
setValue(theValue);
}}
inputValue={value}
onInputChange={((event, value) => setValue(value))}
renderInput={(params) => (
<TextField name="search"
margin="dense"
size="small"
variant="outlined"
label="Search"
type="text"
{...params}
/>
)}
/>
);
} Related issue/behaviorA related slightly surprising behavior is that the full result list also "sticks" to the selected text. Let's remove the workaround and just turn it into a controlled component: const RelatedIssueComponent = () => {
const [value, setValue] = useState<string>("");
return (
<Autocomplete options={[{name: "aa"}, {name: "aab"}, {name: "baab"}, {name: "not interesting result"}]}
renderOption={({name}) => (name)}
getOptionLabel={({name}) => (name)}
//value={null}
/*onBlur={() => {
let theValue = value;
setValue("");
setValue(theValue);
}}*/
inputValue={value}
onInputChange={((event, value) => setValue(value))}
renderInput={(params) => (
<TextField name="search"
margin="dense"
size="small"
variant="outlined"
label="Search"
type="text"
{...params}
/>
)}
/>
);
}
If you want to look at the filtered list you need to "trick" the cache by removing the full text and then retype it. |
@worldsayshi @pedrohenriiz @JohnRDOrazio |
@adirzoari until the maintainers decide that the current behaviour should be fixed, I am using this workaround:
You can see an example of my fix here: https://codesandbox.io/s/material-demo-forked-ir1o4?file=/demo.js Of course in your own use case you may need a different approach... |
May be this is happening due to some objects may have same labels. we have to pass a unique identifier, for this pass the getOptionKey prop in Autocomplete like this getOptionKey={(option) => option.hotelCode} in my case some objects may have same labels but each object has a unique hotelCode |
Current Behavior 😯
filterOptions is not correctly working after select option once.
Expected Behavior 🤔
after click to select the option, filter will still work fine.
Steps to Reproduce 🕹
And every sample in documentation has the same problem.
Your Environment 🌎
The text was updated successfully, but these errors were encountered: