Skip to content

Commit f6be153

Browse files
authored
refactor(material-experimental): avoid unique id conflicts (#19046)
Some of the MDC components were using the same pattern as the base components to generate unique ids, allowing for potential conflicts.
1 parent 313e3f3 commit f6be153

File tree

6 files changed

+8
-8
lines changed

6 files changed

+8
-8
lines changed

src/material-experimental/mdc-chips/chip-input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class MatChipInput implements MatChipTextControl, OnChanges {
8484
@Input() placeholder: string = '';
8585

8686
/** Unique id for the input. */
87-
@Input() id: string = `mat-chip-list-input-${nextUniqueId++}`;
87+
@Input() id: string = `mat-mdc-chip-list-input-${nextUniqueId++}`;
8888

8989
/** Whether the input is disabled. */
9090
@Input()

src/material-experimental/mdc-form-field/directives/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ let nextUniqueId = 0;
2020
}
2121
})
2222
export class MatError {
23-
@Input() id: string = `mat-error-${nextUniqueId++}`;
23+
@Input() id: string = `mat-mdc-error-${nextUniqueId++}`;
2424
}

src/material-experimental/mdc-form-field/directives/hint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ export class MatHint {
2626
@Input() align: 'start' | 'end' = 'start';
2727

2828
/** Unique ID for the hint. Used for the aria-describedby on the form field control. */
29-
@Input() id: string = `mat-hint-${nextUniqueId++}`;
29+
@Input() id: string = `mat-mdc-hint-${nextUniqueId++}`;
3030
}

src/material-experimental/mdc-form-field/form-field.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,13 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
191191
private _hintLabel = '';
192192

193193
// Unique id for the hint label.
194-
_hintLabelId: string = `mat-hint-${nextUniqueId++}`;
194+
_hintLabelId = `mat-mdc-hint-${nextUniqueId++}`;
195195

196196
// Unique id for the internal form field label.
197-
_labelId = `mat-form-field-label-${nextUniqueId++}`;
197+
_labelId = `mat-mdc-form-field-label-${nextUniqueId++}`;
198198

199199
/** State of the mat-hint and mat-error animations. */
200-
_subscriptAnimationState: string = '';
200+
_subscriptAnimationState = '';
201201

202202
/** Width of the outline notch. */
203203
_outlineNotchWidth: number;

src/material-experimental/mdc-slide-toggle/slide-toggle.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('MDC-based MatSlideToggle without forms', () => {
156156
fixture.detectChanges();
157157

158158
// Once the id binding is set to null, the id property should auto-generate a unique id.
159-
expect(inputElement.id).toMatch(/mat-slide-toggle-\d+-input/);
159+
expect(inputElement.id).toMatch(/mat-mdc-slide-toggle-\d+-input/);
160160
});
161161

162162
it('should forward the tabIndex to the underlying input', () => {

src/material-experimental/mdc-slide-toggle/slide-toggle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
9292
private _onChange = (_: any) => {};
9393
private _onTouched = () => {};
9494

95-
private _uniqueId: string = `mat-slide-toggle-${++nextUniqueId}`;
95+
private _uniqueId: string = `mat-mdc-slide-toggle-${++nextUniqueId}`;
9696
private _required: boolean = false;
9797
private _checked: boolean = false;
9898
private _foundation: MDCSwitchFoundation;

0 commit comments

Comments
 (0)