Skip to content

Commit ef5fc3f

Browse files
committed
refactor(material/datepicker): use ID generator
Switches to using the ID generator service to create unique IDs.
1 parent 88ae57e commit ef5fc3f

File tree

6 files changed

+21
-26
lines changed

6 files changed

+21
-26
lines changed

src/material/datepicker/calendar-body.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
afterNextRender,
2525
Injector,
2626
} from '@angular/core';
27+
import {_IdGenerator} from '@angular/cdk/a11y';
2728
import {NgClass} from '@angular/common';
2829
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
2930
import {_StructuralStylesLoader} from '@angular/material/core';
@@ -63,8 +64,6 @@ export interface MatCalendarUserEvent<D> {
6364
event: Event;
6465
}
6566

66-
let calendarBodyId = 1;
67-
6867
/** Event options that can be used to bind an active, capturing event. */
6968
const activeCapturingEventOptions = normalizePassiveListenerOptions({
7069
passive: false,
@@ -195,6 +194,12 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
195194
/** Width of an individual cell. */
196195
_cellWidth: string;
197196

197+
/** ID for the start date label. */
198+
_startDateLabelId: string;
199+
200+
/** ID for the end date label. */
201+
_endDateLabelId: string;
202+
198203
private _didDragSinceMouseDown = false;
199204

200205
private _injector = inject(Injector);
@@ -209,7 +214,12 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
209214
constructor(...args: unknown[]);
210215

211216
constructor() {
217+
const idGenerator = inject(_IdGenerator);
218+
this._startDateLabelId = idGenerator.getId('mat-calendar-body-start-');
219+
this._endDateLabelId = idGenerator.getId('mat-calendar-body-start-');
220+
212221
inject(_CdkPrivateStyleLoader).load(_StructuralStylesLoader);
222+
213223
this._ngZone.runOutsideAngular(() => {
214224
const element = this._elementRef.nativeElement;
215225

@@ -597,12 +607,6 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
597607

598608
return null;
599609
}
600-
601-
private _id = `mat-calendar-body-${calendarBodyId++}`;
602-
603-
_startDateLabelId = `${this._id}-start-date`;
604-
605-
_endDateLabelId = `${this._id}-end-date`;
606610
}
607611

608612
/** Checks whether a node is a table cell element. */

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ describe('MatCalendarHeader', () => {
199199
expect(periodButton.hasAttribute('aria-label')).toBe(true);
200200
expect(periodButton.getAttribute('aria-label')).toMatch(/^[a-z0-9\s]+$/i);
201201
expect(periodButton.hasAttribute('aria-describedby')).toBe(true);
202-
expect(periodButton.getAttribute('aria-describedby')).toMatch(/mat-calendar-header-[0-9]+/i);
202+
expect(periodButton.getAttribute('aria-describedby')).toMatch(
203+
/mat-calendar-period-label-\w+[0-9]+/i,
204+
);
203205
});
204206
});
205207

src/material/datepicker/calendar.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ import {
3939
import {MatYearView} from './year-view';
4040
import {MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER, DateRange} from './date-selection-model';
4141
import {MatIconButton, MatButton} from '@angular/material/button';
42-
import {CdkMonitorFocus} from '@angular/cdk/a11y';
42+
import {_IdGenerator, CdkMonitorFocus} from '@angular/cdk/a11y';
4343
import {_CdkPrivateStyleLoader, _VisuallyHiddenLoader} from '@angular/cdk/private';
4444

45-
let calendarHeaderId = 1;
46-
4745
/**
4846
* Possible views for the calendar.
4947
* @docs-private
@@ -222,9 +220,7 @@ export class MatCalendarHeader<D> {
222220
return [minYearLabel, maxYearLabel];
223221
}
224222

225-
private _id = `mat-calendar-header-${calendarHeaderId++}`;
226-
227-
_periodButtonLabelId = `${this._id}-period-label`;
223+
_periodButtonLabelId = inject(_IdGenerator).getId('mat-calendar-period-label-');
228224
}
229225

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

src/material/datepicker/date-range-input.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {CdkMonitorFocus, FocusOrigin} from '@angular/cdk/a11y';
9+
import {_IdGenerator, CdkMonitorFocus, FocusOrigin} from '@angular/cdk/a11y';
1010
import {
1111
AfterContentInit,
1212
ChangeDetectionStrategy,
@@ -39,8 +39,6 @@ import {MatDatepickerControl, MatDatepickerPanel} from './datepicker-base';
3939
import {createMissingDateImplError} from './datepicker-errors';
4040
import {DateFilterFn, _MatFormFieldPartial, dateInputsHaveChanged} from './datepicker-input-base';
4141

42-
let nextUniqueId = 0;
43-
4442
@Component({
4543
selector: 'mat-date-range-input',
4644
templateUrl: 'date-range-input.html',
@@ -90,7 +88,7 @@ export class MatDateRangeInput<D>
9088
}
9189

9290
/** Unique ID for the group. */
93-
id = `mat-date-range-input-${nextUniqueId++}`;
91+
id: string = inject(_IdGenerator).getId('mat-date-range-input-');
9492

9593
/** Whether the control is focused. */
9694
focused = false;

src/material/datepicker/datepicker-base.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {AnimationEvent} from '@angular/animations';
10-
import {CdkTrapFocus} from '@angular/cdk/a11y';
10+
import {_IdGenerator, CdkTrapFocus} from '@angular/cdk/a11y';
1111
import {Directionality} from '@angular/cdk/bidi';
1212
import {coerceStringArray} from '@angular/cdk/coercion';
1313
import {
@@ -76,9 +76,6 @@ import {DateFilterFn} from './datepicker-input-base';
7676
import {MatDatepickerIntl} from './datepicker-intl';
7777
import {_CdkPrivateStyleLoader, _VisuallyHiddenLoader} from '@angular/cdk/private';
7878

79-
/** Used to generate a unique ID for each datepicker instance. */
80-
let datepickerUid = 0;
81-
8279
/** Injection token that determines the scroll handling while the calendar is open. */
8380
export const MAT_DATEPICKER_SCROLL_STRATEGY = new InjectionToken<() => ScrollStrategy>(
8481
'mat-datepicker-scroll-strategy',
@@ -497,7 +494,7 @@ export abstract class MatDatepickerBase<
497494
private _opened = false;
498495

499496
/** The id for the datepicker calendar. */
500-
id: string = `mat-datepicker-${datepickerUid++}`;
497+
id: string = inject(_IdGenerator).getId('mat-datepicker-');
501498

502499
/** The minimum selectable date. */
503500
_getMinDate(): D | null {

tools/public_api_guard/material/datepicker.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
203203
// (undocumented)
204204
_emitActiveDateChange(cell: MatCalendarCell, event: FocusEvent): void;
205205
endDateAccessibleName: string | null;
206-
// (undocumented)
207206
_endDateLabelId: string;
208207
endValue: number;
209208
_firstRowOffset: number;
@@ -240,7 +239,6 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
240239
_scheduleFocusActiveCellAfterViewChecked(): void;
241240
readonly selectedValueChange: EventEmitter<MatCalendarUserEvent<number>>;
242241
startDateAccessibleName: string | null;
243-
// (undocumented)
244242
_startDateLabelId: string;
245243
startValue: number;
246244
todayValue: number;

0 commit comments

Comments
 (0)