Skip to content

Commit 28dbbd5

Browse files
crisbetotinayuangao
authored andcommitted
docs(autocomplete): add docs and example for autoActiveFirstOption (#9735)
Adds some documentation and a live example for the `MatAutocomplete.autoActiveFirstOption` property.
1 parent 4523ecd commit 28dbbd5

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/lib/autocomplete/autocomplete.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ desired display value. Then bind it to the autocomplete's `displayWith` property
8282

8383
<!-- example(autocomplete-display) -->
8484

85+
### Automatically highlighting the first option
86+
87+
If your use case requires for the first autocomplete option to be highlighted when the user opens
88+
the panel, you can do so by setting the `autoActiveFirstOption` input on the `mat-autocomplete`
89+
component. This behavior can be configured globally using the `MAT_AUTOCOMPLETE_DEFAULT_OPTIONS`
90+
injection token.
91+
92+
<!-- example(autocomplete-auto-active-first-option) -->
93+
8594
### Keyboard interaction
8695
- <kbd>DOWN_ARROW</kbd>: Next option becomes active.
8796
- <kbd>UP_ARROW</kbd>: Previous option becomes active.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.example-form {
2+
min-width: 150px;
3+
max-width: 500px;
4+
width: 100%;
5+
}
6+
7+
.example-full-width {
8+
width: 100%;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<form class="example-form">
2+
<mat-form-field class="example-full-width">
3+
<input type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
4+
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete">
5+
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
6+
{{ option }}
7+
</mat-option>
8+
</mat-autocomplete>
9+
</mat-form-field>
10+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {Component} from '@angular/core';
2+
import {FormControl} from '@angular/forms';
3+
import {Observable} from 'rxjs/Observable';
4+
import {startWith} from 'rxjs/operators/startWith';
5+
import {map} from 'rxjs/operators/map';
6+
7+
/**
8+
* @title Highlight the first autocomplete option
9+
*/
10+
@Component({
11+
selector: 'autocomplete-auto-active-first-option-example',
12+
templateUrl: 'autocomplete-auto-active-first-option-example.html',
13+
styleUrls: ['autocomplete-auto-active-first-option-example.css']
14+
})
15+
export class AutocompleteAutoActiveFirstOptionExample {
16+
myControl: FormControl = new FormControl();
17+
options = ['One', 'Two', 'Three'];
18+
filteredOptions: Observable<string[]>;
19+
20+
ngOnInit() {
21+
this.filteredOptions = this.myControl.valueChanges.pipe(
22+
startWith(''),
23+
map(val => this.filter(val))
24+
);
25+
}
26+
27+
filter(val: string): string[] {
28+
return this.options.filter(option => option.toLowerCase().indexOf(val.toLowerCase()) === 0);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)