Skip to content

Commit f525db1

Browse files
crisbetojelbourn
authored andcommitted
feat(dialog): allow for an object literal to be passed on init (#1679)
Adds the ability to pass in an object literal, that matches the signature of `MdDialogConfig`, when opening a dialog.
1 parent 9115538 commit f525db1

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

src/lib/dialog/dialog-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class MdDialogConfig {
1515
role?: DialogRole = 'dialog';
1616

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

2020
// TODO(jelbourn): add configuration for size, lifecycle hooks, ARIA labelling.
2121
}

src/lib/dialog/dialog.spec.ts

Lines changed: 27 additions & 38 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

@@ -79,11 +77,7 @@ describe('MdDialog', () => {
7977
});
8078

8179
it('should apply the configured role to the dialog element', () => {
82-
let config = new MdDialogConfig();
83-
config.viewContainerRef = testViewContainerRef;
84-
config.role = 'alertdialog';
85-
86-
dialog.open(PizzaMsg, config);
80+
dialog.open(PizzaMsg, { role: 'alertdialog' });
8781

8882
viewContainerFixture.detectChanges();
8983

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

9488
it('should close a dialog and get back a result', () => {
95-
let config = new MdDialogConfig();
96-
config.viewContainerRef = testViewContainerRef;
97-
98-
let dialogRef = dialog.open(PizzaMsg, config);
89+
let dialogRef = dialog.open(PizzaMsg, {
90+
viewContainerRef: testViewContainerRef
91+
});
9992

10093
viewContainerFixture.detectChanges();
10194

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

113106

114107
it('should close a dialog via the escape key', () => {
115-
let config = new MdDialogConfig();
116-
config.viewContainerRef = testViewContainerRef;
117-
118-
dialog.open(PizzaMsg, config);
108+
dialog.open(PizzaMsg, {
109+
viewContainerRef: testViewContainerRef
110+
});
119111

120112
viewContainerFixture.detectChanges();
121113

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

131123
it('should close when clicking on the overlay backdrop', () => {
132-
let config = new MdDialogConfig();
133-
config.viewContainerRef = testViewContainerRef;
134-
135-
dialog.open(PizzaMsg, config);
124+
dialog.open(PizzaMsg, {
125+
viewContainerRef: testViewContainerRef
126+
});
136127

137128
viewContainerFixture.detectChanges();
138129

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

145136
describe('disableClose option', () => {
146137
it('should prevent closing via clicks on the backdrop', () => {
147-
let config = new MdDialogConfig();
148-
config.viewContainerRef = testViewContainerRef;
149-
config.disableClose = true;
150-
151-
dialog.open(PizzaMsg, config);
138+
dialog.open(PizzaMsg, {
139+
disableClose: true,
140+
viewContainerRef: testViewContainerRef
141+
});
152142

153143
viewContainerFixture.detectChanges();
154144

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

161151
it('should prevent closing via the escape key', () => {
162-
let config = new MdDialogConfig();
163-
config.viewContainerRef = testViewContainerRef;
164-
config.disableClose = true;
165-
166-
dialog.open(PizzaMsg, config);
152+
dialog.open(PizzaMsg, {
153+
disableClose: true,
154+
viewContainerRef: testViewContainerRef
155+
});
167156

168157
viewContainerFixture.detectChanges();
169158

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

191180
it('should focus the first tabbable element of the dialog on open', fakeAsync(() => {
192-
let config = new MdDialogConfig();
193-
config.viewContainerRef = testViewContainerRef;
181+
dialog.open(PizzaMsg, {
182+
viewContainerRef: testViewContainerRef
183+
});
194184

195-
dialog.open(PizzaMsg, config);
196185
viewContainerFixture.detectChanges();
197186
flushMicrotasks();
198187

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

210-
let config = new MdDialogConfig();
211-
config.viewContainerRef = testViewContainerRef;
199+
let dialogRef = dialog.open(PizzaMsg, {
200+
viewContainerRef: testViewContainerRef
201+
});
212202

213-
let dialogRef = dialog.open(PizzaMsg, config);
214203
viewContainerFixture.detectChanges();
215204
flushMicrotasks();
216205

src/lib/dialog/dialog.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ export class MdDialog {
4040
* @param component Type of the component to load into the load.
4141
* @param config
4242
*/
43-
open<T>(component: ComponentType<T>, config = new MdDialogConfig()): MdDialogRef<T> {
43+
open<T>(component: ComponentType<T>, config?: MdDialogConfig): MdDialogRef<T> {
44+
config = this._applyConfigDefaults(config);
45+
4446
let overlayRef = this._createOverlay(config);
4547
let dialogContainer = this._attachDialogContainer(overlayRef, config);
4648

@@ -125,6 +127,15 @@ export class MdDialog {
125127

126128
return state;
127129
}
130+
131+
/**
132+
* Applies default options to the dialog config.
133+
* @param dialogConfig Config to be modified.
134+
* @returns The new configuration object.
135+
*/
136+
private _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
137+
return Object.assign(new MdDialogConfig(), dialogConfig);
138+
}
128139
}
129140

130141

0 commit comments

Comments
 (0)