Repository navigation
docs(material/autocomplete): keep selected option in optgroup example - #34232
Open
DuncanFaulkner wants to merge 1 commit into
Open
DuncanFaulkner wants to merge 1 commit into
DuncanFaulkner wants to merge 1 commit into
Conversation
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
_filterGroupreturns new group objects whenever the input value changes, and the template tracked groups withtrack group. Selecting an option changes the value, so everymat-optgroupandmat-optionwas destroyed and created again. The autocomplete keeps the selected state on theMatOptioninstance, 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.aria-selected="true"track group(before)track group.letter(after)track name@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-selectworks out the selected option from its value, but the autocomplete trigger only remembers theMatOptionthat was clicked.MatAutocompleteTriggercould pick the option that matches the control value again whenautocomplete.optionschanges. That would fix these apps without them changing theirtrackexpressions. It would change long-standing behaviour, though, and options with object values would need a way to compare them (the autocomplete has nocompareWith). Would you want that as a change? If so, I can open a separate PR for it.