Skip to content

Commit 40072a1

Browse files
amcdnlandrewseguin
authored andcommitted
docs(demos): autocomplete examples (#5787)
1 parent 63411dc commit 40072a1

14 files changed

+250
-140
lines changed

src/lib/autocomplete/autocomplete.md

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ local template variable (here we called it "auto"), and binding that variable to
4343
</md-autocomplete>
4444
```
4545

46+
<!-- example(autocomplete-simple) -->
47+
4648
### Adding a custom filter
4749

4850
At this point, the autocomplete panel should be toggleable on focus and options should be selectable. But if we want
@@ -61,41 +63,7 @@ Below we are also priming our value change stream with `null` so that the option
6163
This is especially helpful for screenreader users if you're using a non-standard filter that doesn't limit matches
6264
to the beginning of the string.
6365

64-
*my-comp.ts*
65-
```ts
66-
class MyComp {
67-
myControl = new FormControl();
68-
options = [
69-
'One',
70-
'Two',
71-
'Three'
72-
];
73-
filteredOptions: Observable<string[]>;
74-
75-
ngOnInit() {
76-
this.filteredOptions = this.myControl.valueChanges
77-
.startWith(null)
78-
.map(val => val ? this.filter(val) : this.options.slice());
79-
}
80-
81-
filter(val: string): string[] {
82-
return this.options.filter(option => new RegExp(`^${val}`, 'gi').test(option));
83-
}
84-
}
85-
```
86-
87-
*my-comp.html*
88-
```html
89-
<md-input-container>
90-
<input type="text" mdInput [formControl]="myControl" [mdAutocomplete]="auto">
91-
</md-input-container>
92-
93-
<md-autocomplete #auto="mdAutocomplete">
94-
<md-option *ngFor="let option of filteredOptions | async" [value]="option">
95-
{{ option }}
96-
</md-option>
97-
</md-autocomplete>
98-
```
66+
<!-- example(autocomplete-filter) -->
9967

10068
### Setting separate control and display values
10169

@@ -107,45 +75,7 @@ the option's string properties.
10775
To make this work, create a function on your component class that maps the control value to the desired display value.
10876
Then bind it to the autocomplete's `displayWith` property.
10977

110-
```html
111-
<md-input-container>
112-
<input type="text" mdInput [formControl]="myControl" [mdAutocomplete]="auto">
113-
</md-input-container>
114-
115-
<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayFn">
116-
<md-option *ngFor="let option of filteredOptions | async" [value]="option">
117-
{{ option.name }}
118-
</md-option>
119-
</md-autocomplete>
120-
```
121-
122-
*my-comp.ts*
123-
```ts
124-
class MyComp {
125-
myControl = new FormControl();
126-
options = [
127-
new User('Mary'),
128-
new User('Shelley'),
129-
new User('Igor')
130-
];
131-
filteredOptions: Observable<User[]>;
132-
133-
ngOnInit() {
134-
this.filteredOptions = this.myControl.valueChanges
135-
.startWith(null)
136-
.map(user => user && typeof user === 'object' ? user.name : user)
137-
.map(name => name ? this.filter(name) : this.options.slice());
138-
}
139-
140-
filter(name: string): User[] {
141-
return this.options.filter(option => new RegExp(`^${name}`, 'gi').test(option.name));
142-
}
143-
144-
displayFn(user: User): string {
145-
return user ? user.name : user;
146-
}
147-
}
148-
```
78+
<!-- example(autocomplete-display) -->
14979

15080

15181
#### Keyboard interaction:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.example-form {
2+
width: 500px;
3+
}
4+
5+
.example-full-width {
6+
width: 100%;
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<form class="example-form">
2+
<md-input-container class="example-full-width">
3+
<input type="text" placeholder="Assignee" aria-label="Assignee" mdInput [formControl]="myControl" [mdAutocomplete]="auto">
4+
</md-input-container>
5+
6+
<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayFn">
7+
<md-option *ngFor="let option of filteredOptions | async" [value]="option">
8+
{{ option.name }}
9+
</md-option>
10+
</md-autocomplete>
11+
</form>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {Component} from '@angular/core';
2+
import {FormControl} from '@angular/forms';
3+
import {Observable} from 'rxjs/Observable';
4+
import 'rxjs/add/operator/startWith';
5+
import 'rxjs/add/operator/map';
6+
7+
export class User {
8+
constructor(public name: string) { }
9+
}
10+
11+
/**
12+
* @title Display value autocomplete
13+
*/
14+
@Component({
15+
selector: 'autocomplete-display-example',
16+
templateUrl: 'autocomplete-display-example.html',
17+
styleUrls: ['autocomplete-display-example.css']
18+
})
19+
export class AutocompleteDisplayExample {
20+
21+
myControl = new FormControl();
22+
23+
options = [
24+
new User('Mary'),
25+
new User('Shelley'),
26+
new User('Igor')
27+
];
28+
29+
filteredOptions: Observable<User[]>;
30+
31+
ngOnInit() {
32+
this.filteredOptions = this.myControl.valueChanges
33+
.startWith(null)
34+
.map(user => user && typeof user === 'object' ? user.name : user)
35+
.map(name => name ? this.filter(name) : this.options.slice());
36+
}
37+
38+
filter(name: string): User[] {
39+
return this.options.filter(option =>
40+
option.name.toLowerCase().indexOf(name.toLowerCase()) === 0);
41+
}
42+
43+
displayFn(user: User): string {
44+
return user ? user.name : user;
45+
}
46+
47+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.example-form {
2+
width: 500px;
3+
}
4+
5+
.example-full-width {
6+
width: 100%;
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<form class="example-form">
2+
<md-input-container class="example-full-width">
3+
<input type="text" placeholder="Pick one" aria-label="Number" mdInput [formControl]="myControl" [mdAutocomplete]="auto">
4+
</md-input-container>
5+
6+
<md-autocomplete #auto="mdAutocomplete">
7+
<md-option *ngFor="let option of filteredOptions | async" [value]="option">
8+
{{ option }}
9+
</md-option>
10+
</md-autocomplete>
11+
</form>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {Component} from '@angular/core';
2+
import {FormControl} from '@angular/forms';
3+
import {Observable} from 'rxjs/Observable';
4+
import 'rxjs/add/operator/startWith';
5+
import 'rxjs/add/operator/map';
6+
7+
/**
8+
* @title Filter autocomplete
9+
*/
10+
@Component({
11+
selector: 'autocomplete-filter-example',
12+
templateUrl: 'autocomplete-filter-example.html',
13+
styleUrls: ['autocomplete-filter-example.css']
14+
})
15+
export class AutocompleteFilterExample {
16+
17+
myControl: FormControl = new FormControl();
18+
19+
options = [
20+
'One',
21+
'Two',
22+
'Three'
23+
];
24+
25+
filteredOptions: Observable<string[]>;
26+
27+
ngOnInit() {
28+
this.filteredOptions = this.myControl.valueChanges
29+
.startWith(null)
30+
.map(val => val ? this.filter(val) : this.options.slice());
31+
}
32+
33+
filter(val: string): string[] {
34+
return this.options.filter(option =>
35+
option.toLowerCase().indexOf(val.toLowerCase()) === 0);
36+
}
37+
38+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.example-form {
2+
width: 500px;
3+
}
4+
5+
.example-full-width {
6+
width: 100%;
7+
}
Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
<md-input-container>
2-
<input mdInput placeholder="State" [mdAutocomplete]="auto" [formControl]="stateCtrl">
3-
</md-input-container>
1+
<form class="example-form">
2+
<md-input-container class="example-full-width">
3+
<input mdInput placeholder="State" aria-label="State" [mdAutocomplete]="auto" [formControl]="stateCtrl">
4+
</md-input-container>
45

5-
<md-autocomplete #auto="mdAutocomplete">
6-
<md-option *ngFor="let state of filteredStates | async" [value]="state">
7-
{{ state }}
8-
</md-option>
9-
</md-autocomplete>
6+
<md-autocomplete #auto="mdAutocomplete">
7+
<md-option *ngFor="let state of filteredStates | async" [value]="state.name">
8+
<img style="vertical-align:middle;" aria-hidden src="{{state.flag}}" height="25" />
9+
<span>{{ state.name }}</span> |
10+
<small>Population: {{state.population}}</small>
11+
</md-option>
12+
</md-autocomplete>
13+
14+
<br />
15+
16+
<md-slide-toggle
17+
[checked]="stateCtrl.disabled"
18+
(change)="stateCtrl.disabled ? stateCtrl.enable() : stateCtrl.disable()">
19+
Disable Input?
20+
</md-slide-toggle>
21+
</form>
Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,59 @@
11
import {Component} from '@angular/core';
22
import {FormControl} from '@angular/forms';
33

4+
import {Observable} from 'rxjs/Observable';
45
import 'rxjs/add/operator/startWith';
56
import 'rxjs/add/operator/map';
67

78
/**
8-
* @title Basic autocomplete
9+
* @title Autocomplete overview
910
*/
1011
@Component({
1112
selector: 'autocomplete-overview-example',
1213
templateUrl: 'autocomplete-overview-example.html',
14+
styleUrls: ['autocomplete-overview-example.css']
1315
})
1416
export class AutocompleteOverviewExample {
1517
stateCtrl: FormControl;
16-
filteredStates: any;
18+
filteredStates: Observable<any[]>;
1719

18-
states = [
19-
'Alabama',
20-
'Alaska',
21-
'Arizona',
22-
'Arkansas',
23-
'California',
24-
'Colorado',
25-
'Connecticut',
26-
'Delaware',
27-
'Florida',
28-
'Georgia',
29-
'Hawaii',
30-
'Idaho',
31-
'Illinois',
32-
'Indiana',
33-
'Iowa',
34-
'Kansas',
35-
'Kentucky',
36-
'Louisiana',
37-
'Maine',
38-
'Maryland',
39-
'Massachusetts',
40-
'Michigan',
41-
'Minnesota',
42-
'Mississippi',
43-
'Missouri',
44-
'Montana',
45-
'Nebraska',
46-
'Nevada',
47-
'New Hampshire',
48-
'New Jersey',
49-
'New Mexico',
50-
'New York',
51-
'North Carolina',
52-
'North Dakota',
53-
'Ohio',
54-
'Oklahoma',
55-
'Oregon',
56-
'Pennsylvania',
57-
'Rhode Island',
58-
'South Carolina',
59-
'South Dakota',
60-
'Tennessee',
61-
'Texas',
62-
'Utah',
63-
'Vermont',
64-
'Virginia',
65-
'Washington',
66-
'West Virginia',
67-
'Wisconsin',
68-
'Wyoming',
20+
states: any[] = [
21+
{
22+
name: 'Arkansas',
23+
population: '2.978M',
24+
// https://commons.wikimedia.org/wiki/File:Flag_of_Arkansas.svg
25+
flag: 'https://upload.wikimedia.org/wikipedia/commons/9/9d/Flag_of_Arkansas.svg'
26+
},
27+
{
28+
name: 'California',
29+
population: '39.14M',
30+
// https://commons.wikimedia.org/wiki/File:Flag_of_California.svg
31+
flag: 'https://upload.wikimedia.org/wikipedia/commons/0/01/Flag_of_California.svg'
32+
},
33+
{
34+
name: 'Florida',
35+
population: '20.27M',
36+
// https://commons.wikimedia.org/wiki/File:Flag_of_Florida.svg
37+
flag: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Flag_of_Florida.svg'
38+
},
39+
{
40+
name: 'Texas',
41+
population: '27.47M',
42+
// https://commons.wikimedia.org/wiki/File:Flag_of_Texas.svg
43+
flag: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Flag_of_Texas.svg'
44+
}
6945
];
7046

7147
constructor() {
7248
this.stateCtrl = new FormControl();
7349
this.filteredStates = this.stateCtrl.valueChanges
7450
.startWith(null)
75-
.map(name => this.filterStates(name));
51+
.map(state => state ? this.filterStates(state) : this.states.slice());
7652
}
7753

78-
filterStates(val: string) {
79-
return val ? this.states.filter(s => s.toLowerCase().indexOf(val.toLowerCase()) === 0)
80-
: this.states;
54+
filterStates(name: string) {
55+
return this.states.filter(state =>
56+
state.name.toLowerCase().indexOf(name.toLowerCase()) === 0);
8157
}
8258

8359
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.example-form {
2+
width: 500px;
3+
}
4+
5+
.example-full-width {
6+
width: 100%;
7+
}

0 commit comments

Comments
 (0)