Skip to content

Commit 3ee3ecb

Browse files
flaucjelbourn
authored andcommitted
feat(Dialog): don't override type attribute if set on mat-dialog-close (#16927)
* feat(Dialog): don't override type attribute if set on mat-dialog-close only default the type attribute on mat-dialog-close if it isn't set #16909 * chore(Dialog): renamed buttonType to type in mat-dialog-close #16909
1 parent 237e030 commit 3ee3ecb

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ let dialogElementUid = 0;
3030
host: {
3131
'(click)': 'dialogRef.close(dialogResult)',
3232
'[attr.aria-label]': 'ariaLabel || null',
33-
'type': 'button', // Prevents accidental form submits.
33+
'[attr.type]': 'type',
3434
}
3535
})
3636
export class MatDialogClose implements OnInit, OnChanges {
3737
/** Screenreader label for the button. */
3838
@Input('aria-label') ariaLabel: string;
3939

40+
/** Default to "button" to prevents accidental form submits. */
41+
@Input() type: 'submit' | 'button' | 'reset' = 'button';
42+
4043
/** Dialog close input. */
4144
@Input('mat-dialog-close') dialogResult: any;
4245

src/material/dialog/dialog.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1204,12 +1204,18 @@ describe('MatDialog', () => {
12041204
expect(button.getAttribute('aria-label')).toBe('Best close button ever');
12051205
}));
12061206

1207-
it('should override the "type" attribute of the close button', () => {
1207+
it('should set the "type" attribute of the close button if not set manually', () => {
12081208
let button = overlayContainerElement.querySelector('button[mat-dialog-close]')!;
12091209

12101210
expect(button.getAttribute('type')).toBe('button');
12111211
});
12121212

1213+
it('should not override type attribute of the close button if set manually', () => {
1214+
let button = overlayContainerElement.querySelector('button.with-submit')!;
1215+
1216+
expect(button.getAttribute('type')).toBe('submit');
1217+
});
1218+
12131219
it('should return the [mat-dialog-close] result when clicking the close button',
12141220
fakeAsync(() => {
12151221
let afterCloseCallback = jasmine.createSpy('afterClose callback');
@@ -1557,6 +1563,7 @@ class PizzaMsg {
15571563
aria-label="Best close button ever"
15581564
[mat-dialog-close]="true"></button>
15591565
<div mat-dialog-close>Should not close</div>
1566+
<button class="with-submit" type="submit" mat-dialog-close>Should have submit</button>
15601567
</mat-dialog-actions>
15611568
`
15621569
})
@@ -1575,6 +1582,7 @@ class ContentElementDialog {}
15751582
aria-label="Best close button ever"
15761583
[mat-dialog-close]="true"></button>
15771584
<div mat-dialog-close>Should not close</div>
1585+
<button class="with-submit" type="submit" mat-dialog-close>Should have submit</button>
15781586
</mat-dialog-actions>
15791587
</ng-template>
15801588
`

tools/public_api_guard/material/dialog.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export declare class MatDialogClose implements OnInit, OnChanges {
4949
ariaLabel: string;
5050
dialogRef: MatDialogRef<any>;
5151
dialogResult: any;
52+
type: 'submit' | 'button' | 'reset';
5253
constructor(dialogRef: MatDialogRef<any>, _elementRef: ElementRef<HTMLElement>, _dialog: MatDialog);
5354
ngOnChanges(changes: SimpleChanges): void;
5455
ngOnInit(): void;

0 commit comments

Comments
 (0)