From 759b5357492a0b3dd05363f2bc7103acc4c66e52 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Sun, 26 Mar 2017 13:37:32 +0200 Subject: [PATCH] fix(dialog): unable to press escape to close in lazy-loaded module Fixes not being able to use the escape key to close a dialog that was opened from a lazy-loaded module. Fixes #3737. --- src/lib/dialog/dialog.spec.ts | 12 +++++++++++- src/lib/dialog/dialog.ts | 7 +++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/lib/dialog/dialog.spec.ts b/src/lib/dialog/dialog.spec.ts index 001564ea1dfb..b15ce7d7dd74 100644 --- a/src/lib/dialog/dialog.spec.ts +++ b/src/lib/dialog/dialog.spec.ts @@ -127,7 +127,6 @@ describe('MdDialog', () => { }); })); - it('should close a dialog via the escape key', async(() => { dialog.open(PizzaMsg, { viewContainerRef: testViewContainerRef @@ -553,6 +552,17 @@ describe('MdDialog with a parent MdDialog', () => { .toBe('', 'Expected closeAll on parent MdDialog to close dialog opened by child'); }); })); + + it('should close the top dialog via the escape key', async(() => { + childDialog.open(PizzaMsg); + + dispatchKeyboardEvent(document, 'keydown', ESCAPE); + fixture.detectChanges(); + + fixture.whenStable().then(() => { + expect(overlayContainerElement.querySelector('md-dialog-container')).toBeNull(); + }); + })); }); diff --git a/src/lib/dialog/dialog.ts b/src/lib/dialog/dialog.ts index 3cc1c57ebe67..1e27fc89c28a 100644 --- a/src/lib/dialog/dialog.ts +++ b/src/lib/dialog/dialog.ts @@ -64,7 +64,7 @@ export class MdDialog { let dialogRef = this._attachDialogContent(componentOrTemplateRef, dialogContainer, overlayRef, config); - if (!this._openDialogs.length && !this._parentDialog) { + if (!this._openDialogs.length) { document.addEventListener('keydown', this._boundKeydown); } @@ -210,10 +210,9 @@ export class MdDialog { */ private _handleKeydown(event: KeyboardEvent): void { let topDialog = this._openDialogs[this._openDialogs.length - 1]; + let canClose = topDialog ? !topDialog._containerInstance.dialogConfig.disableClose : false; - if (event.keyCode === ESCAPE && topDialog && - !topDialog._containerInstance.dialogConfig.disableClose) { - + if (event.keyCode === ESCAPE && canClose) { topDialog.close(); } }