Skip to content

feat(dialog): add disableClose option #1678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/lib/dialog/dialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export class MdDialogConfig {
/** The ARIA role of the dialog element. */
role: DialogRole = 'dialog';

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

// TODO(jelbourn): add configuration for size, lifecycle hooks, ARIA labelling.
}
17 changes: 9 additions & 8 deletions src/lib/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
Component,
ComponentRef,
ViewChild,
ViewEncapsulation,
NgZone,
OnDestroy
Component,
ComponentRef,
ViewChild,
ViewEncapsulation,
NgZone,
OnDestroy,
} from '@angular/core';
import {BasePortalHost, ComponentPortal, PortalHostDirective, TemplatePortal} from '../core';
import {MdDialogConfig} from './dialog-config';
Expand Down Expand Up @@ -74,8 +74,9 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {

/** Handles the user pressing the Escape key. */
handleEscapeKey() {
// TODO(jelbourn): add MdDialogConfig option to disable this behavior.
this.dialogRef.close();
if (!this.dialogConfig.disableClose) {
this.dialogRef.close();
}
}

ngOnDestroy() {
Expand Down
37 changes: 36 additions & 1 deletion src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,47 @@ describe('MdDialog', () => {

viewContainerFixture.detectChanges();

let backdrop = <HTMLElement> overlayContainerElement.querySelector('.md-overlay-backdrop');
let backdrop = overlayContainerElement.querySelector('.md-overlay-backdrop') as HTMLElement;
backdrop.click();

expect(overlayContainerElement.querySelector('md-dialog-container')).toBeFalsy();
});

describe('disableClose option', () => {
it('should prevent closing via clicks on the backdrop', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
config.disableClose = true;

dialog.open(PizzaMsg, config);

viewContainerFixture.detectChanges();

let backdrop = overlayContainerElement.querySelector('.md-overlay-backdrop') as HTMLElement;
backdrop.click();

expect(overlayContainerElement.querySelector('md-dialog-container')).toBeTruthy();
});

it('should prevent closing via the escape key', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
config.disableClose = true;

dialog.open(PizzaMsg, config);

viewContainerFixture.detectChanges();

let dialogContainer: MdDialogContainer = viewContainerFixture.debugElement.query(
By.directive(MdDialogContainer)).componentInstance;

// Fake the user pressing the escape key by calling the handler directly.
dialogContainer.handleEscapeKey();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this comment here as well:

// Fake the user pressing the escape key by calling the handler directly.


expect(overlayContainerElement.querySelector('md-dialog-container')).toBeTruthy();
});
});

describe('focus management', () => {

// When testing focus, all of the elements must be in the DOM.
Expand Down
6 changes: 4 additions & 2 deletions src/lib/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ export class MdDialog {
// to modify and close it.
let dialogRef = <MdDialogRef<T>> new MdDialogRef(overlayRef);

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

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