Skip to content

Commit 0e9c4f7

Browse files
authored
fix(material/autocomplete): ensure selected option updates correctly on backspace
Bug: Select an option from the autocomplete and close it. The input field is updated accordingly. Then click inside the input field and press backspace to update the input's value. The autocomplete panel opens, but the selected option is not updated yet. Pressing another backspace triggers the update. Problem and fix: When the `_handleInput` function is called, `panelOpen` is still `false`, causing the selected option to not update correctly. The `_openPanelInternal` function needs to be called to ensure the panel is open before processing the selected option logic.
1 parent f47f5f9 commit 0e9c4f7

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -529,18 +529,6 @@ export class MatAutocompleteTrigger
529529

530530
if (!value) {
531531
this._clearPreviousSelectedOption(null, false);
532-
} else if (this.panelOpen && !this.autocomplete.requireSelection) {
533-
// Note that we don't reset this when `requireSelection` is enabled,
534-
// because the option will be reset when the panel is closed.
535-
const selectedOption = this.autocomplete.options?.find(option => option.selected);
536-
537-
if (selectedOption) {
538-
const display = this._getDisplayValue(selectedOption.value);
539-
540-
if (value !== display) {
541-
selectedOption.deselect(false);
542-
}
543-
}
544532
}
545533

546534
if (this._canOpen() && this._document.activeElement === event.target) {
@@ -553,6 +541,20 @@ export class MatAutocompleteTrigger
553541
this._valueOnLastKeydown = null;
554542
this._openPanelInternal(valueOnAttach);
555543
}
544+
545+
if (value && this.panelOpen && !this.autocomplete.requireSelection) {
546+
// Note that we don't reset this when `requireSelection` is enabled,
547+
// because the option will be reset when the panel is closed.
548+
const selectedOption = this.autocomplete.options?.find(option => option.selected);
549+
550+
if (selectedOption) {
551+
const display = this._getDisplayValue(selectedOption.value);
552+
553+
if (value !== display) {
554+
selectedOption.deselect(false);
555+
}
556+
}
557+
}
556558
}
557559
}
558560

0 commit comments

Comments
 (0)