Skip to content

Commit 112be4a

Browse files
committed
fix(datepicker): avoid accidental form submissions through calendar buttons
Sets all internal calendar buttons to be `type="button"` in order to avoid accidental submissions if the calendar is placed inside a form. Fixes #10514.
1 parent 01807da commit 112be4a

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/lib/datepicker/calendar-header.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<div class="mat-calendar-header">
22
<div class="mat-calendar-controls">
3-
<button mat-button class="mat-calendar-period-button"
3+
<button mat-button type="button" class="mat-calendar-period-button"
44
(click)="currentPeriodClicked()" [attr.aria-label]="periodButtonLabel">
55
{{periodButtonText}}
66
<div class="mat-calendar-arrow" [class.mat-calendar-invert]="calendar.currentView != 'month'"></div>
77
</button>
88

99
<div class="mat-calendar-spacer"></div>
1010

11-
<button mat-icon-button class="mat-calendar-previous-button"
11+
<button mat-icon-button type="button" class="mat-calendar-previous-button"
1212
[disabled]="!previousEnabled()" (click)="previousClicked()"
1313
[attr.aria-label]="prevButtonLabel">
1414
</button>
1515

16-
<button mat-icon-button class="mat-calendar-next-button"
16+
<button mat-icon-button type="button" class="mat-calendar-next-button"
1717
[disabled]="!nextEnabled()" (click)="nextClicked()"
1818
[attr.aria-label]="nextButtonLabel">
1919
</button>

src/lib/datepicker/calendar.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,12 @@ describe('MatCalendar', () => {
113113
fixture.detectChanges();
114114

115115
expect(button.getAttribute('aria-label')).toBe('Go to multi-year view?');
116-
})
117-
);
116+
}));
118117

118+
it('should set all buttons to be `type="button"`', () => {
119+
const invalidButtons = calendarElement.querySelectorAll('button:not([type="button"])');
120+
expect(invalidButtons.length).toBe(0);
121+
});
119122

120123
describe('a11y', () => {
121124
describe('calendar body', () => {

0 commit comments

Comments
 (0)