Skip to content

Commit e5f67ca

Browse files
committed
fix(material/datepicker): announce the "to" when reading year range
Create period button's `aria-description` using `formatYearRangeLabel` method. Format year range in a TTS friendly way (e.g. "2019 to 2020"). Previously, some screen readers would announce the range as "2019 2020". Fixes #23467.
1 parent 3761275 commit e5f67ca

File tree

4 files changed

+61
-36
lines changed

4 files changed

+61
-36
lines changed

src/material/datepicker/calendar-header.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
<div class="mat-calendar-controls">
33
<button mat-button type="button" class="mat-calendar-period-button"
44
(click)="currentPeriodClicked()" [attr.aria-label]="periodButtonLabel"
5-
[attr.aria-describedby]="_buttonDescriptionId"
6-
aria-live="polite">
7-
<span [attr.id]="_buttonDescriptionId">{{periodButtonText}}</span>
5+
[attr.aria-description]="periodButtonDescription" aria-live="polite">
6+
<span aria-hidden="true">{{periodButtonText}}</span>
87
<svg class="mat-calendar-arrow" [class.mat-calendar-invert]="calendar.currentView !== 'month'"
9-
viewBox="0 0 10 5" focusable="false">
8+
viewBox="0 0 10 5" focusable="false" aria-hidden="true">
109
<polygon points="0,0 5,5 10,0"/>
1110
</svg>
1211
</button>

src/material/datepicker/calendar-header.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,16 @@ describe('MatCalendarHeader', () => {
191191
});
192192

193193
it('should label and describe period button for assistive technology', () => {
194-
const description = periodButton.querySelector('span[id]');
194+
expect(calendarInstance.currentView).toBe('month');
195+
196+
periodButton.click();
197+
fixture.detectChanges();
198+
199+
expect(calendarInstance.currentView).toBe('multi-year');
195200
expect(periodButton.hasAttribute('aria-label')).toBe(true);
196-
expect(periodButton.hasAttribute('aria-describedby')).toBe(true);
197-
expect(periodButton.getAttribute('aria-describedby')).toBe(description?.getAttribute('id')!);
201+
expect(periodButton.getAttribute('aria-label')).toMatch(/^[a-z0-9\s]+$/i);
202+
expect(periodButton.hasAttribute('aria-description')).toBe(true);
203+
expect(periodButton.getAttribute('aria-description')).toMatch(/^[a-z0-9\s]+$/i);
198204
});
199205
});
200206

src/material/datepicker/calendar.ts

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ import {MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER, DateRange} from './date-select
4747
*/
4848
export type MatCalendarView = 'month' | 'year' | 'multi-year';
4949

50-
/** Counter used to generate unique IDs. */
51-
let uniqueId = 0;
52-
5350
/** Default header for MatCalendar */
5451
@Component({
5552
selector: 'mat-calendar-header',
@@ -59,8 +56,6 @@ let uniqueId = 0;
5956
changeDetection: ChangeDetectionStrategy.OnPush,
6057
})
6158
export class MatCalendarHeader<D> {
62-
_buttonDescriptionId = `mat-calendar-button-${uniqueId++}`;
63-
6459
constructor(
6560
private _intl: MatDatepickerIntl,
6661
@Inject(forwardRef(() => MatCalendar)) public calendar: MatCalendar<D>,
@@ -71,7 +66,7 @@ export class MatCalendarHeader<D> {
7166
this.calendar.stateChanges.subscribe(() => changeDetectorRef.markForCheck());
7267
}
7368

74-
/** The label for the current calendar view. */
69+
/** The display text for the current calendar view. */
7570
get periodButtonText(): string {
7671
if (this.calendar.currentView == 'month') {
7772
return this._dateAdapter
@@ -82,28 +77,26 @@ export class MatCalendarHeader<D> {
8277
return this._dateAdapter.getYearName(this.calendar.activeDate);
8378
}
8479

85-
// The offset from the active year to the "slot" for the starting year is the
86-
// *actual* first rendered year in the multi-year view, and the last year is
87-
// just yearsPerPage - 1 away.
88-
const activeYear = this._dateAdapter.getYear(this.calendar.activeDate);
89-
const minYearOfPage =
90-
activeYear -
91-
getActiveOffset(
92-
this._dateAdapter,
93-
this.calendar.activeDate,
94-
this.calendar.minDate,
95-
this.calendar.maxDate,
96-
);
97-
const maxYearOfPage = minYearOfPage + yearsPerPage - 1;
98-
const minYearName = this._dateAdapter.getYearName(
99-
this._dateAdapter.createDate(minYearOfPage, 0, 1),
100-
);
101-
const maxYearName = this._dateAdapter.getYearName(
102-
this._dateAdapter.createDate(maxYearOfPage, 0, 1),
103-
);
104-
return this._intl.formatYearRange(minYearName, maxYearName);
80+
return this._intl.formatYearRange(...this._formatMinAndMaxYearLabels());
81+
}
82+
83+
/** The `aria-description` for the current calendar view. */
84+
get periodButtonDescription(): string {
85+
if (this.calendar.currentView == 'month') {
86+
return this._dateAdapter
87+
.format(this.calendar.activeDate, this._dateFormats.display.monthYearLabel)
88+
.toLocaleUpperCase();
89+
}
90+
if (this.calendar.currentView == 'year') {
91+
return this._dateAdapter.getYearName(this.calendar.activeDate);
92+
}
93+
94+
// Format a label for the window of years displayed in the multi-year calendar view. Use
95+
// `formatYearRangeLabel` because it is TTS friendly.
96+
return this._intl.formatYearRangeLabel(...this._formatMinAndMaxYearLabels());
10597
}
10698

99+
/** The `aria-label` for changing the calendar view. */
107100
get periodButtonLabel(): string {
108101
return this.calendar.currentView == 'month'
109102
? this._intl.switchToMultiYearViewLabel
@@ -192,6 +185,35 @@ export class MatCalendarHeader<D> {
192185
this.calendar.maxDate,
193186
);
194187
}
188+
189+
/**
190+
* Format two individual labels for the minimum year and maximum year available in the multi-year
191+
* calendar view. Returns an array of two strings where the first string is the formatted label
192+
* for the minimum year, and the second string is the formatted label for the maximum year.
193+
*/
194+
private _formatMinAndMaxYearLabels(): [minYearLabel: string, maxYearLabel: string] {
195+
// The offset from the active year to the "slot" for the starting year is the
196+
// *actual* first rendered year in the multi-year view, and the last year is
197+
// just yearsPerPage - 1 away.
198+
const activeYear = this._dateAdapter.getYear(this.calendar.activeDate);
199+
const minYearOfPage =
200+
activeYear -
201+
getActiveOffset(
202+
this._dateAdapter,
203+
this.calendar.activeDate,
204+
this.calendar.minDate,
205+
this.calendar.maxDate,
206+
);
207+
const maxYearOfPage = minYearOfPage + yearsPerPage - 1;
208+
const minYearLabel = this._dateAdapter.getYearName(
209+
this._dateAdapter.createDate(minYearOfPage, 0, 1),
210+
);
211+
const maxYearLabel = this._dateAdapter.getYearName(
212+
this._dateAdapter.createDate(maxYearOfPage, 0, 1),
213+
);
214+
215+
return [minYearLabel, maxYearLabel];
216+
}
195217
}
196218

197219
/** A calendar that is used as part of the datepicker. */

tools/public_api_guard/material/datepicker.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,12 @@ export type MatCalendarCellCssClasses = string | string[] | Set<string> | {
291291
export class MatCalendarHeader<D> {
292292
constructor(_intl: MatDatepickerIntl, calendar: MatCalendar<D>, _dateAdapter: DateAdapter<D>, _dateFormats: MatDateFormats, changeDetectorRef: ChangeDetectorRef);
293293
// (undocumented)
294-
_buttonDescriptionId: string;
295-
// (undocumented)
296294
calendar: MatCalendar<D>;
297295
currentPeriodClicked(): void;
298296
get nextButtonLabel(): string;
299297
nextClicked(): void;
300298
nextEnabled(): boolean;
301-
// (undocumented)
299+
get periodButtonDescription(): string;
302300
get periodButtonLabel(): string;
303301
get periodButtonText(): string;
304302
get prevButtonLabel(): string;

0 commit comments

Comments
 (0)