Skip to content

Commit 89300e6

Browse files
committed
refactor(material/form-field): use ID generator
Switches to using the ID generator service to create unique IDs.
1 parent 62a1cdb commit 89300e6

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

src/material/form-field/directives/error.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import {
1414
HostAttributeToken,
1515
inject,
1616
} from '@angular/core';
17-
18-
let nextUniqueId = 0;
17+
import {_IdGenerator} from '@angular/cdk/a11y';
1918

2019
/**
2120
* Injection token that can be used to reference instances of `MatError`. It serves as
@@ -35,7 +34,7 @@ export const MAT_ERROR = new InjectionToken<MatError>('MatError');
3534
providers: [{provide: MAT_ERROR, useExisting: MatError}],
3635
})
3736
export class MatError {
38-
@Input() id: string = `mat-mdc-error-${nextUniqueId++}`;
37+
@Input() id: string = inject(_IdGenerator).getId('mat-mdc-error-');
3938

4039
constructor(...args: unknown[]);
4140

src/material/form-field/directives/hint.ts

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

9-
import {Directive, Input} from '@angular/core';
10-
11-
let nextUniqueId = 0;
9+
import {Directive, inject, Input} from '@angular/core';
10+
import {_IdGenerator} from '@angular/cdk/a11y';
1211

1312
/** Hint text to be shown underneath the form field control. */
1413
@Directive({
@@ -26,5 +25,5 @@ export class MatHint {
2625
@Input() align: 'start' | 'end' = 'start';
2726

2827
/** Unique ID for the hint. Used for the aria-describedby on the form field control. */
29-
@Input() id: string = `mat-mdc-hint-${nextUniqueId++}`;
28+
@Input() id: string = inject(_IdGenerator).getId('mat-mdc-hint-');
3029
}

src/material/form-field/form-field.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
} from '@angular/core';
3535
import {AbstractControlDirective} from '@angular/forms';
3636
import {ThemePalette} from '@angular/material/core';
37+
import {_IdGenerator} from '@angular/cdk/a11y';
3738
import {Subject, Subscription, merge} from 'rxjs';
3839
import {takeUntil} from 'rxjs/operators';
3940
import {MAT_ERROR, MatError} from './directives/error';
@@ -105,8 +106,6 @@ export const MAT_FORM_FIELD_DEFAULT_OPTIONS = new InjectionToken<MatFormFieldDef
105106
'MAT_FORM_FIELD_DEFAULT_OPTIONS',
106107
);
107108

108-
let nextUniqueId = 0;
109-
110109
/** Default appearance used by the form field. */
111110
const DEFAULT_APPEARANCE: MatFormFieldAppearance = 'fill';
112111

@@ -191,6 +190,7 @@ export class MatFormField
191190
private _changeDetectorRef = inject(ChangeDetectorRef);
192191
private _dir = inject(Directionality);
193192
private _platform = inject(Platform);
193+
private _idGenerator = inject(_IdGenerator);
194194
private _defaults = inject<MatFormFieldDefaultOptions>(MAT_FORM_FIELD_DEFAULT_OPTIONS, {
195195
optional: true,
196196
});
@@ -305,10 +305,10 @@ export class MatFormField
305305
_hasTextSuffix = false;
306306

307307
// Unique id for the internal form field label.
308-
readonly _labelId = `mat-mdc-form-field-label-${nextUniqueId++}`;
308+
readonly _labelId = this._idGenerator.getId('mat-mdc-form-field-label-');
309309

310310
// Unique id for the hint label.
311-
readonly _hintLabelId = `mat-mdc-hint-${nextUniqueId++}`;
311+
readonly _hintLabelId = this._idGenerator.getId('mat-mdc-hint-');
312312

313313
/** State of the mat-hint and mat-error animations. */
314314
_subscriptAnimationState = '';

0 commit comments

Comments
 (0)