Skip to content

Commit

Permalink
fix(dialog): unable to press escape to close in lazy-loaded module
Browse files Browse the repository at this point in the history
Fixes not being able to use the escape key to close a dialog that was opened from a lazy-loaded module.

Fixes angular#3737.
  • Loading branch information
crisbeto committed Mar 26, 2017
1 parent a25e7bb commit 759b535
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ describe('MdDialog', () => {
});
}));


it('should close a dialog via the escape key', async(() => {
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
Expand Down Expand Up @@ -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();
});
}));
});


Expand Down
7 changes: 3 additions & 4 deletions src/lib/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}
}
Expand Down

0 comments on commit 759b535

Please sign in to comment.