Skip to content

Commit

Permalink
refactor(material/tabs): use ID generator
Browse files Browse the repository at this point in the history
Switches to using the ID generator service to create unique IDs.

(cherry picked from commit 0a6cf2f)
  • Loading branch information
crisbeto committed Nov 4, 2024
1 parent f2589b8 commit 20f5157
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
13 changes: 5 additions & 8 deletions src/material/tabs/tab-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ import {ThemePalette, MatRipple} from '@angular/material/core';
import {merge, Subscription} from 'rxjs';
import {MAT_TABS_CONFIG, MatTabsConfig} from './tab-config';
import {startWith} from 'rxjs/operators';
import {CdkMonitorFocus, FocusOrigin} from '@angular/cdk/a11y';
import {_IdGenerator, CdkMonitorFocus, FocusOrigin} from '@angular/cdk/a11y';
import {MatTabBody} from './tab-body';
import {CdkPortalOutlet} from '@angular/cdk/portal';
import {MatTabLabelWrapper} from './tab-label-wrapper';
import {Platform} from '@angular/cdk/platform';

/** Used to generate unique ID's for each tab component */
let nextId = 0;

/** @docs-private */
export interface MatTabGroupBaseHeader {
_alignInkBarToSelectedTab(): void;
Expand Down Expand Up @@ -268,7 +265,7 @@ export class MatTabGroup implements AfterContentInit, AfterContentChecked, OnDes
@Output() readonly selectedTabChange: EventEmitter<MatTabChangeEvent> =
new EventEmitter<MatTabChangeEvent>(true);

private _groupId: number;
private _groupId: string;

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

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

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

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

/**
Expand Down
9 changes: 3 additions & 6 deletions src/material/tabs/tab-nav-bar/tab-nav-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
ThemePalette,
_StructuralStylesLoader,
} from '@angular/material/core';
import {FocusableOption, FocusMonitor} from '@angular/cdk/a11y';
import {_IdGenerator, FocusableOption, FocusMonitor} from '@angular/cdk/a11y';
import {Directionality} from '@angular/cdk/bidi';
import {ViewportRuler} from '@angular/cdk/scrolling';
import {Platform} from '@angular/cdk/platform';
Expand All @@ -49,9 +49,6 @@ import {MatPaginatedTabHeader} from '../paginated-tab-header';
import {CdkObserveContent} from '@angular/cdk/observers';
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';

// Increasing integer for generating unique ids for tab nav components.
let nextUniqueId = 0;

/**
* Navigation component matching the styles of the tab group header.
* Provides anchored navigation with animated ink bar.
Expand Down Expand Up @@ -329,7 +326,7 @@ export class MatTabLink
}

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

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

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

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

0 comments on commit 20f5157

Please sign in to comment.