Skip to content

Commit 3728555

Browse files
crisbetoandrewseguin
authored andcommitted
fix(datepicker): leaking backdropClick subscriptions (#8919)
Currently the datepicker re-subscribes to the same `backdropClicked` stream whenever the popup is attached, which causes it to stack up multiple subscriptions for every time the calendar is opened. These changes move the subscription so it only happens once.
1 parent 4d8c712 commit 3728555

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/lib/datepicker/datepicker.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,29 @@ describe('MatDatepicker', () => {
340340

341341
expect(() => fixture.detectChanges()).not.toThrow();
342342
});
343+
344+
it('should clear out the backdrop subscriptions on close', () => {
345+
for (let i = 0; i < 3; i++) {
346+
testComponent.datepicker.open();
347+
fixture.detectChanges();
348+
349+
testComponent.datepicker.close();
350+
fixture.detectChanges();
351+
}
352+
353+
testComponent.datepicker.open();
354+
fixture.detectChanges();
355+
356+
spyOn(testComponent.datepicker, 'close').and.callThrough();
357+
358+
const backdrop = document.querySelector('.cdk-overlay-backdrop')! as HTMLElement;
359+
360+
backdrop.click();
361+
fixture.detectChanges();
362+
363+
expect(testComponent.datepicker.close).toHaveBeenCalledTimes(1);
364+
expect(testComponent.datepicker.opened).toBe(false);
365+
});
343366
});
344367

345368
describe('datepicker with too many inputs', () => {

src/lib/datepicker/datepicker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,6 @@ export class MatDatepicker<D> implements OnDestroy {
352352
this._popupRef.updatePosition();
353353
});
354354
}
355-
356-
this._popupRef.backdropClick().subscribe(() => this.close());
357355
}
358356

359357
/** Create the popup. */
@@ -368,6 +366,7 @@ export class MatDatepicker<D> implements OnDestroy {
368366
});
369367

370368
this._popupRef = this._overlay.create(overlayConfig);
369+
this._popupRef.backdropClick().subscribe(() => this.close());
371370
}
372371

373372
/** Create the popup PositionStrategy. */

0 commit comments

Comments
 (0)