Skip to content

Commit e8d009f

Browse files
authored
fix(material/datepicker): adds comparison ids and aria-describedby spans (angular#30040)
* fix(material/datepicker): adds comparison ids and aria-describedby spans Updates Angular Components Datepicker calendar-body .ts and .html files to add id values for if there is a comparison start/end date and to provide values to their respective html values to provide an aria-describedby value to be announced for screenreaders when comparison dates are present. Fixes b/195600299 * refactor(material/datepicker): removes '-' for proper id recognition Updates previous fix to remove '-' from the comparison date labels to allow proper id recognition for the aria-describedby. Fixes b/195600299 * fix(material/datepicker): update goldens Updates goldens for previous fix.
1 parent 525e781 commit e8d009f

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/material/datepicker/calendar-body.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,9 @@
9090
<span [id]="_endDateLabelId" class="mat-calendar-body-hidden-label">
9191
{{endDateAccessibleName}}
9292
</span>
93+
<span [id]="_comparisonStartDateLabelId" class="mat-calendar-body-hidden-label">
94+
{{comparisonDateAccessibleName}} {{startDateAccessibleName}}
95+
</span>
96+
<span [id]="_comparisonEndDateLabelId" class="mat-calendar-body-hidden-label">
97+
{{comparisonDateAccessibleName}} {{endDateAccessibleName}}
98+
</span>

src/material/datepicker/calendar-body.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {_IdGenerator} from '@angular/cdk/a11y';
2828
import {NgClass} from '@angular/common';
2929
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
3030
import {_StructuralStylesLoader} from '@angular/material/core';
31+
import {MatDatepickerIntl} from './datepicker-intl';
3132

3233
/** Extra CSS classes that can be associated with a calendar cell. */
3334
export type MatCalendarCellCssClasses = string | string[] | Set<string> | {[key: string]: any};
@@ -99,6 +100,7 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
99100
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
100101
private _ngZone = inject(NgZone);
101102
private _platform = inject(Platform);
103+
private _intl = inject(MatDatepickerIntl);
102104

103105
/**
104106
* Used to skip the next focus event when rendering the preview range.
@@ -200,10 +202,18 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
200202
/** ID for the end date label. */
201203
_endDateLabelId: string;
202204

205+
/** ID for the comparison start date label. */
206+
_comparisonStartDateLabelId: string;
207+
208+
/** ID for the comparison end date label. */
209+
_comparisonEndDateLabelId: string;
210+
203211
private _didDragSinceMouseDown = false;
204212

205213
private _injector = inject(Injector);
206214

215+
comparisonDateAccessibleName = this._intl.comparisonDateLabel;
216+
207217
/**
208218
* Tracking function for rows based on their identity. Ideally we would use some sort of
209219
* key on the row, but that would require a breaking change for the `rows` input. We don't
@@ -216,7 +226,9 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
216226
constructor() {
217227
const idGenerator = inject(_IdGenerator);
218228
this._startDateLabelId = idGenerator.getId('mat-calendar-body-start-');
219-
this._endDateLabelId = idGenerator.getId('mat-calendar-body-start-');
229+
this._endDateLabelId = idGenerator.getId('mat-calendar-body-end-');
230+
this._comparisonStartDateLabelId = idGenerator.getId('mat-calendar-body-comparison-start-');
231+
this._comparisonEndDateLabelId = idGenerator.getId('mat-calendar-body-comparison-end-');
220232

221233
inject(_CdkPrivateStyleLoader).load(_StructuralStylesLoader);
222234

@@ -467,6 +479,17 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
467479
} else if (this.endValue === value) {
468480
return this._endDateLabelId;
469481
}
482+
483+
if (this.comparisonStart !== null && this.comparisonEnd !== null) {
484+
if (value === this.comparisonStart && value === this.comparisonEnd) {
485+
return `${this._comparisonStartDateLabelId} ${this._comparisonEndDateLabelId}`;
486+
} else if (value === this.comparisonStart) {
487+
return this._comparisonStartDateLabelId;
488+
} else if (value === this.comparisonEnd) {
489+
return this._comparisonEndDateLabelId;
490+
}
491+
}
492+
470493
return null;
471494
}
472495

src/material/datepicker/datepicker-intl.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ export class MatDatepickerIntl {
6565
*/
6666
endDateLabel = 'End date';
6767

68+
/**
69+
* A label for the Comparison date of a range of dates (used by screen readers).
70+
*/
71+
comparisonDateLabel = 'Comparison range';
72+
6873
/** Formats a range of years (used for visuals). */
6974
formatYearRange(start: string, end: string): string {
7075
return `${start} \u2013 ${end}`;

tools/public_api_guard/material/datepicker.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,12 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
196196
_cellClicked(cell: MatCalendarCell, event: MouseEvent): void;
197197
_cellPadding: string;
198198
_cellWidth: string;
199+
// (undocumented)
200+
comparisonDateAccessibleName: string;
199201
comparisonEnd: number | null;
202+
_comparisonEndDateLabelId: string;
200203
comparisonStart: number | null;
204+
_comparisonStartDateLabelId: string;
201205
readonly dragEnded: EventEmitter<MatCalendarUserEvent<D | null>>;
202206
readonly dragStarted: EventEmitter<MatCalendarUserEvent<D>>;
203207
// (undocumented)
@@ -480,6 +484,7 @@ export class MatDatepickerIntl {
480484
calendarLabel: string;
481485
readonly changes: Subject<void>;
482486
closeCalendarLabel: string;
487+
comparisonDateLabel: string;
483488
// @deprecated
484489
endDateLabel: string;
485490
formatYearRange(start: string, end: string): string;

0 commit comments

Comments
 (0)