Skip to content

Commit 0a6cf2f

Browse files
committed
refactor(material/tabs): use ID generator
Switches to using the ID generator service to create unique IDs.
1 parent 6996851 commit 0a6cf2f

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/material/tabs/tab-group.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,12 @@ import {ThemePalette, MatRipple} from '@angular/material/core';
3232
import {merge, Subscription} from 'rxjs';
3333
import {MAT_TABS_CONFIG, MatTabsConfig} from './tab-config';
3434
import {startWith} from 'rxjs/operators';
35-
import {CdkMonitorFocus, FocusOrigin} from '@angular/cdk/a11y';
35+
import {_IdGenerator, CdkMonitorFocus, FocusOrigin} from '@angular/cdk/a11y';
3636
import {MatTabBody} from './tab-body';
3737
import {CdkPortalOutlet} from '@angular/cdk/portal';
3838
import {MatTabLabelWrapper} from './tab-label-wrapper';
3939
import {Platform} from '@angular/cdk/platform';
4040

41-
/** Used to generate unique ID's for each tab component */
42-
let nextId = 0;
43-
4441
/** @docs-private */
4542
export interface MatTabGroupBaseHeader {
4643
_alignInkBarToSelectedTab(): void;
@@ -268,7 +265,7 @@ export class MatTabGroup implements AfterContentInit, AfterContentChecked, OnDes
268265
@Output() readonly selectedTabChange: EventEmitter<MatTabChangeEvent> =
269266
new EventEmitter<MatTabChangeEvent>(true);
270267

271-
private _groupId: number;
268+
private _groupId: string;
272269

273270
/** Whether the tab group is rendered on the server. */
274271
protected _isServer: boolean = !inject(Platform).isBrowser;
@@ -278,7 +275,7 @@ export class MatTabGroup implements AfterContentInit, AfterContentChecked, OnDes
278275
constructor() {
279276
const defaultConfig = inject<MatTabsConfig>(MAT_TABS_CONFIG, {optional: true});
280277

281-
this._groupId = nextId++;
278+
this._groupId = inject(_IdGenerator).getId('mat-tab-group-');
282279
this.animationDuration =
283280
defaultConfig && defaultConfig.animationDuration ? defaultConfig.animationDuration : '500ms';
284281
this.disablePagination =
@@ -492,12 +489,12 @@ export class MatTabGroup implements AfterContentInit, AfterContentChecked, OnDes
492489

493490
/** Returns a unique id for each tab label element */
494491
_getTabLabelId(i: number): string {
495-
return `mat-tab-label-${this._groupId}-${i}`;
492+
return `${this._groupId}-label-${i}`;
496493
}
497494

498495
/** Returns a unique id for each tab content element */
499496
_getTabContentId(i: number): string {
500-
return `mat-tab-content-${this._groupId}-${i}`;
497+
return `${this._groupId}-content-${i}`;
501498
}
502499

503500
/**

src/material/tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
ThemePalette,
3737
_StructuralStylesLoader,
3838
} from '@angular/material/core';
39-
import {FocusableOption, FocusMonitor} from '@angular/cdk/a11y';
39+
import {_IdGenerator, FocusableOption, FocusMonitor} from '@angular/cdk/a11y';
4040
import {Directionality} from '@angular/cdk/bidi';
4141
import {ViewportRuler} from '@angular/cdk/scrolling';
4242
import {Platform} from '@angular/cdk/platform';
@@ -49,9 +49,6 @@ import {MatPaginatedTabHeader} from '../paginated-tab-header';
4949
import {CdkObserveContent} from '@angular/cdk/observers';
5050
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
5151

52-
// Increasing integer for generating unique ids for tab nav components.
53-
let nextUniqueId = 0;
54-
5552
/**
5653
* Navigation component matching the styles of the tab group header.
5754
* Provides anchored navigation with animated ink bar.
@@ -329,7 +326,7 @@ export class MatTabLink
329326
}
330327

331328
/** Unique id for the tab. */
332-
@Input() id = `mat-tab-link-${nextUniqueId++}`;
329+
@Input() id: string = inject(_IdGenerator).getId('mat-tab-link-');
333330

334331
constructor(...args: unknown[]);
335332

@@ -444,7 +441,7 @@ export class MatTabLink
444441
})
445442
export class MatTabNavPanel {
446443
/** Unique id for the tab panel. */
447-
@Input() id = `mat-tab-nav-panel-${nextUniqueId++}`;
444+
@Input() id: string = inject(_IdGenerator).getId('mat-tab-nav-panel-');
448445

449446
/** Id of the active tab in the nav bar. */
450447
_activeTabId?: string;

0 commit comments

Comments
 (0)