Skip to content

Commit 86ad515

Browse files
authored
fix(material/select): avoid error if panel is closed too quickly (#30408)
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`.
1 parent 0776acc commit 86ad515

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/material/select/select.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,26 @@ describe('MatSelect', () => {
14571457
expect(() => fixture.componentInstance.select.open()).not.toThrow();
14581458
});
14591459

1460+
it('should not throw when closing too quickly after opening', () => {
1461+
// Need to recreate the testing module, because the issue we're
1462+
// testing for only happens when animations are enabled.
1463+
TestBed.resetTestingModule();
1464+
TestBed.configureTestingModule({
1465+
imports: [MatFormFieldModule, MatSelectModule],
1466+
declarations: [BasicSelect],
1467+
});
1468+
1469+
fixture = TestBed.createComponent(BasicSelect);
1470+
fixture.detectChanges();
1471+
const select = fixture.componentInstance.select;
1472+
1473+
expect(() => {
1474+
select.open();
1475+
select.close();
1476+
}).not.toThrow();
1477+
expect(select.panelOpen).toBe(false);
1478+
});
1479+
14601480
it('should open the panel when trigger is clicked', fakeAsync(() => {
14611481
trigger.click();
14621482
fixture.detectChanges();

src/material/select/select.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ export class MatSelect
844844

845845
/** Triggers the exit animation and detaches the overlay at the end. */
846846
private _exitAndDetach() {
847-
if (this._animationsDisabled) {
847+
if (this._animationsDisabled || !this.panel) {
848848
this._overlayDir.detachOverlay();
849849
return;
850850
}

0 commit comments

Comments
 (0)