Skip to content

Commit 13629b0

Browse files
authored
fix(material/autocomplete): regression in requireSelection when options are filtered (angular#28119)
In angular#27781 the call to capture the input value was moved into the root of `_attachOverlay` in order to capture it even when there are no options. This is problematic, because `_attachOverlay` is called as the user is typing which basically breaks the `requireSelection` feature. These changes resolve the issue while preserving the original fix by only capturing the value when the overlay is actually attached. Fixes angular#28113.
1 parent 6e29584 commit 13629b0

File tree

2 files changed

+2
-8
lines changed

2 files changed

+2
-8
lines changed

src/material/autocomplete/autocomplete-trigger.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,6 @@ export class MatAutocompleteTrigger
600600
// of the available options,
601601
// - if a valid string is entered after an invalid one.
602602
if (this.panelOpen) {
603-
this._captureValueOnAttach();
604603
this._emitOpened();
605604
} else {
606605
this.autocomplete.closed.emit();
@@ -626,11 +625,6 @@ export class MatAutocompleteTrigger
626625
this.autocomplete.opened.emit();
627626
}
628627

629-
/** Intended to be called when the panel is attached. Captures the current value of the input. */
630-
private _captureValueOnAttach() {
631-
this._valueOnAttach = this._element.nativeElement.value;
632-
}
633-
634628
/** Destroys the autocomplete suggestion panel. */
635629
private _destroyPanel(): void {
636630
if (this._overlayRef) {
@@ -744,6 +738,7 @@ export class MatAutocompleteTrigger
744738

745739
if (overlayRef && !overlayRef.hasAttached()) {
746740
overlayRef.attach(this._portal);
741+
this._valueOnAttach = this._element.nativeElement.value;
747742
this._closingActionsSubscription = this._subscribeToClosingActions();
748743
}
749744

@@ -753,7 +748,6 @@ export class MatAutocompleteTrigger
753748
this.autocomplete._setColor(this._formField?.color);
754749
this._updatePanelState();
755750
this._applyModalPanelOwnership();
756-
this._captureValueOnAttach();
757751

758752
// We need to do an extra `panelOpen` check in here, because the
759753
// autocomplete won't be shown if there are no options.

tools/public_api_guard/material/autocomplete.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, AfterViewIn
190190
// (undocumented)
191191
_handleKeydown(event: KeyboardEvent): void;
192192
// (undocumented)
193-
static ngAcceptInputType_autocompleteDisabled: unknown /** Handles keyboard events coming from the overlay panel. */;
193+
static ngAcceptInputType_autocompleteDisabled: unknown;
194194
// (undocumented)
195195
ngAfterViewInit(): void;
196196
// (undocumented)

0 commit comments

Comments
 (0)