mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(overlay): avoid same overlay being added to the keyboard event stack multiple times (#12222)
This commit is contained in:
committed by
Joey Perrott
parent
701a0dd10e
commit
45d6ae4a13
@@ -157,6 +157,27 @@ describe('OverlayKeyboardDispatcher', () => {
|
||||
expect(overlayOneSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not add the same overlay to the stack multiple times', () => {
|
||||
const overlayOne = overlay.create();
|
||||
const overlayTwo = overlay.create();
|
||||
const overlayOneSpy = jasmine.createSpy('overlayOne keyboard event spy');
|
||||
const overlayTwoSpy = jasmine.createSpy('overlayTwo keyboard event spy');
|
||||
|
||||
overlayOne.keydownEvents().subscribe(overlayOneSpy);
|
||||
overlayTwo.keydownEvents().subscribe(overlayTwoSpy);
|
||||
|
||||
keyboardDispatcher.add(overlayOne);
|
||||
keyboardDispatcher.add(overlayTwo);
|
||||
keyboardDispatcher.add(overlayOne);
|
||||
|
||||
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
|
||||
|
||||
expect(keyboardDispatcher._attachedOverlays).toEqual([overlayTwo, overlayOne]);
|
||||
|
||||
expect(overlayTwoSpy).not.toHaveBeenCalled();
|
||||
expect(overlayOneSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -42,6 +42,9 @@ export class OverlayKeyboardDispatcher implements OnDestroy {
|
||||
|
||||
/** Add a new overlay to the list of attached overlay refs. */
|
||||
add(overlayRef: OverlayRef): void {
|
||||
// Ensure that we don't get the same overlay multiple times.
|
||||
this.remove(overlayRef);
|
||||
|
||||
// Lazily start dispatcher once first overlay is added
|
||||
if (!this._isAttached) {
|
||||
this._document.body.addEventListener('keydown', this._keydownListener, true);
|
||||
|
||||
Reference in New Issue
Block a user