Skip to content
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

feat(dialog): add escapeToClose config option #1682

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions src/lib/dialog/dialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class MdDialogConfig {
/** The ARIA role of the dialog element. */
role: DialogRole = 'dialog';

/** Whether the escape key should close the dialog. */
escapeToClose = true;
Copy link
Member

Choose a reason for hiding this comment

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

Let's just make this disableClose and have it apply to both escape and backdrop click, since you're unlikely to want to allow one but not the other.


// TODO(jelbourn): add configuration for size, clickOutsideToClose, lifecycle hooks,
// ARIA labelling.
}
5 changes: 3 additions & 2 deletions src/lib/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
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.escapeToClose) {
this.dialogRef.close();
}
}

ngOnDestroy() {
Expand Down
40 changes: 30 additions & 10 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,43 @@ describe('MdDialog', () => {
});


it('should close a dialog via the escape key', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
describe('escape key functionality', () => {
it('should close a dialog via the escape key', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;

dialog.open(PizzaMsg, config);
dialog.open(PizzaMsg, config);

viewContainerFixture.detectChanges();
viewContainerFixture.detectChanges();

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

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

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

it('should allow disabling the close via the escape key', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
config.escapeToClose = false;

dialog.open(PizzaMsg, config);

viewContainerFixture.detectChanges();

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

dialogContainer.handleEscapeKey();

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


it('should close when clicking on the overlay backdrop', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
Expand Down