-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsingle-selection-example.component.ts
95 lines (76 loc) · 2.7 KB
/
single-selection-example.component.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { FormControl } from '@angular/forms';
import { MatSelect } from '@angular/material';
import { ReplaySubject, Subject } from 'rxjs';
import { take, takeUntil } from 'rxjs/operators';
import { Bank, BANKS } from '../demo-data';
@Component({
selector: 'app-single-selection-example',
templateUrl: './single-selection-example.component.html',
styleUrls: ['./single-selection-example.component.scss']
})
export class SingleSelectionExampleComponent implements OnInit, AfterViewInit, OnDestroy {
/** list of banks */
protected banks: Bank[] = BANKS;
/** control for the selected bank */
public bankCtrl: FormControl = new FormControl();
/** control for the MatSelect filter keyword */
public bankFilterCtrl: FormControl = new FormControl();
/** list of banks filtered by search keyword */
public filteredBanks: ReplaySubject<Bank[]> = new ReplaySubject<Bank[]>(1);
@ViewChild('singleSelect') singleSelect: MatSelect;
/** Subject that emits when the component has been destroyed. */
protected _onDestroy = new Subject<void>();
constructor() { }
ngOnInit() {
// set initial selection
this.bankCtrl.setValue(this.banks[10]);
// load the initial bank list
this.filteredBanks.next(this.banks.slice());
// listen for search field value changes
this.bankFilterCtrl.valueChanges
.pipe(takeUntil(this._onDestroy))
.subscribe(() => {
this.filterBanks();
});
}
ngAfterViewInit() {
this.setInitialValue();
}
ngOnDestroy() {
this._onDestroy.next();
this._onDestroy.complete();
}
/**
* Sets the initial value after the filteredBanks are loaded initially
*/
protected setInitialValue() {
this.filteredBanks
.pipe(take(1), takeUntil(this._onDestroy))
.subscribe(() => {
// setting the compareWith property to a comparison function
// triggers initializing the selection according to the initial value of
// the form control (i.e. _initializeSelection())
// this needs to be done after the filteredBanks are loaded initially
// and after the mat-option elements are available
this.singleSelect.compareWith = (a: Bank, b: Bank) => a && b && a.id === b.id;
});
}
protected filterBanks() {
if (!this.banks) {
return;
}
// get the search keyword
let search = this.bankFilterCtrl.value;
if (!search) {
this.filteredBanks.next(this.banks.slice());
return;
} else {
search = search.toLowerCase();
}
// filter the banks
this.filteredBanks.next(
this.banks.filter(bank => bank.name.toLowerCase().indexOf(search) > -1)
);
}
}