Skip to content

Commit fd5c261

Browse files
committed
chore: add option group demo
1 parent b9700e8 commit fd5c261

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

src/demo-app/autocomplete/autocomplete-demo.html

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@
4040
</md-card-actions>
4141

4242
</md-card>
43+
44+
<md-card>
45+
<div>Option groups (currentGroupedState): {{ currentGroupedState }}</div>
46+
47+
<md-input-container>
48+
<input
49+
mdInput
50+
placeholder="State"
51+
[mdAutocomplete]="groupedAuto"
52+
[(ngModel)]="currentGroupedState"
53+
(ngModelChange)="filteredGroupedStates = filterStateGroups(currentGroupedState)">
54+
</md-input-container>
55+
</md-card>
4356
</div>
4457

4558
<md-autocomplete #reactiveAuto="mdAutocomplete" [displayWith]="displayFn">
@@ -53,4 +66,11 @@
5366
<md-option *ngFor="let state of tdStates" [value]="state.name">
5467
<span>{{ state.name }}</span>
5568
</md-option>
56-
</md-autocomplete>
69+
</md-autocomplete>
70+
71+
<md-autocomplete #groupedAuto="mdAutocomplete">
72+
<md-optgroup *ngFor="let group of filteredGroupedStates"
73+
[label]="'States starting with ' + group.letter">
74+
<md-option *ngFor="let state of group.states" [value]="state.name">{{ state.name }}</md-option>
75+
</md-optgroup>
76+
</md-autocomplete>

src/demo-app/autocomplete/autocomplete-demo.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
.mat-input-container {
1111
margin-top: 16px;
12+
min-width: 200px;
13+
max-width: 100%;
1214
}
1315
}
1416

src/demo-app/autocomplete/autocomplete-demo.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import {FormControl, NgModel} from '@angular/forms';
33
import 'rxjs/add/operator/startWith';
44
import 'rxjs/add/operator/map';
55

6+
interface State {
7+
code: string;
8+
name: string;
9+
}
10+
11+
interface StateGroup {
12+
letter: string;
13+
states: State[];
14+
}
15+
616
@Component({
717
moduleId: module.id,
818
selector: 'autocomplete-demo',
@@ -13,6 +23,7 @@ import 'rxjs/add/operator/map';
1323
export class AutocompleteDemo {
1424
stateCtrl: FormControl;
1525
currentState = '';
26+
currentGroupedState = '';
1627
topHeightCtrl = new FormControl(0);
1728

1829
reactiveStates: any;
@@ -22,7 +33,9 @@ export class AutocompleteDemo {
2233

2334
@ViewChild(NgModel) modelDir: NgModel;
2435

25-
states = [
36+
groupedStates: StateGroup[];
37+
filteredGroupedStates: StateGroup[];
38+
states: State[] = [
2639
{code: 'AL', name: 'Alabama'},
2740
{code: 'AK', name: 'Alaska'},
2841
{code: 'AZ', name: 'Arizona'},
@@ -82,18 +95,41 @@ export class AutocompleteDemo {
8295
.startWith(this.stateCtrl.value)
8396
.map(val => this.displayFn(val))
8497
.map(name => this.filterStates(name));
98+
99+
this.filteredGroupedStates = this.groupedStates = this.states.reduce((groups, state) => {
100+
let group = groups.find(g => g.letter === state.name[0]);
101+
102+
if (!group) {
103+
group = { letter: state.name[0], states: [] };
104+
groups.push(group);
105+
}
106+
107+
group.states.push({ code: state.code, name: state.name });
108+
109+
return groups;
110+
}, [] as StateGroup[]);
85111
}
86112

87113
displayFn(value: any): string {
88114
return value && typeof value === 'object' ? value.name : value;
89115
}
90116

91117
filterStates(val: string) {
118+
return val ? this._filter(this.states, val) : this.states;
119+
}
120+
121+
filterStateGroups(val: string) {
92122
if (val) {
93-
const filterValue = val.toLowerCase();
94-
return this.states.filter(state => state.name.toLowerCase().startsWith(filterValue));
123+
return this.groupedStates
124+
.map(group => ({ letter: group.letter, states: this._filter(group.states, val) }))
125+
.filter(group => group.states.length > 0);
95126
}
96127

97-
return this.states;
128+
return this.groupedStates;
129+
}
130+
131+
private _filter(states: State[], val: string) {
132+
const filterValue = val.toLowerCase();
133+
return states.filter(state => state.name.toLowerCase().startsWith(filterValue));
98134
}
99135
}

0 commit comments

Comments
 (0)