Skip to content

Commit 66166c4

Browse files
committed
feat(dialog): add result to MdDialogClose directive
MdDialog has MdDialogClose to make it easier to structure a dialog. But as MdDialogClose doesn't return any value, it's imposibble to use MdDialogClose if you want to get any return value when MdDialog is close. So `@Input('md-dialog-close')result` is added to MdDialogClose to solve it. no breaking changes
1 parent 4021276 commit 66166c4

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

src/lib/dialog/dialog-content-directives.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {MdDialogRef} from './dialog-ref';
88
@Directive({
99
selector: 'button[md-dialog-close], button[mat-dialog-close]',
1010
host: {
11-
'(click)': 'dialogRef.close()',
11+
'(click)': 'dialogRef.close(result)',
1212
'[attr.aria-label]': 'ariaLabel',
1313
'type': 'button', // Prevents accidental form submits.
1414
}
@@ -17,6 +17,9 @@ export class MdDialogClose {
1717
/** Screenreader label for the button. */
1818
@Input('aria-label') ariaLabel: string = 'Close dialog';
1919

20+
/** Result to return to the dialog opener. */
21+
@Input('md-dialog-close') result: any;
22+
2023
constructor(public dialogRef: MdDialogRef<any>) { }
2124
}
2225

src/lib/dialog/dialog.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,22 @@ export class YourDialog {
5656
### Dialog content
5757
Several directives are available to make it easier to structure your dialog content:
5858

59-
| Name | Description |
60-
|-----------------------|--------------------------------------------------------------------------|
61-
| `md-dialog-title` | \[Attr] Dialog title, applied to a heading element (e.g., `<h1>`, `<h2>`)|
62-
| `<md-dialog-content>` | Primary scrollable content of the dialog |
63-
| `<md-dialog-actions>` | Container for action buttons at the bottom of the dialog |
64-
| `md-dialog-close` | \[Attr] Added to a `<button>`, makes the button close the dialog on click|
59+
| Name | Description |
60+
|-----------------------|------------------------------------------------------------------------------------------------------|
61+
| `md-dialog-title` | \[Attr] Dialog title, applied to a heading element (e.g., `<h1>`, `<h2>`) |
62+
| `<md-dialog-content>` | Primary scrollable content of the dialog |
63+
| `<md-dialog-actions>` | Container for action buttons at the bottom of the dialog |
64+
| `md-dialog-close` | \[Attr] Added to a `<button>`, makes the button close the dialog and return the binded value on click|
65+
66+
For example:
67+
```html
68+
<h2 md-dialog-title>Delete all</h2>
69+
<md-dialog-content>Are you sure?</md-dialog-content>
70+
<md-dialog-actions>
71+
<button md-button [md-dialog-close]="true">Yes</button>
72+
<button md-button [md-dialog-close]="false">No</button>
73+
</md-dialog-actions>
74+
```
6575

6676
Once a dialog opens, the dialog will automatically focus the first tabbable element.
6777

src/lib/dialog/dialog.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,18 @@ describe('MdDialog', () => {
600600
expect(button.getAttribute('type')).toBe('button');
601601
});
602602

603+
it('should return the [md-dialog-close] result when clicking on the close button', async(() => {
604+
let afterCloseCallback = jasmine.createSpy('afterClose callback');
605+
dialogRef.afterClosed().subscribe(afterCloseCallback);
606+
607+
(overlayContainerElement.querySelector('button.close-with-true') as HTMLElement).click();
608+
viewContainerFixture.detectChanges();
609+
610+
viewContainerFixture.whenStable().then(() => {
611+
expect(afterCloseCallback).toHaveBeenCalledWith(true);
612+
});
613+
}));
614+
603615
});
604616
});
605617

@@ -714,6 +726,7 @@ class PizzaMsg {
714726
<md-dialog-content>Lorem ipsum dolor sit amet.</md-dialog-content>
715727
<md-dialog-actions>
716728
<button md-dialog-close [aria-label]="closeButtonAriaLabel">Close</button>
729+
<button class="close-with-true" [md-dialog-close]="true">Close and return true</button>
717730
<div md-dialog-close>Should not close</div>
718731
</md-dialog-actions>
719732
`
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1 md-dialog-title>Dialog</h1>
22
<div md-dialog-content>What would you like to do?</div>
33
<div md-dialog-actions>
4-
<button md-button (click)="dialogRef.close('Option 1')">Option 1</button>
5-
<button md-button (click)="dialogRef.close('Option 2')">Option 2</button>
4+
<button md-button md-dialog-close="Option 1">Option 1</button>
5+
<button md-button md-dialog-close="Option 2">Option 2</button>
66
</div>

0 commit comments

Comments
 (0)