Skip to content

Commit 1cc0c7f

Browse files
raygervaismmalerba
authored andcommitted
docs(autocomplete): Adds option group example (#10666)
* docs(autocomplete): Adds option group example Adds a option group example which displays ideal logic and best practices for the material autocomplete component. * docs(autocomplete): Fixes option group example Changes code style and logic to expected logic and styleguide
1 parent 74755f0 commit 1cc0c7f

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

src/lib/autocomplete/autocomplete.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ injection token.
9898

9999
### Option groups
100100
`mat-option` can be collected into groups using the `mat-optgroup` element:
101+
<!-- example(autocomplete-optgroup) -->
102+
101103

102104
```html
103105
<mat-autocomplete #auto="matAutocomplete">
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/** No CSS for this example */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<form [formGroup]="stateForm">
2+
<mat-form-field>
3+
<input type="text" matInput placeholder="States Group" formControlName="stateGroup" required [matAutocomplete]="autoGroup"/>
4+
<mat-autocomplete #autoGroup="matAutocomplete">
5+
<mat-optgroup *ngFor="let group of stateGroupOptions | async" [label]="group.letter">
6+
<mat-option *ngFor="let name of group.names" [value]="name">
7+
{{ name }}
8+
</mat-option>
9+
</mat-optgroup>
10+
</mat-autocomplete>
11+
</mat-form-field>
12+
</form>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import {Component, OnInit} from '@angular/core';
2+
import {FormGroup, FormBuilder} from '@angular/forms';
3+
import {Observable} from 'rxjs';
4+
import {startWith, map} from 'rxjs/operators';
5+
6+
export interface StateGroup {
7+
letter: string;
8+
names: string[];
9+
}
10+
11+
/**
12+
* @title Option groups autocomplete
13+
*/
14+
@Component({
15+
templateUrl: './autocomplete-optgroup-example.html',
16+
styleUrls: ['./autocomplete-optgroup-example.css'],
17+
})
18+
19+
export class AutocompleteOptionGroupExample implements OnInit {
20+
stateForm: FormGroup = this.fb.group({
21+
stateGroup: '',
22+
});
23+
24+
stateGroups: StateGroup[] = [{
25+
letter: 'A',
26+
names: ['Alabama', 'Alaska', 'Arizona', 'Arkansas']
27+
}, {
28+
letter: 'C',
29+
names: ['California', 'Colorado', 'Connecticut']
30+
}, {
31+
letter: 'D',
32+
names: ['Delaware']
33+
}, {
34+
letter: 'F',
35+
names: ['Florida']
36+
}, {
37+
letter: 'G',
38+
names: ['Georgia']
39+
}, {
40+
letter: 'H',
41+
names: ['Hawaii']
42+
}, {
43+
letter: 'I',
44+
names: ['Idaho', 'Illinois', 'Indiana', 'Iowa']
45+
}, {
46+
letter: 'K',
47+
names: ['Kansas', 'Kentucky']
48+
}, {
49+
letter: 'L',
50+
names: ['Louisiana']
51+
}, {
52+
letter: 'M',
53+
names: ['Maine', 'Maryland', 'Massachusetts', 'Michigan',
54+
'Minnesota', 'Mississippi', 'Missouri', 'Montana']
55+
}, {
56+
letter: 'N',
57+
names: ['Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
58+
'New Mexico', 'New York', 'North Carolina', 'North Dakota']
59+
}, {
60+
letter: 'O',
61+
names: ['Ohio', 'Oklahoma', 'Oregon']
62+
}, {
63+
letter: 'P',
64+
names: ['Pennsylvania']
65+
}, {
66+
letter: 'R',
67+
names: ['Rhode Island']
68+
}, {
69+
letter: 'S',
70+
names: ['South Carolina', 'South Dakota']
71+
}, {
72+
letter: 'T',
73+
names: ['Tennessee', 'Texas']
74+
}, {
75+
letter: 'U',
76+
names: ['Utah']
77+
}, {
78+
letter: 'V',
79+
names: ['Vermont', 'Virginia']
80+
}, {
81+
letter: 'W',
82+
names: ['Washington', 'West Virginia', 'Wisconsin', 'Wyoming']
83+
}];
84+
85+
stateGroupOptions: Observable<StateGroup[]>;
86+
87+
constructor(private fb: FormBuilder) { }
88+
89+
ngOnInit() {
90+
this.stateGroupOptions = this.stateForm.get('stateGroup')!.valueChanges
91+
.pipe(
92+
startWith(''),
93+
map(val => this.filterGroup(val))
94+
);
95+
}
96+
97+
filterGroup(val: string): StateGroup[] {
98+
if (val) {
99+
return this.stateGroups
100+
.map(group => ({ letter: group.letter, names: this._filter(group.names, val) }))
101+
.filter(group => group.names.length > 0);
102+
}
103+
104+
return this.stateGroups;
105+
}
106+
107+
private _filter(opt: string[], val: string) {
108+
const filterValue = val.toLowerCase();
109+
return opt.filter(item => item.toLowerCase().startsWith(filterValue));
110+
}
111+
}

0 commit comments

Comments
 (0)