Skip to content

Commit cd8b25e

Browse files
committed
feat(dialog): allow for an object literal to be passed on init
Adds the ability to pass in an object literal, that matches the signature of `MdDialogConfig`, when opening a dialog.
1 parent 1e86066 commit cd8b25e

File tree

4 files changed

+44
-36
lines changed

4 files changed

+44
-36
lines changed

src/demo-app/dialog/dialog-demo.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, ViewContainerRef} from '@angular/core';
2-
import {MdDialog, MdDialogConfig, MdDialogRef} from '@angular/material';
2+
import {MdDialog, MdDialogRef} from '@angular/material';
33

44
@Component({
55
moduleId: module.id,
@@ -16,10 +16,9 @@ export class DialogDemo {
1616
public viewContainerRef: ViewContainerRef) { }
1717

1818
open() {
19-
let config = new MdDialogConfig();
20-
config.viewContainerRef = this.viewContainerRef;
21-
22-
this.dialogRef = this.dialog.open(JazzDialog, config);
19+
this.dialogRef = this.dialog.open(JazzDialog, {
20+
viewContainerRef: this.viewContainerRef
21+
});
2322

2423
this.dialogRef.afterClosed().subscribe(result => {
2524
this.lastCloseResult = result;

src/lib/dialog/dialog-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class MdDialogConfig {
1212
viewContainerRef: ViewContainerRef;
1313

1414
/** The ARIA role of the dialog element. */
15-
role: DialogRole = 'dialog';
15+
role?: DialogRole;
1616

1717
// TODO(jelbourn): add configuration for size, clickOutsideToClose, lifecycle hooks,
1818
// ARIA labelling.

src/lib/dialog/dialog.spec.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {By} from '@angular/platform-browser';
1010
import {NgModule, Component, Directive, ViewChild, ViewContainerRef} from '@angular/core';
1111
import {MdDialog, MdDialogModule} from './dialog';
1212
import {OverlayContainer} from '../core';
13-
import {MdDialogConfig} from './dialog-config';
1413
import {MdDialogRef} from './dialog-ref';
1514
import {MdDialogContainer} from './dialog-container';
1615

@@ -48,10 +47,9 @@ describe('MdDialog', () => {
4847
});
4948

5049
it('should open a dialog with a component', () => {
51-
let config = new MdDialogConfig();
52-
config.viewContainerRef = testViewContainerRef;
53-
54-
let dialogRef = dialog.open(PizzaMsg, config);
50+
let dialogRef = dialog.open(PizzaMsg, {
51+
viewContainerRef: testViewContainerRef
52+
});
5553

5654
viewContainerFixture.detectChanges();
5755

@@ -65,11 +63,10 @@ describe('MdDialog', () => {
6563
});
6664

6765
it('should apply the configured role to the dialog element', () => {
68-
let config = new MdDialogConfig();
69-
config.viewContainerRef = testViewContainerRef;
70-
config.role = 'alertdialog';
71-
72-
dialog.open(PizzaMsg, config);
66+
dialog.open(PizzaMsg, {
67+
viewContainerRef: testViewContainerRef,
68+
role: 'alertdialog'
69+
});
7370

7471
viewContainerFixture.detectChanges();
7572

@@ -78,10 +75,9 @@ describe('MdDialog', () => {
7875
});
7976

8077
it('should close a dialog and get back a result', () => {
81-
let config = new MdDialogConfig();
82-
config.viewContainerRef = testViewContainerRef;
83-
84-
let dialogRef = dialog.open(PizzaMsg, config);
78+
let dialogRef = dialog.open(PizzaMsg, {
79+
viewContainerRef: testViewContainerRef
80+
});
8581

8682
viewContainerFixture.detectChanges();
8783

@@ -98,10 +94,9 @@ describe('MdDialog', () => {
9894

9995

10096
it('should close a dialog via the escape key', () => {
101-
let config = new MdDialogConfig();
102-
config.viewContainerRef = testViewContainerRef;
103-
104-
dialog.open(PizzaMsg, config);
97+
dialog.open(PizzaMsg, {
98+
viewContainerRef: testViewContainerRef
99+
});
105100

106101
viewContainerFixture.detectChanges();
107102

@@ -115,10 +110,9 @@ describe('MdDialog', () => {
115110
});
116111

117112
it('should close when clicking on the overlay backdrop', () => {
118-
let config = new MdDialogConfig();
119-
config.viewContainerRef = testViewContainerRef;
120-
121-
dialog.open(PizzaMsg, config);
113+
dialog.open(PizzaMsg, {
114+
viewContainerRef: testViewContainerRef
115+
});
122116

123117
viewContainerFixture.detectChanges();
124118

@@ -140,10 +134,10 @@ describe('MdDialog', () => {
140134
});
141135

142136
it('should focus the first tabbable element of the dialog on open', fakeAsync(() => {
143-
let config = new MdDialogConfig();
144-
config.viewContainerRef = testViewContainerRef;
137+
dialog.open(PizzaMsg, {
138+
viewContainerRef: testViewContainerRef
139+
});
145140

146-
dialog.open(PizzaMsg, config);
147141
viewContainerFixture.detectChanges();
148142
flushMicrotasks();
149143

@@ -158,10 +152,10 @@ describe('MdDialog', () => {
158152
document.body.appendChild(button);
159153
button.focus();
160154

161-
let config = new MdDialogConfig();
162-
config.viewContainerRef = testViewContainerRef;
155+
let dialogRef = dialog.open(PizzaMsg, {
156+
viewContainerRef: testViewContainerRef
157+
});
163158

164-
let dialogRef = dialog.open(PizzaMsg, config);
165159
viewContainerFixture.detectChanges();
166160
flushMicrotasks();
167161

src/lib/dialog/dialog.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {DialogInjector} from './dialog-injector';
1515
import {MdDialogContainer} from './dialog-container';
1616
import {A11yModule, InteractivityChecker} from '../core';
1717

18-
export {MdDialogConfig} from './dialog-config';
18+
export {MdDialogConfig, DialogRole} from './dialog-config';
1919
export {MdDialogRef} from './dialog-ref';
2020

2121

@@ -27,7 +27,6 @@ export {MdDialogRef} from './dialog-ref';
2727
// TODO(jelbourn): animations
2828

2929

30-
3130
/**
3231
* Service to open Material Design modal dialogs.
3332
*/
@@ -41,6 +40,8 @@ export class MdDialog {
4140
* @param config
4241
*/
4342
open<T>(component: ComponentType<T>, config: MdDialogConfig): MdDialogRef<T> {
43+
config = this._applyConfigDefaults(config);
44+
4445
let overlayRef = this._createOverlay(config);
4546
let dialogContainer = this._attachDialogContainer(overlayRef, config);
4647

@@ -122,6 +123,20 @@ export class MdDialog {
122123

123124
return state;
124125
}
126+
127+
/**
128+
* Applies default options to the dialog config.
129+
* @param {MdDialogConfig} config Config to be modified.
130+
* @returns The new configuration object.
131+
*/
132+
private _applyConfigDefaults(config: MdDialogConfig): MdDialogConfig {
133+
let defaults: MdDialogConfig = {
134+
role: 'dialog',
135+
viewContainerRef: null
136+
};
137+
138+
return Object.assign(defaults, config);
139+
}
125140
}
126141

127142

0 commit comments

Comments
 (0)