Skip to content

Commit 557e0f3

Browse files
committed
feat(material/autocomplete): add panelClass default option
1 parent 7a2a732 commit 557e0f3

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,40 @@ describe('MatAutocomplete', () => {
21192119
}));
21202120
});
21212121

2122+
describe('with panel classes in the default options', () => {
2123+
it('should apply them if provided as string', fakeAsync(() => {
2124+
const fixture = createComponent(SimpleAutocomplete, [
2125+
{provide: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,
2126+
useValue: {panelClass: 'default1 default2'}}
2127+
]);
2128+
2129+
fixture.detectChanges();
2130+
fixture.componentInstance.trigger.openPanel();
2131+
fixture.detectChanges();
2132+
2133+
const panelClassList =
2134+
overlayContainerElement.querySelector('.mat-autocomplete-panel')!.classList;
2135+
expect(panelClassList).toContain('default1');
2136+
expect(panelClassList).toContain('default2');
2137+
}));
2138+
2139+
it('should apply them if provided as array', fakeAsync(() => {
2140+
const fixture = createComponent(SimpleAutocomplete, [
2141+
{provide: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,
2142+
useValue: {panelClass: ['default1', 'default2']}}
2143+
]);
2144+
2145+
fixture.detectChanges();
2146+
fixture.componentInstance.trigger.openPanel();
2147+
fixture.detectChanges();
2148+
2149+
const panelClassList =
2150+
overlayContainerElement.querySelector('.mat-autocomplete-panel')!.classList;
2151+
expect(panelClassList).toContain('default1');
2152+
expect(panelClassList).toContain('default2');
2153+
}));
2154+
});
2155+
21222156
describe('misc', () => {
21232157

21242158
it('should allow basic use without any forms directives', () => {

src/material/autocomplete/autocomplete.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {ActiveDescendantKeyManager} from '@angular/cdk/a11y';
1010
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
11+
import {coerceArray} from '@angular/cdk/coercion/array';
1112
import {
1213
AfterContentInit,
1314
ChangeDetectionStrategy,
@@ -75,6 +76,9 @@ const _MatAutocompleteMixinBase: CanDisableRippleCtor & typeof MatAutocompleteBa
7576
export interface MatAutocompleteDefaultOptions {
7677
/** Whether the first option should be highlighted when an autocomplete panel is opened. */
7778
autoActiveFirstOption?: boolean;
79+
80+
/** Extra CSS classes to be added to the autocomplete panel. */
81+
panelClass?: string | string[];
7882
}
7983

8084
/** Injection token to be used to override the default options for `mat-autocomplete`. */
@@ -167,16 +171,13 @@ export abstract class _MatAutocompleteBase extends _MatAutocompleteMixinBase imp
167171
*/
168172
@Input('class')
169173
set classList(value: string) {
174+
this._resetClassList();
175+
this._setVisibilityClasses(this._classList);
176+
170177
if (value && value.length) {
171-
this._classList = value.split(' ').reduce((classList, className) => {
172-
classList[className.trim()] = true;
173-
return classList;
174-
}, {} as {[key: string]: boolean});
175-
} else {
176-
this._classList = {};
178+
this._classList[value] = true;
177179
}
178180

179-
this._setVisibilityClasses(this._classList);
180181
this._elementRef.nativeElement.className = '';
181182
}
182183
_classList: {[key: string]: boolean} = {};
@@ -187,10 +188,11 @@ export abstract class _MatAutocompleteBase extends _MatAutocompleteMixinBase imp
187188
constructor(
188189
private _changeDetectorRef: ChangeDetectorRef,
189190
private _elementRef: ElementRef<HTMLElement>,
190-
@Inject(MAT_AUTOCOMPLETE_DEFAULT_OPTIONS) defaults: MatAutocompleteDefaultOptions) {
191+
@Inject(MAT_AUTOCOMPLETE_DEFAULT_OPTIONS) private _defaults: MatAutocompleteDefaultOptions) {
191192
super();
192193

193-
this._autoActiveFirstOption = !!defaults.autoActiveFirstOption;
194+
this._autoActiveFirstOption = !!_defaults.autoActiveFirstOption;
195+
this._resetClassList();
194196
}
195197

196198
ngAfterContentInit() {
@@ -207,6 +209,15 @@ export abstract class _MatAutocompleteBase extends _MatAutocompleteMixinBase imp
207209
this._activeOptionChanges.unsubscribe();
208210
}
209211

212+
/** Sets the default value for the classes applied to the panel. */
213+
_resetClassList() {
214+
this._classList = {};
215+
216+
if (this._defaults.panelClass) {
217+
this._classList[coerceArray(this._defaults.panelClass).join(' ')] = true;
218+
}
219+
}
220+
210221
/**
211222
* Sets the panel scrollTop. This allows us to manually scroll to display options
212223
* above or below the fold, as they are not actually being focused when active.

0 commit comments

Comments
 (0)