Skip to content

[Autocomplete] Invalid highlighted index when selecting last item with filterSelectedOptions #19637

@jonatanklosko

Description

@jonatanklosko

Current Behavior 😯

When filterSelectedOptions is enabled, selecting the last item means it disappears from the list, yet the highlight is not updated.

Expected Behavior 🤔

Not entirely sure, I can imagine one of the following:

  • clear highlight (i.e. set highlighted index to -1)
  • highlight the previous item (edge case: when the list has only 1 element, then the index should be set to -1)

Steps to Reproduce 🕹

Steps:

  1. Open this demo (taken directly from the documentation).
  2. Select the last item and hit enter.
  3. Hit arrow down (to open the popup) and then hit enter again (the highlighted index is still the old end-of-list and thus the selected value is undefined).
Tech Version
Material-UI v4.9.2

Notes

One simple solution is to reset the highlighted index when a new value is selected and filterSelectedOptions is used. Adding the following code

if (filterSelectedOptions) {
  setHighlightedIndex(-1);
}

below this: https://github.com/mui-org/material-ui/blob/f567b384f509d36b7e10f691e6b58538e7db8869/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js#L453-L456
does the job, although as mentioned above, it's not necessarily the expected behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGreat for first contributions. Enable to learn the contribution process.scope: autocompleteChanges related to the autocomplete. This includes ComboBox.type: bugIt doesn't behave as expected.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions