Skip to content

Commit 8c073c5

Browse files
committed
refactor(material/dialog): use ID generator
Switches to using the ID generator service to create unique IDs.
1 parent bc17e72 commit 8c073c5

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

src/material/dialog/dialog-content-directives.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ import {
1616
SimpleChanges,
1717
inject,
1818
} from '@angular/core';
19+
import {_IdGenerator} from '@angular/cdk/a11y';
1920
import {CdkScrollable} from '@angular/cdk/scrolling';
2021

2122
import {MatDialog} from './dialog';
2223
import {_closeDialogVia, MatDialogRef} from './dialog-ref';
2324

24-
/** Counter used to generate unique IDs for dialog elements. */
25-
let dialogElementUid = 0;
26-
2725
/**
2826
* Button that will close the current dialog.
2927
*/
@@ -137,7 +135,7 @@ export abstract class MatDialogLayoutSection implements OnInit, OnDestroy {
137135
},
138136
})
139137
export class MatDialogTitle extends MatDialogLayoutSection {
140-
@Input() id: string = `mat-mdc-dialog-title-${dialogElementUid++}`;
138+
@Input() id: string = inject(_IdGenerator).getId('mat-mdc-dialog-title-');
141139

142140
protected _onAdd() {
143141
// Note: we null check the queue, because there are some internal

src/material/dialog/dialog.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {MatDialogRef} from './dialog-ref';
2222
import {defer, Observable, Subject} from 'rxjs';
2323
import {Dialog, DialogConfig} from '@angular/cdk/dialog';
2424
import {startWith} from 'rxjs/operators';
25+
import {_IdGenerator} from '@angular/cdk/a11y';
2526

2627
/** Injection token that can be used to access the data that was passed in to a dialog. */
2728
export const MAT_DIALOG_DATA = new InjectionToken<any>('MatMdcDialogData');
@@ -65,9 +66,6 @@ export const MAT_DIALOG_SCROLL_STRATEGY_PROVIDER = {
6566
useFactory: MAT_DIALOG_SCROLL_STRATEGY_PROVIDER_FACTORY,
6667
};
6768

68-
// Counter for unique dialog ids.
69-
let uniqueId = 0;
70-
7169
/**
7270
* Service to open Material Design modal dialogs.
7371
*/
@@ -77,6 +75,7 @@ export class MatDialog implements OnDestroy {
7775
private _defaultOptions = inject<MatDialogConfig>(MAT_DIALOG_DEFAULT_OPTIONS, {optional: true});
7876
private _scrollStrategy = inject(MAT_DIALOG_SCROLL_STRATEGY);
7977
private _parentDialog = inject(MatDialog, {optional: true, skipSelf: true});
78+
private _idGenerator = inject(_IdGenerator);
8079
protected _dialog = inject(Dialog);
8180

8281
private readonly _openDialogsAtThisLevel: MatDialogRef<any>[] = [];
@@ -154,7 +153,7 @@ export class MatDialog implements OnDestroy {
154153
): MatDialogRef<T, R> {
155154
let dialogRef: MatDialogRef<T, R>;
156155
config = {...(this._defaultOptions || new MatDialogConfig()), ...config};
157-
config.id = config.id || `mat-mdc-dialog-${uniqueId++}`;
156+
config.id = config.id || this._idGenerator.getId('mat-mdc-dialog-');
158157
config.scrollStrategy = config.scrollStrategy || this._scrollStrategy();
159158

160159
const cdkRef = this._dialog.open<R, D, T>(componentOrTemplateRef, {

src/material/dialog/testing/dialog-harness.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('MatDialogHarness', () => {
7272
fixture.componentInstance.open();
7373
fixture.componentInstance.open({ariaLabelledBy: 'dialog-label'});
7474
const dialogs = await loader.getAllHarnesses(MatDialogHarness);
75-
expect(await dialogs[0].getAriaLabelledby()).toMatch(/-dialog-title-\d+/);
75+
expect(await dialogs[0].getAriaLabelledby()).toMatch(/-dialog-title-\w+\d+/);
7676
expect(await dialogs[1].getAriaLabelledby()).toBe('dialog-label');
7777
});
7878

0 commit comments

Comments
 (0)