Skip to content

Commit

Permalink
fix(material/autocomplete): regression in requireSelection when optio…
Browse files Browse the repository at this point in the history
…ns are filtered

In #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 #28113.
  • Loading branch information
crisbeto committed Nov 14, 2023
1 parent 6875bb6 commit 87bbae4
Showing 1 changed file with 1 addition and 7 deletions.
8 changes: 1 addition & 7 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,6 @@ export class MatAutocompleteTrigger
// of the available options,
// - if a valid string is entered after an invalid one.
if (this.panelOpen) {
this._captureValueOnAttach();
this._emitOpened();
} else {
this.autocomplete.closed.emit();
Expand All @@ -626,11 +625,6 @@ export class MatAutocompleteTrigger
this.autocomplete.opened.emit();
}

/** Intended to be called when the panel is attached. Captures the current value of the input. */
private _captureValueOnAttach() {
this._valueOnAttach = this._element.nativeElement.value;
}

/** Destroys the autocomplete suggestion panel. */
private _destroyPanel(): void {
if (this._overlayRef) {
Expand Down Expand Up @@ -744,6 +738,7 @@ export class MatAutocompleteTrigger

if (overlayRef && !overlayRef.hasAttached()) {
overlayRef.attach(this._portal);
this._valueOnAttach = this._element.nativeElement.value;
this._closingActionsSubscription = this._subscribeToClosingActions();
}

Expand All @@ -753,7 +748,6 @@ export class MatAutocompleteTrigger
this.autocomplete._setColor(this._formField?.color);
this._updatePanelState();
this._applyModalPanelOwnership();
this._captureValueOnAttach();

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

0 comments on commit 87bbae4

Please sign in to comment.