Skip to content
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

fix(material/autocomplete): ensure selected option updates correctly on backspace #30074

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,18 +529,6 @@ export class MatAutocompleteTrigger

if (!value) {
this._clearPreviousSelectedOption(null, false);
} else if (this.panelOpen && !this.autocomplete.requireSelection) {
// Note that we don't reset this when `requireSelection` is enabled,
// because the option will be reset when the panel is closed.
const selectedOption = this.autocomplete.options?.find(option => option.selected);

if (selectedOption) {
const display = this._getDisplayValue(selectedOption.value);

if (value !== display) {
selectedOption.deselect(false);
}
}
}

if (this._canOpen() && this._document.activeElement === event.target) {
Expand All @@ -553,6 +541,20 @@ export class MatAutocompleteTrigger
this._valueOnLastKeydown = null;
this._openPanelInternal(valueOnAttach);
}

if (value && this.panelOpen && !this.autocomplete.requireSelection) {
// Note that we don't reset this when `requireSelection` is enabled,
// because the option will be reset when the panel is closed.
const selectedOption = this.autocomplete.options?.find(option => option.selected);

if (selectedOption) {
const display = this._getDisplayValue(selectedOption.value);

if (value !== display) {
selectedOption.deselect(false);
}
}
}
}
}

Expand Down
Loading