Skip to content

Commit c819a32

Browse files
committed
feat(dialog): add a config option for passing in data
Adds the ability to pass in data to a dialog via the `data` property. While this was previously possible through the `dialogRef.componentInstance.someUserSuppliedProperty`, it wasn't the most intuitive. The new approach looks like this: ``` dialog.open(SomeDialog, { data: { hello: 'world' } }); class SometDialog { constructor(data: MdDialogData) { console.log(data['hello']); } } ``` Fixes #2181.
1 parent 592f33f commit c819a32

File tree

7 files changed

+86
-21
lines changed

7 files changed

+86
-21
lines changed

src/demo-app/dialog/dialog-demo.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ <h2>Other options</h2>
3434
</md-select>
3535
</p>
3636

37-
<md-checkbox [(ngModel)]="config.disableClose">Disable close</md-checkbox>
37+
<p>
38+
<md-input [(ngModel)]="config.data.message" placeholder="Dialog message"></md-input>
39+
</p>
40+
41+
<p>
42+
<md-checkbox [(ngModel)]="config.disableClose">Disable close</md-checkbox>
43+
</p>
3844
</md-card-content>
3945
</md-card>
4046

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

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

44
@Component({
55
moduleId: module.id,
@@ -20,6 +20,9 @@ export class DialogDemo {
2020
bottom: '',
2121
left: '',
2222
right: ''
23+
},
24+
data: {
25+
message: 'Jazzy jazz jazz'
2326
}
2427
};
2528

@@ -28,7 +31,7 @@ export class DialogDemo {
2831
openJazz() {
2932
this.dialogRef = this.dialog.open(JazzDialog, this.config);
3033

31-
this.dialogRef.afterClosed().subscribe(result => {
34+
this.dialogRef.afterClosed().subscribe((result: string) => {
3235
this.lastCloseResult = result;
3336
this.dialogRef = null;
3437
});
@@ -46,13 +49,13 @@ export class DialogDemo {
4649
template: `
4750
<p>It's Jazz!</p>
4851
<p><label>How much? <input #howMuch></label></p>
49-
<p> {{ jazzMessage }} </p>
52+
<p> {{ data.message }} </p>
5053
<button type="button" (click)="dialogRef.close(howMuch.value)">Close dialog</button>`
5154
})
5255
export class JazzDialog {
53-
jazzMessage = 'Jazzy jazz jazz';
54-
55-
constructor(public dialogRef: MdDialogRef<JazzDialog>) { }
56+
constructor(
57+
public dialogRef: MdDialogRef<JazzDialog>,
58+
public data: MdDialogData) { }
5659
}
5760

5861

src/lib/dialog/README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ MdDialog is a service, which opens dialogs components in the view.
1313

1414
| Key | Description |
1515
| --- | ------------ |
16-
| `role: DialogRole = 'dialog'` | The ARIA role of the dialog element. Possible values are `dialog` and `alertdialog`. Optional. |
17-
| `disableClose: boolean = false` | Whether to prevent the user from closing a dialog by clicking on the backdrop or pressing escape. Optional. |
18-
| `width: string = ''` | Width of the dialog. Takes any valid CSS value. Optional. |
19-
| `height: string = ''` | Height of the dialog. Takes any valid CSS value. Optional. |
20-
| `position: { top?: string, bottom?: string, left?: string, right?: string }` | Position of the dialog that overrides the default centering in it's axis. Optional. |
21-
| `viewContainerRef: ViewContainerRef` | The view container ref to attach the dialog to. Optional. |
16+
| `role?: DialogRole = 'dialog'` | The ARIA role of the dialog element. Possible values are `dialog` and `alertdialog`. |
17+
| `disableClose?: boolean = false` | Whether to prevent the user from closing a dialog by clicking on the backdrop or pressing escape. |
18+
| `data?: { [key: string]: any }` | Data that will be injected into the dialog as `MdDialogData`. |
19+
| `width?: string = ''` | Width of the dialog. Takes any valid CSS value. |
20+
| `height?: string = ''` | Height of the dialog. Takes any valid CSS value. |
21+
| `position?: { top?: string, bottom?: string, left?: string, right?: string }` | Position of the dialog that overrides the default centering in it's axis. |
22+
| `viewContainerRef?: ViewContainerRef` | The view container ref to attach the dialog to. |
2223

2324
## MdDialogRef
2425

@@ -57,7 +58,10 @@ export class PizzaComponent {
5758

5859
openDialog() {
5960
this.dialogRef = this.dialog.open(PizzaDialog, {
60-
disableClose: false
61+
disableClose: false,
62+
data: {
63+
pizzaType: 'pepperoni'
64+
}
6165
});
6266

6367
this.dialogRef.afterClosed().subscribe(result => {
@@ -70,7 +74,7 @@ export class PizzaComponent {
7074
@Component({
7175
selector: 'pizza-dialog',
7276
template: `
73-
<h1 md-dialog-title>Would you like to order pizza?</h1>
77+
<h1 md-dialog-title>Would you like to order a {{ data.pizzaType }} pizza?</h1>
7478
7579
<md-dialog-actions>
7680
<button (click)="dialogRef.close('yes')">Yes</button>
@@ -79,7 +83,9 @@ export class PizzaComponent {
7983
`
8084
})
8185
export class PizzaDialog {
82-
constructor(public dialogRef: MdDialogRef<PizzaDialog>) { }
86+
constructor(
87+
public dialogRef: MdDialogRef<PizzaDialog>,
88+
public data: MdDialogData) { }
8389
}
8490
```
8591

src/lib/dialog/dialog-config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export interface DialogPosition {
1111
right?: string;
1212
};
1313

14+
/** Data to be injected into a dialog. */
15+
export class MdDialogData {
16+
[key: string]: any;
17+
}
1418

1519
/**
1620
* Configuration for opening a modal dialog with the MdDialog service.
@@ -33,5 +37,8 @@ export class MdDialogConfig {
3337
/** Position overrides. */
3438
position?: DialogPosition;
3539

40+
/** Data being injected into the child component. */
41+
data?: MdDialogData;
42+
3643
// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
3744
}

src/lib/dialog/dialog-injector.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import {Injector} from '@angular/core';
22
import {MdDialogRef} from './dialog-ref';
3+
import {MdDialogData} from './dialog-config';
34

45

56
/** Custom injector type specifically for instantiating components with a dialog. */
67
export class DialogInjector implements Injector {
7-
constructor(private _dialogRef: MdDialogRef<any>, private _parentInjector: Injector) { }
8+
constructor(
9+
private _dialogRef: MdDialogRef<any>,
10+
private _data: MdDialogData,
11+
private _parentInjector: Injector) { }
812

913
get(token: any, notFoundValue?: any): any {
1014
if (token === MdDialogRef) {
1115
return this._dialogRef;
1216
}
1317

18+
if (token === MdDialogData && this._data) {
19+
return this._data;
20+
}
21+
1422
return this._parentInjector.get(token, notFoundValue);
1523
}
1624
}

src/lib/dialog/dialog.spec.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {MdDialog} from './dialog';
1414
import {OverlayContainer} from '../core';
1515
import {MdDialogRef} from './dialog-ref';
1616
import {MdDialogContainer} from './dialog-container';
17+
import {MdDialogData} from './dialog-config';
1718

1819

1920
describe('MdDialog', () => {
@@ -242,6 +243,28 @@ describe('MdDialog', () => {
242243
expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(0);
243244
});
244245

246+
describe('passing in data', () => {
247+
it('should be able to pass in data', () => {
248+
let config = {
249+
data: {
250+
stringParam: 'hello',
251+
dateParam: new Date()
252+
}
253+
};
254+
255+
let instance = dialog.open(DialogWithInjectedData, config).componentInstance;
256+
257+
expect(instance.data['stringParam']).toBe(config.data.stringParam);
258+
expect(instance.data['dateParam']).toBe(config.data.dateParam);
259+
});
260+
261+
it('should throw if injected data is expected but none is passed', () => {
262+
expect(() => {
263+
dialog.open(DialogWithInjectedData);
264+
}).toThrow();
265+
});
266+
});
267+
245268
describe('disableClose option', () => {
246269
it('should prevent closing via clicks on the backdrop', () => {
247270
dialog.open(PizzaMsg, {
@@ -476,19 +499,31 @@ class ComponentThatProvidesMdDialog {
476499
constructor(public dialog: MdDialog) {}
477500
}
478501

502+
/** Simple component for testing ComponentPortal. */
503+
@Component({template: ''})
504+
class DialogWithInjectedData {
505+
constructor(public data: MdDialogData) { }
506+
}
507+
479508
// Create a real (non-test) NgModule as a workaround for
480509
// https://github.com/angular/angular/issues/10760
481510
const TEST_DIRECTIVES = [
482511
ComponentWithChildViewContainer,
483512
PizzaMsg,
484513
DirectiveWithViewContainer,
485-
ContentElementDialog
514+
ContentElementDialog,
515+
DialogWithInjectedData
486516
];
487517

488518
@NgModule({
489519
imports: [MdDialogModule],
490520
exports: TEST_DIRECTIVES,
491521
declarations: TEST_DIRECTIVES,
492-
entryComponents: [ComponentWithChildViewContainer, PizzaMsg, ContentElementDialog],
522+
entryComponents: [
523+
ComponentWithChildViewContainer,
524+
PizzaMsg,
525+
ContentElementDialog,
526+
DialogWithInjectedData
527+
],
493528
})
494529
class DialogTestModule { }

src/lib/dialog/dialog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class MdDialog {
108108
// to modify and close it.
109109
let dialogRef = <MdDialogRef<T>> new MdDialogRef(overlayRef);
110110

111-
if (!dialogContainer.dialogConfig.disableClose) {
111+
if (!config.disableClose) {
112112
// When the dialog backdrop is clicked, we want to close it.
113113
overlayRef.backdropClick().first().subscribe(() => dialogRef.close());
114114
}
@@ -120,7 +120,7 @@ export class MdDialog {
120120
// inject the MdDialogRef. This allows a component loaded inside of a dialog to close itself
121121
// and, optionally, to return a value.
122122
let userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
123-
let dialogInjector = new DialogInjector(dialogRef, userInjector || this._injector);
123+
let dialogInjector = new DialogInjector(dialogRef, config.data, userInjector || this._injector);
124124

125125
let contentPortal = new ComponentPortal(component, null, dialogInjector);
126126

0 commit comments

Comments
 (0)