Skip to content

Commit

Permalink
feat(autocomplete): add input for adding classes to the panel
Browse files Browse the repository at this point in the history
implemented simalar to select's panel class feature. fixes angular#4196.
  • Loading branch information
davidreher committed Aug 24, 2017
1 parent 595cffd commit 83a7ccf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/lib/autocomplete/autocomplete.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<ng-template>
<div class="mat-autocomplete-panel" role="listbox" [id]="id" [ngClass]="_getClassList()" #panel>
<div
class="mat-autocomplete-panel"
role="listbox"
[id]="id"
[ngClass]="panelClass"
[class.mat-autocomplete-visible]="showPanel"
[class.mat-autocomplete-hidden]="!showPanel"
#panel>
<ng-content></ng-content>
</div>
</ng-template>
16 changes: 15 additions & 1 deletion src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,19 @@ describe('MdAutocomplete', () => {
});
}));

it('should be able to set extra classes on the panel', () => {
fixture.componentInstance.trigger.openPanel();
fixture.whenStable().then(() => {
fixture.detectChanges();

const panel =
overlayContainerElement.querySelector('.mat-autocomplete-panel') as HTMLElement;

expect(panel.classList).toContain('custom-one');
expect(panel.classList).toContain('custom-two');
});
});

it('should keep the label floating until the panel closes', async(() => {
fixture.componentInstance.trigger.openPanel();
expect(fixture.componentInstance.formField.floatPlaceholder)
Expand Down Expand Up @@ -1556,7 +1569,7 @@ describe('MdAutocomplete', () => {
<input mdInput placeholder="State" [mdAutocomplete]="auto" [formControl]="stateCtrl">
</md-form-field>
<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayFn">
<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayFn" [panelClass]="panelClass">
<md-option *ngFor="let state of filteredStates" [value]="state">
<span> {{ state.code }}: {{ state.name }} </span>
</md-option>
Expand All @@ -1569,6 +1582,7 @@ class SimpleAutocomplete implements OnDestroy {
valueSub: Subscription;
placeholder = 'auto';
width: number;
panelClass = ['custom-one', 'custom-two'];

@ViewChild(MdAutocompleteTrigger) trigger: MdAutocompleteTrigger;
@ViewChild(MdAutocomplete) panel: MdAutocomplete;
Expand Down
11 changes: 3 additions & 8 deletions src/lib/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export class MdAutocomplete implements AfterContentInit {
/** Function that maps an option's control value to its display value in the trigger. */
@Input() displayWith: ((value: any) => string) | null = null;

/** Classes to be passed to the select panel. Supports the same syntax as `ngClass`. */
@Input() panelClass: string|string[]|Set<string>|{[key: string]: any};

/** Unique ID to be used by autocomplete trigger's "aria-owns" property. */
id: string = `md-autocomplete-${_uniqueAutocompleteIdCounter++}`;

Expand Down Expand Up @@ -95,13 +98,5 @@ export class MdAutocomplete implements AfterContentInit {
});
}

/** Sets a class on the panel based on whether it is visible. */
_getClassList() {
return {
'mat-autocomplete-visible': this.showPanel,
'mat-autocomplete-hidden': !this.showPanel
};
}

}

0 comments on commit 83a7ccf

Please sign in to comment.