Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/material/dialog/dialog-content-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ let dialogElementUid = 0;
host: {
'(click)': 'dialogRef.close(dialogResult)',
'[attr.aria-label]': 'ariaLabel || null',
'type': 'button', // Prevents accidental form submits.
'[attr.type]': 'type',
}
})
export class MatDialogClose implements OnInit, OnChanges {
/** Screenreader label for the button. */
@Input('aria-label') ariaLabel: string;

/** Default to "button" to prevents accidental form submits. */
@Input() type: 'submit' | 'button' | 'reset' = 'button';

/** Dialog close input. */
@Input('mat-dialog-close') dialogResult: any;

Expand Down
10 changes: 9 additions & 1 deletion src/material/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1204,12 +1204,18 @@ describe('MatDialog', () => {
expect(button.getAttribute('aria-label')).toBe('Best close button ever');
}));

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

expect(button.getAttribute('type')).toBe('button');
});

it('should not override type attribute of the close button if set manually', () => {
let button = overlayContainerElement.querySelector('button.with-submit')!;

expect(button.getAttribute('type')).toBe('submit');
});

it('should return the [mat-dialog-close] result when clicking the close button',
fakeAsync(() => {
let afterCloseCallback = jasmine.createSpy('afterClose callback');
Expand Down Expand Up @@ -1557,6 +1563,7 @@ class PizzaMsg {
aria-label="Best close button ever"
[mat-dialog-close]="true"></button>
<div mat-dialog-close>Should not close</div>
<button class="with-submit" type="submit" mat-dialog-close>Should have submit</button>
</mat-dialog-actions>
`
})
Expand All @@ -1575,6 +1582,7 @@ class ContentElementDialog {}
aria-label="Best close button ever"
[mat-dialog-close]="true"></button>
<div mat-dialog-close>Should not close</div>
<button class="with-submit" type="submit" mat-dialog-close>Should have submit</button>
</mat-dialog-actions>
</ng-template>
`
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/dialog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export declare class MatDialogClose implements OnInit, OnChanges {
ariaLabel: string;
dialogRef: MatDialogRef<any>;
dialogResult: any;
type: 'submit' | 'button' | 'reset';
constructor(dialogRef: MatDialogRef<any>, _elementRef: ElementRef<HTMLElement>, _dialog: MatDialog);
ngOnChanges(changes: SimpleChanges): void;
ngOnInit(): void;
Expand Down