Skip to content

Commit 2af6e74

Browse files
committed
refactor(cdk/accordion): use ID generator
Switches to using the ID generator service to create unique IDs.
1 parent 938f478 commit 2af6e74

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

src/cdk/accordion/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ng_module(
1515
exclude = ["**/*.spec.ts"],
1616
),
1717
deps = [
18+
"//src/cdk/a11y",
1819
"//src/cdk/collections",
1920
"@npm//@angular/core",
2021
"@npm//rxjs",

src/cdk/accordion/accordion-item.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ import {
1717
inject,
1818
OnInit,
1919
} from '@angular/core';
20+
import {_IdGenerator} from '@angular/cdk/a11y';
2021
import {UniqueSelectionDispatcher} from '@angular/cdk/collections';
2122
import {CDK_ACCORDION, CdkAccordion} from './accordion';
2223
import {Subscription} from 'rxjs';
2324

24-
/** Used to generate unique ID for each accordion item. */
25-
let nextId = 0;
26-
2725
/**
2826
* A basic directive expected to be extended and decorated as a component. Sets up all
2927
* events and attributes needed to be managed by a CdkAccordion parent.
@@ -59,7 +57,7 @@ export class CdkAccordionItem implements OnInit, OnDestroy {
5957
@Output() readonly expandedChange: EventEmitter<boolean> = new EventEmitter<boolean>();
6058

6159
/** The unique AccordionItem id. */
62-
readonly id: string = `cdk-accordion-child-${nextId++}`;
60+
readonly id: string = inject(_IdGenerator).getId('cdk-accordion-child-');
6361

6462
/** Whether the AccordionItem is expanded. */
6563
@Input({transform: booleanAttribute})

src/cdk/accordion/accordion.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ import {
1414
OnDestroy,
1515
SimpleChanges,
1616
booleanAttribute,
17+
inject,
1718
} from '@angular/core';
19+
import {_IdGenerator} from '@angular/cdk/a11y';
1820
import {Subject} from 'rxjs';
1921

20-
/** Used to generate unique ID for each accordion. */
21-
let nextId = 0;
22-
2322
/**
2423
* Injection token that can be used to reference instances of `CdkAccordion`. It serves
2524
* as alternative token to the actual `CdkAccordion` class which could cause unnecessary
@@ -43,7 +42,7 @@ export class CdkAccordion implements OnDestroy, OnChanges {
4342
readonly _openCloseAllActions: Subject<boolean> = new Subject<boolean>();
4443

4544
/** A readonly id value to use for unique selection coordination. */
46-
readonly id: string = `cdk-accordion-${nextId++}`;
45+
readonly id: string = inject(_IdGenerator).getId('cdk-accordion-');
4746

4847
/** Whether the accordion should allow multiple expanded accordion items simultaneously. */
4948
@Input({transform: booleanAttribute}) multi: boolean = false;

0 commit comments

Comments
 (0)