Material-ui: [Autocomplete] Don't display whole options list in Autocomplete when input text is the same as selected value

Created on 28 Sep 2020  路  11Comments  路  Source: mui-org/material-ui

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Summary 馃挕

When one of the options in Autocomplete is selected, there is a slightly non-natural behavior when manipulating input text. After deleting e.g. the last letter, the list with possible options is filtered by input text. When you type this deleted letter again, the whole list with all options is displayed. Later, if you add another letter, filtering takes effect again.

Examples 馃寛

You can try it on the first demo (Combo box) on Autocomplete page (https://material-ui.com/components/autocomplete/).

  1. Type the beginning of one of the options, e.g. "Pulp": Screen Shot 2020-09-28 at 3 19 54 PM

  2. Hit enter or choose this option by clicking: Screen Shot 2020-09-28 at 3.24.43 PM

  3. Hit backspace to remove last letter (now filtering is active): Screen Shot 2020-09-28 at 3 27 05 PM

  4. Type "n" (now filtering is not active): Screen Shot 2020-09-28 at 3 30 16 PM

  5. Type any letter (now filtering is active again): Screen Shot 2020-09-28 at 3 33 01 PM

Motivation 馃敠


Displaying the whole list of options with selected value highlighted looks right when you open the list with mouse click, but behavior described above looks a little confusing and may be recognized as a bug.

bug 馃悰 Autocomplete good to take

All 11 comments

@eps1lon has anybody looked at this? I feel like it was somehow unnoticed as new issues are getting responses and this one not.

Can I try to solve this or are you already working on it? :) @k-rokicki

@montelius go ahead, and thanks for volunteering!

@montelius All yours. Please let us know if you no longer intend to work on this issue. If you're stuck at any point feel free to open a PR so a maintainer can take a look.

@eps1lon So I've figured out why this bug exists, but I'm not sure how to solve it. Here's the problem:

When an option is selected, the listbox of options is closed. When you either focus on the textbox again, or click the arrow down button, the entire list of options is shown, not just the option matching the input. That makes sense in that case, because the user probably wants to change the selected option and should be shown all options. That is controlled by this code in useAutocomplete.js:

https://github.com/mui-org/material-ui/blob/d9d60e615470d78a721f5d392a84aaf892637a17/packages/material-ui/src/useAutocomplete/useAutocomplete.js#L180-L181

and this code, specifically line 200:

https://github.com/mui-org/material-ui/blob/d9d60e615470d78a721f5d392a84aaf892637a17/packages/material-ui/src/useAutocomplete/useAutocomplete.js#L185-L202

The problem is that this case also applies to the case described in the issue, which does not make sense for the user. When an option is selected, it continues to be selected even though you remove the last letter, so when you type in the last letter again, inputValueIsSelectedValue in the code above becomes true.

So basically, you would probably want to make inputValueIsSelectedValue false if useAutocomplete was called because of a new keyboard input, and not because the textbox/arrow down button was clicked. However, I can't find a reasonable way to access that information from inside useAutocomplete. Can I get any help on how to solve this?

Alternatively, another possible solution is that an option stops being selected if you remove a letter from the input.

Thanks in advance!

Could you codify your expected behavior into a test or user interactions? You could open a PR with just the test and then we can collaborate on a potential fix.

@eps1lon Sounds good, thanks!

A proposed solution: we could create a new state that keep track of the pristine state of the input value. It's reset each time the popup open. When the input is pristine, we don't filter the options for the current value.

@oliviertassinari Yes, that is a solution! Should the state be represented as a prop passed to useAutocomplete?

@montelius from my perspective, the state should be internal to the useAutocomplete hook.

@oliviertassinari Sure, will look into it! :)

Was this page helpful?
0 / 5 - 0 ratings