Skip to content

feat(dialog): allow for a config object to be passed on init #1679

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 1 commit into from
Nov 9, 2016
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
2 changes: 1 addition & 1 deletion src/lib/dialog/dialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class MdDialogConfig {
role?: DialogRole = 'dialog';

/** Whether the user can use escape or clicking outside to close a modal. */
disableClose = false;
disableClose?: boolean = false;

// TODO(jelbourn): add configuration for size, lifecycle hooks, ARIA labelling.
}
65 changes: 27 additions & 38 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {By} from '@angular/platform-browser';
import {NgModule, Component, Directive, ViewChild, ViewContainerRef} from '@angular/core';
import {MdDialog, MdDialogModule} from './dialog';
import {OverlayContainer} from '../core';
import {MdDialogConfig} from './dialog-config';
import {MdDialogRef} from './dialog-ref';
import {MdDialogContainer} from './dialog-container';

Expand Down Expand Up @@ -48,10 +47,9 @@ describe('MdDialog', () => {
});

it('should open a dialog with a component', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;

let dialogRef = dialog.open(PizzaMsg, config);
let dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand Down Expand Up @@ -79,11 +77,7 @@ describe('MdDialog', () => {
});

it('should apply the configured role to the dialog element', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
config.role = 'alertdialog';

dialog.open(PizzaMsg, config);
dialog.open(PizzaMsg, { role: 'alertdialog' });

viewContainerFixture.detectChanges();

Expand All @@ -92,10 +86,9 @@ describe('MdDialog', () => {
});

it('should close a dialog and get back a result', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;

let dialogRef = dialog.open(PizzaMsg, config);
let dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand All @@ -112,10 +105,9 @@ describe('MdDialog', () => {


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

dialog.open(PizzaMsg, config);
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand All @@ -129,10 +121,9 @@ describe('MdDialog', () => {
});

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

dialog.open(PizzaMsg, config);
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand All @@ -144,11 +135,10 @@ describe('MdDialog', () => {

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);
dialog.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand All @@ -159,11 +149,10 @@ describe('MdDialog', () => {
});

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

dialog.open(PizzaMsg, config);
dialog.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand All @@ -189,10 +178,10 @@ describe('MdDialog', () => {
});

it('should focus the first tabbable element of the dialog on open', fakeAsync(() => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

dialog.open(PizzaMsg, config);
viewContainerFixture.detectChanges();
flushMicrotasks();

Expand All @@ -207,10 +196,10 @@ describe('MdDialog', () => {
document.body.appendChild(button);
button.focus();

let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
let dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

let dialogRef = dialog.open(PizzaMsg, config);
viewContainerFixture.detectChanges();
flushMicrotasks();

Expand Down
13 changes: 12 additions & 1 deletion src/lib/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export class MdDialog {
* @param component Type of the component to load into the load.
* @param config
*/
open<T>(component: ComponentType<T>, config = new MdDialogConfig()): MdDialogRef<T> {
open<T>(component: ComponentType<T>, config?: MdDialogConfig): MdDialogRef<T> {
config = this._applyConfigDefaults(config);

let overlayRef = this._createOverlay(config);
let dialogContainer = this._attachDialogContainer(overlayRef, config);

Expand Down Expand Up @@ -125,6 +127,15 @@ export class MdDialog {

return state;
}

/**
* Applies default options to the dialog config.
* @param dialogConfig Config to be modified.
* @returns The new configuration object.
Copy link
Member

@devversion devversion Nov 3, 2016

Choose a reason for hiding this comment

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

Are the JSDoc types necessary here? I think the TS types should be enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Removed the types and renamed the argument to match the other methods.

*/
private _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
return Object.assign(new MdDialogConfig(), dialogConfig);
}
}


Expand Down