Skip to content

Commit bc17e72

Browse files
committed
refactor(material/core): use ID generator
Switches to using the ID generator service to create unique IDs.
1 parent 81de147 commit bc17e72

File tree

2 files changed

+4
-12
lines changed

2 files changed

+4
-12
lines changed

src/material/core/option/optgroup.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
booleanAttribute,
1616
inject,
1717
} from '@angular/core';
18+
import {_IdGenerator} from '@angular/cdk/a11y';
1819
import {MatOptionParentComponent, MAT_OPTION_PARENT_COMPONENT} from './option-parent';
1920

2021
// Notes on the accessibility pattern used for `mat-optgroup`.
@@ -37,9 +38,6 @@ import {MatOptionParentComponent, MAT_OPTION_PARENT_COMPONENT} from './option-pa
3738
// 3. `<mat-option aria-labelledby="optionLabel groupLabel"` - This works on Chrome, but Safari
3839
// doesn't read out the text at all. Furthermore, on
3940

40-
// Counter for unique group ids.
41-
let _uniqueOptgroupIdCounter = 0;
42-
4341
/**
4442
* Injection token that can be used to reference instances of `MatOptgroup`. It serves as
4543
* alternative token to the actual `MatOptgroup` class which could cause unnecessary
@@ -73,7 +71,7 @@ export class MatOptgroup {
7371
@Input({transform: booleanAttribute}) disabled: boolean = false;
7472

7573
/** Unique id for the underlying label. */
76-
_labelId: string = `mat-optgroup-label-${_uniqueOptgroupIdCounter++}`;
74+
_labelId: string = inject(_IdGenerator).getId('mat-optgroup-label-');
7775

7876
/** Whether the group is in inert a11y mode. */
7977
_inert: boolean;

src/material/core/option/option.ts

Lines changed: 2 additions & 8 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 {FocusableOption, FocusOrigin} from '@angular/cdk/a11y';
9+
import {_IdGenerator, FocusableOption, FocusOrigin} from '@angular/cdk/a11y';
1010
import {ENTER, hasModifierKey, SPACE} from '@angular/cdk/keycodes';
1111
import {
1212
Component,
@@ -34,12 +34,6 @@ import {MatPseudoCheckbox} from '../selection/pseudo-checkbox/pseudo-checkbox';
3434
import {_StructuralStylesLoader} from '../focus-indicators/structural-styles';
3535
import {_CdkPrivateStyleLoader, _VisuallyHiddenLoader} from '@angular/cdk/private';
3636

37-
/**
38-
* Option IDs need to be unique across components, so this counter exists outside of
39-
* the component definition.
40-
*/
41-
let _uniqueIdCounter = 0;
42-
4337
/** Event object emitted by MatOption when selected or deselected. */
4438
export class MatOptionSelectionChange<T = any> {
4539
constructor(
@@ -110,7 +104,7 @@ export class MatOption<T = any> implements FocusableOption, AfterViewChecked, On
110104
@Input() value: T;
111105

112106
/** The unique ID of the option. */
113-
@Input() id: string = `mat-option-${_uniqueIdCounter++}`;
107+
@Input() id: string = inject(_IdGenerator).getId('mat-option-');
114108

115109
/** Whether the option is disabled. */
116110
@Input({transform: booleanAttribute})

0 commit comments

Comments
 (0)