-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathautocomplete-custom-trigger-example.ts
More file actions
83 lines (77 loc) · 2.62 KB
/
autocomplete-custom-trigger-example.ts
File metadata and controls
83 lines (77 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {AsyncPipe} from '@angular/common';
import {Component} from '@angular/core';
import {FormControl, ReactiveFormsModule} from '@angular/forms';
import {MatAutocompleteModule} from '@angular/material/autocomplete';
import {MatFormFieldModule} from '@angular/material/form-field';
import {MatInputModule} from '@angular/material/input';
import {MatSlideToggleModule} from '@angular/material/slide-toggle';
import {map, startWith} from 'rxjs/operators';
export interface State {
flag: string;
name: string;
population: string;
}
/** @title Autocomplete with custom selected-value template */
@Component({
selector: 'autocomplete-custom-trigger-example',
templateUrl: 'autocomplete-custom-trigger-example.html',
styleUrl: 'autocomplete-custom-trigger-example.css',
imports: [
AsyncPipe,
MatAutocompleteModule,
MatFormFieldModule,
MatInputModule,
MatSlideToggleModule,
ReactiveFormsModule,
],
})
export class AutocompleteCustomTriggerExample {
stateCtrl = new FormControl<State | string | null>(null);
filteredStates = this.stateCtrl.valueChanges.pipe(
startWith(null),
map(value => {
const name = typeof value === 'string' ? value : (value?.name ?? '');
return name ? this._filterStates(name) : this.states.slice();
}),
);
states: State[] = [
{
name: 'Arkansas',
population: '2.978M',
// https://commons.wikimedia.org/wiki/File:Flag_of_Arkansas.svg
flag: 'https://upload.wikimedia.org/wikipedia/commons/9/9d/Flag_of_Arkansas.svg',
},
{
name: 'California',
population: '39.14M',
// https://commons.wikimedia.org/wiki/File:Flag_of_California.svg
flag: 'https://upload.wikimedia.org/wikipedia/commons/0/01/Flag_of_California.svg',
},
{
name: 'Florida',
population: '20.27M',
// https://commons.wikimedia.org/wiki/File:Flag_of_Florida.svg
flag: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Flag_of_Florida.svg',
},
{
name: 'Texas',
population: '27.47M',
// https://commons.wikimedia.org/wiki/File:Flag_of_Texas.svg
flag: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Flag_of_Texas.svg',
},
];
displayFn(state: State | null): string {
return state?.name ?? '';
}
private _filterStates(value: string): State[] {
const filterValue = value.toLowerCase();
return this.states.filter(state => state.name.toLowerCase().includes(filterValue));
}
}