Skip to content

Commit 3796f69

Browse files
crisbetojelbourn
authored andcommitted
fix(dialog): unable to press escape to close in lazy-loaded module (#3788)
Fixes not being able to use the escape key to close a dialog that was opened from a lazy-loaded module. Fixes #3737.
1 parent 033fe8d commit 3796f69

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/lib/dialog/dialog.spec.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ describe('MdDialog', () => {
127127
});
128128
}));
129129

130-
131130
it('should close a dialog via the escape key', async(() => {
132131
dialog.open(PizzaMsg, {
133132
viewContainerRef: testViewContainerRef
@@ -585,6 +584,17 @@ describe('MdDialog with a parent MdDialog', () => {
585584
.toBe('', 'Expected closeAll on parent MdDialog to close dialog opened by child');
586585
});
587586
}));
587+
588+
it('should close the top dialog via the escape key', async(() => {
589+
childDialog.open(PizzaMsg);
590+
591+
dispatchKeyboardEvent(document, 'keydown', ESCAPE);
592+
fixture.detectChanges();
593+
594+
fixture.whenStable().then(() => {
595+
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeNull();
596+
});
597+
}));
588598
});
589599

590600

src/lib/dialog/dialog.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class MdDialog {
6565
let dialogRef =
6666
this._attachDialogContent(componentOrTemplateRef, dialogContainer, overlayRef, config);
6767

68-
if (!this._openDialogs.length && !this._parentDialog) {
68+
if (!this._openDialogs.length) {
6969
document.addEventListener('keydown', this._boundKeydown);
7070
}
7171

@@ -199,10 +199,9 @@ export class MdDialog {
199199
*/
200200
private _handleKeydown(event: KeyboardEvent): void {
201201
let topDialog = this._openDialogs[this._openDialogs.length - 1];
202+
let canClose = topDialog ? !topDialog._containerInstance.dialogConfig.disableClose : false;
202203

203-
if (event.keyCode === ESCAPE && topDialog &&
204-
!topDialog._containerInstance.dialogConfig.disableClose) {
205-
204+
if (event.keyCode === ESCAPE && canClose) {
206205
topDialog.close();
207206
}
208207
}

0 commit comments

Comments
 (0)