Skip to content

Commit d3ea71d

Browse files
authored
fix(material/autocomplete): remove dependency on NgClass (#28849)
We can set classes directly through `[class]` instead of having to import `NgClass`.
1 parent 2f17c69 commit d3ea71d

File tree

3 files changed

+12
-44
lines changed

3 files changed

+12
-44
lines changed

src/material/autocomplete/autocomplete.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
class="mat-mdc-autocomplete-panel mdc-menu-surface mdc-menu-surface--open"
44
role="listbox"
55
[id]="id"
6-
[ngClass]="_classList"
6+
[class]="_classList"
7+
[class.mat-mdc-autocomplete-visible]="showPanel"
8+
[class.mat-mdc-autocomplete-hidden]="!showPanel"
9+
[class.mat-primary]="_color === 'primary'"
10+
[class.mat-accent]="_color === 'accent'"
11+
[class.mat-warn]="_color === 'warn'"
712
[attr.aria-label]="ariaLabel || null"
813
[attr.aria-labelledby]="_getPanelAriaLabelledby(formFieldId)"
914
[@panelAnimation]="isOpen ? 'visible' : 'hidden'"

src/material/autocomplete/autocomplete.ts

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ import {
3434
ThemePalette,
3535
} from '@angular/material/core';
3636
import {ActiveDescendantKeyManager} from '@angular/cdk/a11y';
37-
import {coerceStringArray} from '@angular/cdk/coercion';
3837
import {Platform} from '@angular/cdk/platform';
3938
import {panelAnimation} from './animations';
4039
import {Subscription} from 'rxjs';
41-
import {NgClass} from '@angular/common';
4240

4341
/**
4442
* Autocomplete IDs need to be unique across components, so this counter exists outside of
@@ -119,17 +117,10 @@ export function MAT_AUTOCOMPLETE_DEFAULT_OPTIONS_FACTORY(): MatAutocompleteDefau
119117
providers: [{provide: MAT_OPTION_PARENT_COMPONENT, useExisting: MatAutocomplete}],
120118
animations: [panelAnimation],
121119
standalone: true,
122-
imports: [NgClass],
123120
})
124121
export class MatAutocomplete implements AfterContentInit, OnDestroy {
125122
private _activeOptionChanges = Subscription.EMPTY;
126123

127-
/** Class to apply to the panel when it's visible. */
128-
private _visibleClass = 'mat-mdc-autocomplete-visible';
129-
130-
/** Class to apply to the panel when it's hidden. */
131-
private _hiddenClass = 'mat-mdc-autocomplete-hidden';
132-
133124
/** Emits when the panel animation is done. Null if the panel doesn't animate. */
134125
_animationDone = new EventEmitter<AnimationEvent>();
135126

@@ -151,10 +142,10 @@ export class MatAutocomplete implements AfterContentInit, OnDestroy {
151142
/** @docs-private Sets the theme color of the panel. */
152143
_setColor(value: ThemePalette) {
153144
this._color = value;
154-
this._setThemeClasses(this._classList);
145+
this._changeDetectorRef.markForCheck();
155146
}
156147
/** @docs-private theme color of the panel */
157-
private _color: ThemePalette;
148+
protected _color: ThemePalette;
158149

159150
// The @ViewChild query for TemplateRef here needs to be static because some code paths
160151
// lead to the overlay being created before change detection has finished for this component.
@@ -227,23 +218,10 @@ export class MatAutocomplete implements AfterContentInit, OnDestroy {
227218
*/
228219
@Input('class')
229220
set classList(value: string | string[]) {
230-
if (value && value.length) {
231-
this._classList = coerceStringArray(value).reduce(
232-
(classList, className) => {
233-
classList[className] = true;
234-
return classList;
235-
},
236-
{} as {[key: string]: boolean},
237-
);
238-
} else {
239-
this._classList = {};
240-
}
241-
242-
this._setVisibilityClasses(this._classList);
243-
this._setThemeClasses(this._classList);
221+
this._classList = value;
244222
this._elementRef.nativeElement.className = '';
245223
}
246-
_classList: {[key: string]: boolean} = {};
224+
_classList: string | string[];
247225

248226
/** Whether checkmark indicator for single-selection options is hidden. */
249227
@Input({transform: booleanAttribute})
@@ -329,7 +307,6 @@ export class MatAutocomplete implements AfterContentInit, OnDestroy {
329307
/** Panel should hide itself when the option list is empty. */
330308
_setVisibility() {
331309
this.showPanel = !!this.options.length;
332-
this._setVisibilityClasses(this._classList);
333310
this._changeDetectorRef.markForCheck();
334311
}
335312

@@ -349,19 +326,6 @@ export class MatAutocomplete implements AfterContentInit, OnDestroy {
349326
return this.ariaLabelledby ? labelExpression + this.ariaLabelledby : labelId;
350327
}
351328

352-
/** Sets the autocomplete visibility classes on a classlist based on the panel is visible. */
353-
private _setVisibilityClasses(classList: {[key: string]: boolean}) {
354-
classList[this._visibleClass] = this.showPanel;
355-
classList[this._hiddenClass] = !this.showPanel;
356-
}
357-
358-
/** Sets the theming classes on a classlist based on the theme of the panel. */
359-
private _setThemeClasses(classList: {[key: string]: boolean}) {
360-
classList['mat-primary'] = this._color === 'primary';
361-
classList['mat-warn'] = this._color === 'warn';
362-
classList['mat-accent'] = this._color === 'accent';
363-
}
364-
365329
// `skipPredicate` determines if key manager should avoid putting a given option in the tab
366330
// order. Allow disabled list items to receive focus via keyboard to align with WAI ARIA
367331
// recommendation.

tools/public_api_guard/material/autocomplete.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ export class MatAutocomplete implements AfterContentInit, OnDestroy {
7272
autoSelectActiveOption: boolean;
7373
set classList(value: string | string[]);
7474
// (undocumented)
75-
_classList: {
76-
[key: string]: boolean;
77-
};
75+
_classList: string | string[];
7876
readonly closed: EventEmitter<void>;
77+
protected _color: ThemePalette;
7978
// (undocumented)
8079
protected _defaults: MatAutocompleteDefaultOptions;
8180
disableRipple: boolean;

0 commit comments

Comments
 (0)