Skip to content

feat(autocomplete): add input for adding classes to the panel #6618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -277,6 +277,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 @@ -1607,7 +1620,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 @@ -1620,6 +1633,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 @@ -72,6 +72,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`. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autocomplete panel instead of select panel.

@Input() panelClass: string|string[]|Set<string>|{[key: string]: any};

/** Event that is emitted whenever an option from the list is selected. */
@Output() optionSelected: EventEmitter<MdAutocompleteSelectedEvent> =
new EventEmitter<MdAutocompleteSelectedEvent>();
Expand Down Expand Up @@ -114,13 +117,5 @@ export class MdAutocomplete implements AfterContentInit {
this.optionSelected.emit(event);
}

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

}