Skip to content

Commit 3f46ec4

Browse files
committed
refactor(material/checkbox): use ID generator
Switches to using the ID generator service to create unique IDs.
1 parent a109af7 commit 3f46ec4

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

src/material/checkbox/checkbox.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ describe('MatCheckbox', () => {
298298
fixture.changeDetectorRef.markForCheck();
299299
fixture.detectChanges();
300300

301-
expect(checkboxInstance.inputId).toMatch(/mat-mdc-checkbox-\d+/);
301+
expect(checkboxInstance.inputId).toMatch(/mat-mdc-checkbox-\w+\d+/);
302302
expect(inputElement.id).toBe(checkboxInstance.inputId);
303303
}));
304304

@@ -965,8 +965,8 @@ describe('MatCheckbox', () => {
965965
.queryAll(By.directive(MatCheckbox))
966966
.map(debugElement => debugElement.nativeElement.querySelector('input').id);
967967

968-
expect(firstId).toMatch(/mat-mdc-checkbox-\d+-input/);
969-
expect(secondId).toMatch(/mat-mdc-checkbox-\d+-input/);
968+
expect(firstId).toMatch(/mat-mdc-checkbox-\w+\d+-input/);
969+
expect(secondId).toMatch(/mat-mdc-checkbox-\w+\d+-input/);
970970
expect(firstId).not.toEqual(secondId);
971971
}));
972972
});

src/material/checkbox/checkbox.ts

Lines changed: 2 additions & 5 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} from '@angular/cdk/a11y';
9+
import {_IdGenerator, FocusableOption} from '@angular/cdk/a11y';
1010
import {
1111
ANIMATION_MODULE_TYPE,
1212
AfterViewInit,
@@ -77,9 +77,6 @@ export class MatCheckboxChange {
7777
checked: boolean;
7878
}
7979

80-
// Increasing integer for generating unique ids for checkbox components.
81-
let nextUniqueId = 0;
82-
8380
// Default checkbox configuration.
8481
const defaults = MAT_CHECKBOX_DEFAULT_OPTIONS_FACTORY();
8582

@@ -255,7 +252,7 @@ export class MatCheckbox
255252
this._options = this._options || defaults;
256253
this.color = this._options.color || defaults.color;
257254
this.tabIndex = tabIndex == null ? 0 : parseInt(tabIndex) || 0;
258-
this.id = this._uniqueId = `mat-mdc-checkbox-${++nextUniqueId}`;
255+
this.id = this._uniqueId = inject(_IdGenerator).getId('mat-mdc-checkbox-');
259256
this.disabledInteractive = this._options?.disabledInteractive ?? false;
260257
}
261258

0 commit comments

Comments
 (0)