Skip to content

Commit

Permalink
fix(material/select): avoid error if panel is closed too quickly (#30408
Browse files Browse the repository at this point in the history
)

Fixes a regression after #30363 where the select panel throws an error if `open` and `close` are called before Angular has had a chance to resolve the `panel`.
  • Loading branch information
crisbeto authored Jan 28, 2025
1 parent 0776acc commit 86ad515
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,26 @@ describe('MatSelect', () => {
expect(() => fixture.componentInstance.select.open()).not.toThrow();
});

it('should not throw when closing too quickly after opening', () => {
// Need to recreate the testing module, because the issue we're
// testing for only happens when animations are enabled.
TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [MatFormFieldModule, MatSelectModule],
declarations: [BasicSelect],
});

fixture = TestBed.createComponent(BasicSelect);
fixture.detectChanges();
const select = fixture.componentInstance.select;

expect(() => {
select.open();
select.close();
}).not.toThrow();
expect(select.panelOpen).toBe(false);
});

it('should open the panel when trigger is clicked', fakeAsync(() => {
trigger.click();
fixture.detectChanges();
Expand Down
2 changes: 1 addition & 1 deletion src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ export class MatSelect

/** Triggers the exit animation and detaches the overlay at the end. */
private _exitAndDetach() {
if (this._animationsDisabled) {
if (this._animationsDisabled || !this.panel) {
this._overlayDir.detachOverlay();
return;
}
Expand Down

0 comments on commit 86ad515

Please sign in to comment.