Skip to content

Commit e1de055

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 d10e5f8 commit e1de055

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

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

+7-1
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(dialogResult)',
1212
'[attr.aria-label]': 'ariaLabel',
1313
'type': 'button', // Prevents accidental form submits.
1414
}
@@ -17,6 +17,12 @@ export class MdDialogClose {
1717
/** Screenreader label for the button. */
1818
@Input('aria-label') ariaLabel: string = 'Close dialog';
1919

20+
/** Dialog close input. */
21+
@Input('md-dialog-close') dialogResult: any;
22+
23+
/** Dialog close input for compatibility mode. */
24+
@Input('mat-dialog-close') set _matDialogClose(value: any) { this.dialogResult = value; }
25+
2026
constructor(public dialogRef: MdDialogRef<any>) { }
2127
}
2228

src/lib/dialog/dialog.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,23 @@ 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 with an optional result from the bound value.|
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>No</button>
72+
<!-- Can optionally provide a result for the closing dialog. -->
73+
<button md-button [md-dialog-close]="true">Yes</button>
74+
</md-dialog-actions>
75+
```
6576

6677
Once a dialog opens, the dialog will automatically focus the first tabbable element.
6778

src/lib/dialog/dialog.spec.ts

+13
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
`
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)