Skip to content

Commit

Permalink
fix(datepicker): avoid accidental form submissions through calendar b…
Browse files Browse the repository at this point in the history
…uttons

Sets all internal calendar buttons to be `type="button"` in order to avoid accidental submissions if the calendar is placed inside a form.

Fixes angular#10514.
  • Loading branch information
crisbeto committed Mar 21, 2018
1 parent f882243 commit b47cc47
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/lib/datepicker/calendar.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<div class="mat-calendar-header">
<div class="mat-calendar-controls">
<button mat-button class="mat-calendar-period-button"
<button type="button" mat-button class="mat-calendar-period-button"
(click)="_currentPeriodClicked()" [attr.aria-label]="_periodButtonLabel">
{{_periodButtonText}}
<div class="mat-calendar-arrow" [class.mat-calendar-invert]="_currentView != 'month'"></div>
</button>

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

<button mat-icon-button class="mat-calendar-previous-button"
<button type="button" mat-icon-button class="mat-calendar-previous-button"
[disabled]="!_previousEnabled()" (click)="_previousClicked()"
[attr.aria-label]="_prevButtonLabel">
</button>

<button mat-icon-button class="mat-calendar-next-button"
<button type="button" mat-icon-button class="mat-calendar-next-button"
[disabled]="!_nextEnabled()" (click)="_nextClicked()"
[attr.aria-label]="_nextButtonLabel">
</button>
Expand Down
7 changes: 5 additions & 2 deletions src/lib/datepicker/calendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,12 @@ describe('MatCalendar', () => {
fixture.detectChanges();

expect(button.getAttribute('aria-label')).toBe('Go to multi-year view?');
})
);
}));

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

describe('a11y', () => {
describe('calendar body', () => {
Expand Down

0 comments on commit b47cc47

Please sign in to comment.