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 27, 2018
1 parent 01807da commit 112be4a
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-header.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 mat-button type="button" class="mat-calendar-period-button"
(click)="currentPeriodClicked()" [attr.aria-label]="periodButtonLabel">
{{periodButtonText}}
<div class="mat-calendar-arrow" [class.mat-calendar-invert]="calendar.currentView != 'month'"></div>
</button>

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

<button mat-icon-button class="mat-calendar-previous-button"
<button mat-icon-button type="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 mat-icon-button type="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 @@ -113,9 +113,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 112be4a

Please sign in to comment.