Skip to content

docs(material/autocomplete): keep selected option in optgroup example - #34232

Open
DuncanFaulkner wants to merge 1 commit into
angular:mainfrom
DuncanFaulkner:docs/autocomplete-optgroup-track
Open

DuncanFaulkner wants to merge 1 commit into
angular:mainfrom
DuncanFaulkner:docs/autocomplete-optgroup-track

Conversation

@DuncanFaulkner

Copy link
Copy Markdown
Contributor

Fixes #29189.

The autocomplete optgroup example lost the selected option. When you picked a state and opened the panel again, the option had no check mark and no aria-selected="true".

The cause is in the example. _filterGroup returns new group objects whenever the input value changes, and the template tracked groups with track group. Selecting an option changes the value, so every mat-optgroup and mat-option was destroyed and created again. The autocomplete keeps the selected state on the MatOption instance, so the new "Alaska" option started out unselected. The flat examples don't have the problem because they track options by their string value.

This PR changes the example to track group.letter. I tested it in Chrome with Material 22.2.7: select "Alaska", then click the input again.

Example Selected after reopening Check mark aria-selected="true"
Optgroup, track group (before) No No No
Optgroup, track group.letter (after) Yes Yes Yes
Flat options, track name Yes Yes Yes

@crisbeto, I'd like your view on a component change as well. Apps can hit the same thing whenever their options get created again, for example when a filter returns new objects or the options come from a fresh server response. mat-select works out the selected option from its value, but the autocomplete trigger only remembers the MatOption that was clicked. MatAutocompleteTrigger could pick the option that matches the control value again when autocomplete.options changes. That would fix these apps without them changing their track expressions. It would change long-standing behaviour, though, and options with object values would need a way to compare them (the autocomplete has no compareWith). Would you want that as a change? If so, I can open a separate PR for it.

The optgroup example builds new group objects whenever the input value
changes and tracked the groups by object identity. Selecting an option
changes the value, so every mat-optgroup and mat-option was destroyed and
created again. The selected state lives on the MatOption instance, so the
new option for the selected value was not marked as selected and had no
check mark or aria-selected.

Track the groups by their letter so the options are kept.

Fixes angular#29189.
@angular-robot angular-robot Bot added area: docs Related to the documentation area: material/autocomplete labels Dec 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: docs Related to the documentation area: material/autocomplete

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(mat-autocomplete): missing selection indicator (highlight + check icon) when using mat-autocomplete with mat-optgroup

1 participant