Skip to content

Commit a109af7

Browse files
committed
refactor(material/button-toggle): use ID generator
Switches to using the ID generator service to create unique IDs.
1 parent b5226c2 commit a109af7

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/material/button-toggle/button-toggle.ts

Lines changed: 4 additions & 6 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 {FocusMonitor} from '@angular/cdk/a11y';
9+
import {_IdGenerator, FocusMonitor} from '@angular/cdk/a11y';
1010
import {SelectionModel} from '@angular/cdk/collections';
1111
import {DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW, UP_ARROW, SPACE, ENTER} from '@angular/cdk/keycodes';
1212
import {
@@ -104,9 +104,6 @@ export const MAT_BUTTON_TOGGLE_GROUP_VALUE_ACCESSOR: any = {
104104
multi: true,
105105
};
106106

107-
// Counter used to generate unique IDs.
108-
let uniqueIdCounter = 0;
109-
110107
/** Change event object emitted by button toggle. */
111108
export class MatButtonToggleChange {
112109
constructor(
@@ -181,7 +178,7 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
181178
this._name = value;
182179
this._markButtonsForCheck();
183180
}
184-
private _name = `mat-button-toggle-group-${uniqueIdCounter++}`;
181+
private _name = inject(_IdGenerator).getId('mat-button-toggle-group-');
185182

186183
/** Whether the toggle group is vertical. */
187184
@Input({transform: booleanAttribute}) vertical: boolean;
@@ -562,6 +559,7 @@ export class MatButtonToggle implements OnInit, AfterViewInit, OnDestroy {
562559
private _changeDetectorRef = inject(ChangeDetectorRef);
563560
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
564561
private _focusMonitor = inject(FocusMonitor);
562+
private _idGenerator = inject(_IdGenerator);
565563

566564
private _checked = false;
567565

@@ -685,7 +683,7 @@ export class MatButtonToggle implements OnInit, AfterViewInit, OnDestroy {
685683

686684
ngOnInit() {
687685
const group = this.buttonToggleGroup;
688-
this.id = this.id || `mat-button-toggle-${uniqueIdCounter++}`;
686+
this.id = this.id || this._idGenerator.getId('mat-button-toggle-');
689687

690688
if (group) {
691689
if (group._isPrechecked(this)) {

0 commit comments

Comments
 (0)