Skip to content

Commit 782f893

Browse files
committed
fix(material/datepicker): update active date on focusing a calendar cell
When a a date cell on the calendar recieves focus, set the active date to that cell. This ensures that the active date matches the date with browser focus. Previously, we set the active date on keydown and click, but that was problematic for screenreaders. That's because many screenreaders trigger a focus event instead of a keydown event when using screenreader specific navigation (VoiceOver, Chromevox, NVDA). Fixes #23483
1 parent dd59b4a commit 782f893

12 files changed

+239
-33
lines changed

src/material/datepicker/calendar-body.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
[attr.aria-disabled]="!item.enabled || null"
6464
[attr.aria-pressed]="_isSelected(item.compareValue)"
6565
[attr.aria-current]="todayValue === item.compareValue ? 'date' : null"
66-
(click)="_cellClicked(item, $event)">
66+
(click)="_cellClicked(item, $event)"
67+
(focus)="_cellFocused(item, $event)">
6768
<div class="mat-calendar-body-cell-content mat-focus-indicator"
6869
[class.mat-calendar-body-selected]="_isSelected(item.compareValue)"
6970
[class.mat-calendar-body-comparison-identical]="_isComparisonIdentical(item.compareValue)"

src/material/datepicker/calendar-body.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
OnChanges,
1919
SimpleChanges,
2020
OnDestroy,
21+
AfterViewChecked,
2122
} from '@angular/core';
2223
import {take} from 'rxjs/operators';
2324

@@ -67,13 +68,18 @@ export interface MatCalendarUserEvent<D> {
6768
encapsulation: ViewEncapsulation.None,
6869
changeDetection: ChangeDetectionStrategy.OnPush,
6970
})
70-
export class MatCalendarBody implements OnChanges, OnDestroy {
71+
export class MatCalendarBody implements OnChanges, OnDestroy, AfterViewChecked {
7172
/**
7273
* Used to skip the next focus event when rendering the preview range.
7374
* We need a flag like this, because some browsers fire focus events asynchronously.
7475
*/
7576
private _skipNextFocus: boolean;
7677

78+
/**
79+
* Used to focus the active cell after change detection has run.
80+
*/
81+
private _focusActiveCellAfterViewChecked = false;
82+
7783
/** The label for the table. (e.g. "Jan 2017"). */
7884
@Input() label: string;
7985

@@ -98,6 +104,13 @@ export class MatCalendarBody implements OnChanges, OnDestroy {
98104
/** The cell number of the active cell in the table. */
99105
@Input() activeCell: number = 0;
100106

107+
ngAfterViewChecked() {
108+
if (this._focusActiveCellAfterViewChecked) {
109+
this._focusActiveCell();
110+
this._focusActiveCellAfterViewChecked = false;
111+
}
112+
}
113+
101114
/** Whether a range is being selected. */
102115
@Input() isRange: boolean = false;
103116

@@ -127,6 +140,8 @@ export class MatCalendarBody implements OnChanges, OnDestroy {
127140
MatCalendarUserEvent<MatCalendarCell | null>
128141
>();
129142

143+
@Output() readonly activeDateChange = new EventEmitter<MatCalendarUserEvent<number>>();
144+
130145
/** The number of blank cells to put at the beginning for the first row. */
131146
_firstRowOffset: number;
132147

@@ -153,6 +168,12 @@ export class MatCalendarBody implements OnChanges, OnDestroy {
153168
}
154169
}
155170

171+
_cellFocused(cell: MatCalendarCell, event: FocusEvent): void {
172+
if (cell.enabled) {
173+
this.activeDateChange.emit({value: cell.value, event});
174+
}
175+
}
176+
156177
/** Returns whether a cell should be marked as selected. */
157178
_isSelected(value: number) {
158179
return this.startValue === value || this.endValue === value;
@@ -214,6 +235,11 @@ export class MatCalendarBody implements OnChanges, OnDestroy {
214235
});
215236
}
216237

238+
/** Focuses the active cell after change detection has run and the microtask queue is empty. */
239+
_scheduleFocusActiveCellAfterViewChecked() {
240+
this._focusActiveCellAfterViewChecked = true;
241+
}
242+
217243
/** Gets whether a value is the start of the main range. */
218244
_isRangeStart(value: number) {
219245
return isStart(value, this.startValue, this.endValue);

src/material/datepicker/month-view.html

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
[labelMinRequiredCells]="3"
2222
[activeCell]="_dateAdapter.getDate(activeDate) - 1"
2323
(selectedValueChange)="_dateSelected($event)"
24+
(activeDateChange)="_handleCalendarBodyDateFocused($event)"
2425
(previewChange)="_previewChanged($event)"
2526
(keyup)="_handleCalendarBodyKeyup($event)"
2627
(keydown)="_handleCalendarBodyKeydown($event)">

src/material/datepicker/month-view.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,30 @@ describe('MatMonthView', () => {
520520
);
521521
},
522522
);
523+
524+
it('should go to month that is focused', () => {
525+
const jan11Cell = fixture.debugElement.nativeElement.querySelector(
526+
'[data-mat-row="1"][data-mat-col="3"] button',
527+
) as HTMLElement;
528+
529+
dispatchFakeEvent(jan11Cell, 'focus');
530+
fixture.detectChanges();
531+
532+
expect(calendarInstance.date).toEqual(new Date(2017, JAN, 11));
533+
});
534+
535+
it('should not call `.focus()` when the active date is focused', () => {
536+
const jan5Cell = fixture.debugElement.nativeElement.querySelector(
537+
'[data-mat-row="0"][data-mat-col="4"] button',
538+
) as HTMLElement;
539+
const focusSpy = (jan5Cell.focus = jasmine.createSpy('cellFocused'));
540+
541+
dispatchFakeEvent(jan5Cell, 'focus');
542+
fixture.detectChanges();
543+
544+
expect(calendarInstance.date).toEqual(new Date(2017, JAN, 5));
545+
expect(focusSpy).not.toHaveBeenCalled();
546+
});
523547
});
524548
});
525549
});

src/material/datepicker/month-view.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
230230
/** Handles when a new date is selected. */
231231
_dateSelected(event: MatCalendarUserEvent<number>) {
232232
const date = event.value;
233-
const selectedYear = this._dateAdapter.getYear(this.activeDate);
234-
const selectedMonth = this._dateAdapter.getMonth(this.activeDate);
235-
const selectedDate = this._dateAdapter.createDate(selectedYear, selectedMonth, date);
233+
const selectedDate = this._getDateFromDayOfMonth(date);
236234
let rangeStartDate: number | null;
237235
let rangeEndDate: number | null;
238236

@@ -252,6 +250,19 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
252250
this._changeDetectorRef.markForCheck();
253251
}
254252

253+
/** Handles focus events on a cell in the calendar body. */
254+
_handleCalendarBodyDateFocused(event: MatCalendarUserEvent<number>) {
255+
const month = event.value;
256+
const oldActiveDate = this._activeDate;
257+
this.activeDate = this._getDateFromDayOfMonth(month);
258+
259+
if (this._dateAdapter.compareDate(oldActiveDate, this.activeDate)) {
260+
this.activeDateChange.emit(this._activeDate);
261+
262+
this._focusActiveCellAfterViewChecked();
263+
}
264+
}
265+
255266
/** Handles keydown events on the calendar body when calendar is in month view. */
256267
_handleCalendarBodyKeydown(event: KeyboardEvent): void {
257268
// TODO(mmalerba): We currently allow keyboard navigation to disabled dates, but just prevent
@@ -327,9 +338,10 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
327338

328339
if (this._dateAdapter.compareDate(oldActiveDate, this.activeDate)) {
329340
this.activeDateChange.emit(this.activeDate);
341+
342+
this._focusActiveCellAfterViewChecked();
330343
}
331344

332-
this._focusActiveCell();
333345
// Prevent unexpected default actions such as form submission.
334346
event.preventDefault();
335347
}
@@ -376,6 +388,11 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
376388
this._matCalendarBody._focusActiveCell(movePreview);
377389
}
378390

391+
/** Focuses the active cell after change detection has run and the microtask queue is empty. */
392+
_focusActiveCellAfterViewChecked() {
393+
this._matCalendarBody._scheduleFocusActiveCellAfterViewChecked();
394+
}
395+
379396
/** Called when the user has activated a new cell and the preview needs to be updated. */
380397
_previewChanged({event, value: cell}: MatCalendarUserEvent<MatCalendarCell<D> | null>) {
381398
if (this._rangeStrategy) {
@@ -398,6 +415,18 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
398415
}
399416
}
400417

418+
/**
419+
* Takes a day of the month and returns a new date in the same month and year as the currently
420+
* active date. The returned date will have the same day of the month as the argument date.
421+
*/
422+
private _getDateFromDayOfMonth(dayOfMonth: number): D {
423+
return this._dateAdapter.createDate(
424+
this._dateAdapter.getYear(this.activeDate),
425+
this._dateAdapter.getMonth(this.activeDate),
426+
dayOfMonth,
427+
);
428+
}
429+
401430
/** Initializes the weekdays. */
402431
private _initWeekdays() {
403432
const firstDayOfWeek = this._dateAdapter.getFirstDayOfWeek();

src/material/datepicker/multi-year-view.html

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
[cellAspectRatio]="4 / 7"
1212
[activeCell]="_getActiveCell()"
1313
(selectedValueChange)="_yearSelected($event)"
14+
(activeDateChange)="_handleCalendarBodyDateFocused($event)"
1415
(keyup)="_handleCalendarBodyKeyup($event)"
1516
(keydown)="_handleCalendarBodyKeydown($event)">
1617
</tbody>

src/material/datepicker/multi-year-view.spec.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {dispatchFakeEvent, dispatchKeyboardEvent} from '../../cdk/testing/privat
1313
import {Component, ViewChild} from '@angular/core';
1414
import {waitForAsync, ComponentFixture, TestBed} from '@angular/core/testing';
1515
import {MatNativeDateModule} from '@angular/material/core';
16-
import {JAN} from '../testing';
16+
import {JAN, MAR} from '../testing';
1717
import {By} from '@angular/platform-browser';
1818
import {MatCalendarBody} from './calendar-body';
1919
import {MatMultiYearView, yearsPerPage, yearsPerRow} from './multi-year-view';
@@ -216,6 +216,34 @@ describe('MatMultiYearView', () => {
216216

217217
expect(calendarInstance.date).toEqual(new Date(2017 + yearsPerPage * 2, JAN, 1));
218218
});
219+
220+
it('should go to the year that is focused', () => {
221+
fixture.componentInstance.date = new Date(2017, MAR, 5);
222+
fixture.detectChanges();
223+
expect(calendarInstance.date).toEqual(new Date(2017, MAR, 5));
224+
225+
const year2022Cell = fixture.debugElement.nativeElement.querySelector(
226+
'[data-mat-row="1"][data-mat-col="2"] button',
227+
) as HTMLElement;
228+
229+
dispatchFakeEvent(year2022Cell, 'focus');
230+
fixture.detectChanges();
231+
232+
expect(calendarInstance.date).toEqual(new Date(2022, MAR, 5));
233+
});
234+
235+
it('should not call `.focus()` when the active date is focused', () => {
236+
const year2017Cell = fixture.debugElement.nativeElement.querySelector(
237+
'[data-mat-row="0"][data-mat-col="1"] button',
238+
) as HTMLElement;
239+
const focusSpy = (year2017Cell.focus = jasmine.createSpy('cellFocused'));
240+
241+
dispatchFakeEvent(year2017Cell, 'focus');
242+
fixture.detectChanges();
243+
244+
expect(calendarInstance.date).toEqual(new Date(2017, JAN, 1));
245+
expect(focusSpy).not.toHaveBeenCalled();
246+
});
219247
});
220248
});
221249
});

src/material/datepicker/multi-year-view.ts

+40-13
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,23 @@ export class MatMultiYearView<D> implements AfterContentInit, OnDestroy {
204204
/** Handles when a new year is selected. */
205205
_yearSelected(event: MatCalendarUserEvent<number>) {
206206
const year = event.value;
207-
this.yearSelected.emit(this._dateAdapter.createDate(year, 0, 1));
208-
let month = this._dateAdapter.getMonth(this.activeDate);
209-
let daysInMonth = this._dateAdapter.getNumDaysInMonth(
210-
this._dateAdapter.createDate(year, month, 1),
211-
);
212-
this.selectedChange.emit(
213-
this._dateAdapter.createDate(
214-
year,
215-
month,
216-
Math.min(this._dateAdapter.getDate(this.activeDate), daysInMonth),
217-
),
218-
);
207+
const selectedYear = this._dateAdapter.createDate(year, 0, 1);
208+
const selectedDate = this._getDateFromYear(year);
209+
210+
this.yearSelected.emit(selectedYear);
211+
this.selectedChange.emit(selectedDate);
212+
}
213+
214+
/** Handles focus events on a cell in the calendar body. */
215+
_handleCalendarBodyDateFocused(event: MatCalendarUserEvent<number>) {
216+
const year = event.value;
217+
const oldActiveDate = this._activeDate;
218+
219+
this.activeDate = this._getDateFromYear(year);
220+
if (this._dateAdapter.compareDate(oldActiveDate, this.activeDate)) {
221+
this.activeDateChange.emit(this.activeDate);
222+
this._focusActiveCellAfterViewChecked();
223+
}
219224
}
220225

221226
/** Handles keydown events on the calendar body when calendar is in multi-year view. */
@@ -278,7 +283,7 @@ export class MatMultiYearView<D> implements AfterContentInit, OnDestroy {
278283
this.activeDateChange.emit(this.activeDate);
279284
}
280285

281-
this._focusActiveCell();
286+
this._focusActiveCellAfterViewChecked();
282287
// Prevent unexpected default actions such as form submission.
283288
event.preventDefault();
284289
}
@@ -303,6 +308,28 @@ export class MatMultiYearView<D> implements AfterContentInit, OnDestroy {
303308
this._matCalendarBody._focusActiveCell();
304309
}
305310

311+
/** Focuses the active cell after change detection has run and the microtask queue is empty. */
312+
_focusActiveCellAfterViewChecked() {
313+
this._matCalendarBody._scheduleFocusActiveCellAfterViewChecked();
314+
}
315+
316+
/**
317+
* Takes a year and returns a new date on the same day and month as the currently active date
318+
* The returned date will have the same year as the argument date.
319+
*/
320+
private _getDateFromYear(year: number) {
321+
const activeMonth = this._dateAdapter.getMonth(this.activeDate);
322+
const daysInMonth = this._dateAdapter.getNumDaysInMonth(
323+
this._dateAdapter.createDate(year, activeMonth, 1),
324+
);
325+
const normalizedDate = this._dateAdapter.createDate(
326+
year,
327+
activeMonth,
328+
Math.min(this._dateAdapter.getDate(this.activeDate), daysInMonth),
329+
);
330+
return normalizedDate;
331+
}
332+
306333
/** Creates an MatCalendarCell for the given year. */
307334
private _createCellForYear(year: number) {
308335
const date = this._dateAdapter.createDate(year, 0, 1);

src/material/datepicker/year-view.html

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
[cellAspectRatio]="4 / 7"
1414
[activeCell]="_dateAdapter.getMonth(activeDate)"
1515
(selectedValueChange)="_monthSelected($event)"
16+
(activeDateChange)="_handleCalendarBodyDateFocused($event)"
1617
(keyup)="_handleCalendarBodyKeyup($event)"
1718
(keydown)="_handleCalendarBodyKeydown($event)">
1819
</tbody>

src/material/datepicker/year-view.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,30 @@ describe('MatYearView', () => {
292292

293293
expect(calendarInstance.date).toEqual(new Date(2018, FEB, 28));
294294
});
295+
296+
it('should go to date that is focused', () => {
297+
const juneCell = fixture.debugElement.nativeElement.querySelector(
298+
'[data-mat-row="1"][data-mat-col="1"] button',
299+
) as HTMLElement;
300+
301+
dispatchFakeEvent(juneCell, 'focus');
302+
fixture.detectChanges();
303+
304+
expect(calendarInstance.date).toEqual(new Date(2017, JUN, 5));
305+
});
306+
307+
it('should not call `.focus()` when the active date is focused', () => {
308+
const janCell = fixture.debugElement.nativeElement.querySelector(
309+
'[data-mat-row="0"][data-mat-col="0"] button',
310+
) as HTMLElement;
311+
const focusSpy = (janCell.focus = jasmine.createSpy('cellFocused'));
312+
313+
dispatchFakeEvent(janCell, 'focus');
314+
fixture.detectChanges();
315+
316+
expect(calendarInstance.date).toEqual(new Date(2017, JAN, 5));
317+
expect(focusSpy).not.toHaveBeenCalled();
318+
});
295319
});
296320
});
297321
});

0 commit comments

Comments
 (0)