Skip to content

Commit

Permalink
fix(overlay): avoid same overlay being added to the keyboard event st…
Browse files Browse the repository at this point in the history
…ack multiple times (#12222)
  • Loading branch information
crisbeto authored and josephperrott committed Jul 18, 2018
1 parent 701a0dd commit 45d6ae4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/cdk/overlay/keyboard/overlay-keyboard-dispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

});


Expand Down
3 changes: 3 additions & 0 deletions src/cdk/overlay/keyboard/overlay-keyboard-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 45d6ae4

Please sign in to comment.