Skip to content

Commit

Permalink
fix(material/autocomplete): panel not visible when opened from multip…
Browse files Browse the repository at this point in the history
…le triggers (#28843)

Fixes that if one panel is tied to multiple triggers, it ends up becoming invisible because the original trigger resets the state after the next trigger picks it up. These changes avoid the state reset if the trigger has changed.

Fixes #28362.

(cherry picked from commit 2f17c69)
  • Loading branch information
crisbeto committed Apr 15, 2024
1 parent cbe0a78 commit 80437d8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,14 @@ export class MatAutocompleteTrigger
});
}

this.autocomplete._isOpen = this._overlayAttached = false;
// Only reset if this trigger is the latest one that opened the
// autocomplete since another may have taken it over.
if (this.autocomplete._latestOpeningTrigger === this) {
this.autocomplete._isOpen = false;
this.autocomplete._latestOpeningTrigger = null;
}

this._overlayAttached = false;
this._pendingAutoselectedOption = null;

if (this._overlayRef && this._overlayRef.hasAttached()) {
Expand All @@ -331,8 +338,7 @@ export class MatAutocompleteTrigger

// Remove aria-owns attribute when the autocomplete is no longer visible.
if (this._trackedModal) {
const panelId = this.autocomplete.id;
removeAriaReferencedId(this._trackedModal, 'aria-owns', panelId);
removeAriaReferencedId(this._trackedModal, 'aria-owns', this.autocomplete.id);
}
}

Expand Down Expand Up @@ -786,6 +792,7 @@ export class MatAutocompleteTrigger
const wasOpen = this.panelOpen;

this.autocomplete._isOpen = this._overlayAttached = true;
this.autocomplete._latestOpeningTrigger = this;
this.autocomplete._setColor(this._formField?.color);
this._updatePanelState();
this._applyModalPanelOwnership();
Expand Down
3 changes: 3 additions & 0 deletions src/material/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export class MatAutocomplete implements AfterContentInit, OnDestroy {
}
_isOpen: boolean = false;

/** Latest trigger that opened the autocomplete. */
_latestOpeningTrigger: unknown;

/** @docs-private Sets the theme color of the panel. */
_setColor(value: ThemePalette) {
this._color = value;
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/autocomplete.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class MatAutocomplete implements AfterContentInit, OnDestroy {
// (undocumented)
_isOpen: boolean;
_keyManager: ActiveDescendantKeyManager<MatOption>;
_latestOpeningTrigger: unknown;
// (undocumented)
static ngAcceptInputType_autoActiveFirstOption: unknown;
// (undocumented)
Expand Down

0 comments on commit 80437d8

Please sign in to comment.