Skip to content

Commit 43d58eb

Browse files
committed
refactor(material/input): use ID generator
Switches to using the ID generator service to create unique IDs.
1 parent 89300e6 commit 43d58eb

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

src/material/input/input.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ describe('MatMdcInput without forms', () => {
607607
fixture.componentInstance.formControl.markAsTouched();
608608
fixture.componentInstance.formControl.setErrors({invalid: true});
609609
fixture.detectChanges();
610-
expect(input.getAttribute('aria-describedby')).toMatch(/^custom-error mat-mdc-error-\d+$/);
610+
expect(input.getAttribute('aria-describedby')).toMatch(/^custom-error mat-mdc-error-\w+\d+$/);
611611

612612
fixture.componentInstance.label = '';
613613
fixture.componentInstance.userDescribedByValue = '';

src/material/input/input.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
OnDestroy,
2626
WritableSignal,
2727
} from '@angular/core';
28+
import {_IdGenerator} from '@angular/cdk/a11y';
2829
import {FormGroupDirective, NgControl, NgForm, Validators} from '@angular/forms';
2930
import {ErrorStateMatcher, _ErrorStateTracker} from '@angular/material/core';
3031
import {MatFormFieldControl, MatFormField, MAT_FORM_FIELD} from '@angular/material/form-field';
@@ -45,8 +46,6 @@ const MAT_INPUT_INVALID_TYPES = [
4546
'submit',
4647
];
4748

48-
let nextUniqueId = 0;
49-
5049
/** Object that can be used to configure the default options for the input. */
5150
export interface MatInputConfig {
5251
/** Whether disabled inputs should be interactive. */
@@ -103,7 +102,7 @@ export class MatInput
103102
private _ngZone = inject(NgZone);
104103
protected _formField? = inject<MatFormField>(MAT_FORM_FIELD, {optional: true});
105104

106-
protected _uid = `mat-input-${nextUniqueId++}`;
105+
protected _uid = inject(_IdGenerator).getId('mat-input-');
107106
protected _previousNativeValue: any;
108107
private _inputValueAccessor: {value: any};
109108
private _signalBasedValueAccessor?: {value: WritableSignal<any>};

src/material/input/testing/input-harness.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ describe('MatInputHarness', () => {
6666
it('should be able to get id of input', async () => {
6767
const inputs = await loader.getAllHarnesses(MatInputHarness);
6868
expect(inputs.length).toBe(7);
69-
expect(await inputs[0].getId()).toMatch(/mat-input-\d+/);
70-
expect(await inputs[1].getId()).toMatch(/mat-input-\d+/);
69+
expect(await inputs[0].getId()).toMatch(/mat-input-\w+\d+/);
70+
expect(await inputs[1].getId()).toMatch(/mat-input-\w+\d+/);
7171
expect(await inputs[2].getId()).toBe('myTextarea');
7272
expect(await inputs[3].getId()).toBe('nativeControl');
73-
expect(await inputs[4].getId()).toMatch(/mat-input-\d+/);
73+
expect(await inputs[4].getId()).toMatch(/mat-input-\w+\d+/);
7474
expect(await inputs[5].getId()).toBe('has-ng-model');
7575
});
7676

0 commit comments

Comments
 (0)