Skip to content

Commit 13ebd1b

Browse files
committed
feat(dialog): add disableClose option
Adds a config option that allows users to disable closing a dialog via a backdrop click or pressing escape. Fixes #1419.
1 parent 96d196a commit 13ebd1b

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

src/lib/dialog/dialog-config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export class MdDialogConfig {
1414
/** The ARIA role of the dialog element. */
1515
role: DialogRole = 'dialog';
1616

17-
// TODO(jelbourn): add configuration for size, clickOutsideToClose, lifecycle hooks,
18-
// ARIA labelling.
17+
/** Whether the user can use escape or clicking outside to close a modal. */
18+
disableClose = false;
19+
20+
// TODO(jelbourn): add configuration for size, lifecycle hooks, ARIA labelling.
1921
}

src/lib/dialog/dialog-container.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {
2-
Component,
3-
ComponentRef,
4-
ViewChild,
5-
ViewEncapsulation,
6-
NgZone,
7-
OnDestroy
2+
Component,
3+
ComponentRef,
4+
ViewChild,
5+
ViewEncapsulation,
6+
NgZone,
7+
OnDestroy,
8+
ElementRef
89
} from '@angular/core';
910
import {BasePortalHost, ComponentPortal, PortalHostDirective, TemplatePortal} from '../core';
1011
import {MdDialogConfig} from './dialog-config';
@@ -25,7 +26,7 @@ import 'rxjs/add/operator/first';
2526
host: {
2627
'class': 'md-dialog-container',
2728
'[attr.role]': 'dialogConfig?.role',
28-
'(keydown.escape)': 'handleEscapeKey()',
29+
'(keydown.escape)': 'handleEscapeKey()'
2930
},
3031
encapsulation: ViewEncapsulation.None,
3132
})
@@ -45,7 +46,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
4546
/** Reference to the open dialog. */
4647
dialogRef: MdDialogRef<any>;
4748

48-
constructor(private _ngZone: NgZone) {
49+
constructor(private _ngZone: NgZone, public elementRef: ElementRef) {
4950
super();
5051
}
5152

@@ -74,8 +75,9 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
7475

7576
/** Handles the user pressing the Escape key. */
7677
handleEscapeKey() {
77-
// TODO(jelbourn): add MdDialogConfig option to disable this behavior.
78-
this.dialogRef.close();
78+
if (!this.dialogConfig.disableClose) {
79+
this.dialogRef.close();
80+
}
7981
}
8082

8183
ngOnDestroy() {

src/lib/dialog/dialog.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,40 @@ describe('MdDialog', () => {
128128
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeFalsy();
129129
});
130130

131+
describe('disableClose option', () => {
132+
it('should prevent closing via clicks on the backdrop', () => {
133+
let config = new MdDialogConfig();
134+
config.viewContainerRef = testViewContainerRef;
135+
config.disableClose = true;
136+
137+
dialog.open(PizzaMsg, config);
138+
139+
viewContainerFixture.detectChanges();
140+
141+
let backdrop = <HTMLElement> overlayContainerElement.querySelector('.md-overlay-backdrop');
142+
backdrop.click();
143+
144+
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeTruthy();
145+
});
146+
147+
it('should prevent closing via the escape key', () => {
148+
let config = new MdDialogConfig();
149+
config.viewContainerRef = testViewContainerRef;
150+
config.disableClose = true;
151+
152+
dialog.open(PizzaMsg, config);
153+
154+
viewContainerFixture.detectChanges();
155+
156+
let dialogContainer: MdDialogContainer = viewContainerFixture.debugElement.query(
157+
By.directive(MdDialogContainer)).componentInstance;
158+
159+
dialogContainer.handleEscapeKey();
160+
161+
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeTruthy();
162+
});
163+
});
164+
131165
describe('focus management', () => {
132166

133167
// When testing focus, all of the elements must be in the DOM.

src/lib/dialog/dialog.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ export class MdDialog {
8787
// to modify and close it.
8888
let dialogRef = <MdDialogRef<T>> new MdDialogRef(overlayRef);
8989

90-
// When the dialog backdrop is clicked, we want to close it.
91-
overlayRef.backdropClick().first().subscribe(() => dialogRef.close());
90+
if (!dialogContainer.dialogConfig.disableClose) {
91+
// When the dialog backdrop is clicked, we want to close it.
92+
overlayRef.backdropClick().first().subscribe(() => dialogRef.close());
93+
}
9294

9395
// Set the dialogRef to the container so that it can use the ref to close the dialog.
9496
dialogContainer.dialogRef = dialogRef;

0 commit comments

Comments
 (0)