Skip to content

Commit 34869f0

Browse files
committed
fix(material/autocomplete): remove dependency on NgClass
We can set classes directly through `[class]` instead of having to import `NgClass`.
1 parent 9cb0761 commit 34869f0

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

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

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

245223
/** Whether checkmark indicator for single-selection options is hidden. */
246224
@Input({transform: booleanAttribute})
@@ -326,7 +304,6 @@ export class MatAutocomplete implements AfterContentInit, OnDestroy {
326304
/** Panel should hide itself when the option list is empty. */
327305
_setVisibility() {
328306
this.showPanel = !!this.options.length;
329-
this._setVisibilityClasses(this._classList);
330307
this._changeDetectorRef.markForCheck();
331308
}
332309

@@ -346,19 +323,6 @@ export class MatAutocomplete implements AfterContentInit, OnDestroy {
346323
return this.ariaLabelledby ? labelExpression + this.ariaLabelledby : labelId;
347324
}
348325

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