Skip to content

Commit b47cc47

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 f882243 commit b47cc47

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/lib/datepicker/calendar.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 type="button" mat-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]="_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 type="button" mat-icon-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 type="button" mat-icon-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
@@ -220,9 +220,12 @@ describe('MatCalendar', () => {
220220
fixture.detectChanges();
221221

222222
expect(button.getAttribute('aria-label')).toBe('Go to multi-year view?');
223-
})
224-
);
223+
}));
225224

225+
it('should set all buttons to be `type="button"`', () => {
226+
const invalidButtons = calendarElement.querySelectorAll('button:not([type="button"])');
227+
expect(invalidButtons.length).toBe(0);
228+
});
226229

227230
describe('a11y', () => {
228231
describe('calendar body', () => {

0 commit comments

Comments
 (0)