Skip to content

Commit f0dd2ec

Browse files
author
Ales Rechtorik
committed
add async example
1 parent 4ec73c7 commit f0dd2ec

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

src/demo-app/select/select-demo.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@
5757
</md-select>
5858
</md-card>
5959

60+
<md-card *ngIf="selectHeaderAsyncControl">
61+
<h3>md-select-header with async search</h3>
62+
<md-select placeholder="Users" [formControl]="selectHeaderAsyncControl.get('selected')">
63+
<md-select-header #selectAsyncHeader>
64+
<input
65+
type="search"
66+
[formControl]="selectHeaderAsyncControl.get('search')"
67+
[attr.aria-owns]="selectAsyncHeader.panelId"
68+
placeholder="Search for a github User"
69+
/>
70+
</md-select-header>
71+
72+
<md-option *ngFor="let user of users$ | async" [value]="user">
73+
<img
74+
class="async-select-avatar mat-elevation-z2"
75+
[src]="user.avatar_url"
76+
[alt]="user.login"
77+
/>
78+
{{ user.login }}
79+
</md-option>
80+
81+
</md-select>
82+
83+
<div *ngIf="selectHeaderAsyncControl.get('selected').value">
84+
<h5>selected user:</h5>
85+
<pre><code>{{ selectHeaderAsyncControl.get('selected').value | json }}</code></pre>
86+
</div>
87+
88+
</md-card>
89+
6090

6191
</div>
6292
<div style="height: 500px">This div is for testing scrolled selects.</div>

src/demo-app/select/select-demo.scss

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,21 @@
77
margin: 24px;
88
}
99

10-
}
10+
}
11+
12+
.async-select-avatar {
13+
display: inline-block;
14+
vertical-align: middle;
15+
width: 30px;
16+
height: 30px;
17+
margin-right: 10px;
18+
border-radius: 100%;
19+
}
20+
21+
pre {
22+
white-space: pre-wrap;
23+
font-family: Consolas, "Andale Mono WT", "Andale Mono", "Lucida Console",
24+
"Lucida Sans Typewriter", "DejaVu Sans Mono", "Bitstream Vera Sans Mono",
25+
"Liberation Mono", "Nimbus Mono L", Monaco, "Courier New", Courier, monospace;
26+
27+
}

src/demo-app/select/select-demo.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import {Component} from '@angular/core';
2-
import {FormControl} from '@angular/forms';
2+
import {FormControl, FormGroup} from '@angular/forms';
33
import {MdSelectChange} from '@angular/material';
4+
import {Http} from '@angular/http';
5+
6+
import 'rxjs/add/operator/debounce';
7+
import 'rxjs/add/operator/withLatestFrom';
8+
import 'rxjs/add/observable/timer';
9+
import {Observable} from 'rxjs/Observable';
10+
11+
const GITHUB_API_ENDPOINT = 'https://api.github.com';
412

513
@Component({
614
moduleId: module.id,
@@ -17,6 +25,29 @@ export class SelectDemo {
1725
latestChangeEvent: MdSelectChange;
1826
foodControl = new FormControl('pizza-1');
1927

28+
selectHeaderAsyncControl = new FormGroup({
29+
search: new FormControl(),
30+
selected: new FormControl({})
31+
});
32+
33+
users$ = this.selectHeaderAsyncControl
34+
.get('search')
35+
.valueChanges
36+
.startWith(null)
37+
.debounce(() => Observable.timer(200))
38+
.switchMap((term) => {
39+
if (term) {
40+
return this._http
41+
.get(`${GITHUB_API_ENDPOINT}/search/users?q=${term}`)
42+
.map((response) => response.json())
43+
.map((data) => data.items);
44+
} else {
45+
return this._http
46+
.get(`${GITHUB_API_ENDPOINT}/users`)
47+
.map((response) => response.json());
48+
}
49+
});
50+
2051
foods = [
2152
{value: 'steak-0', viewValue: 'Steak'},
2253
{value: 'pizza-1', viewValue: 'Pizza'},
@@ -43,6 +74,8 @@ export class SelectDemo {
4374
{value: 'squirtle-2', viewValue: 'Squirtle'}
4475
];
4576

77+
constructor(private _http: Http) { }
78+
4679
toggleDisabled() {
4780
this.foodControl.enabled ? this.foodControl.disable() : this.foodControl.enable();
4881
}

0 commit comments

Comments
 (0)