-
Notifications
You must be signed in to change notification settings - Fork 556
/
autocomplete-type.component.ts
44 lines (40 loc) 路 1.39 KB
/
autocomplete-type.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
import { Component, ViewChild, OnInit, AfterViewInit } from '@angular/core';
import { FieldType } from '@ngx-formly/material';
import { MatInput } from '@angular/material/input';
import { MatAutocompleteTrigger } from '@angular/material/autocomplete';
import { Observable } from 'rxjs';
import { startWith, switchMap } from 'rxjs/operators';
@Component({
selector: 'formly-autocomplete-type',
template: `
<input matInput
[matAutocomplete]="auto"
[formControl]="formControl"
[formlyAttributes]="field"
[placeholder]="to.placeholder"
[errorStateMatcher]="errorStateMatcher">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let value of filter | async" [value]="value">
{{ value }}
</mat-option>
</mat-autocomplete>
`,
})
export class AutocompleteTypeComponent extends FieldType implements OnInit, AfterViewInit {
@ViewChild(MatInput) formFieldControl: MatInput;
@ViewChild(MatAutocompleteTrigger) autocomplete: MatAutocompleteTrigger;
filter: Observable<any>;
ngOnInit() {
super.ngOnInit();
this.filter = this.formControl.valueChanges
.pipe(
startWith(''),
switchMap(term => this.to.filter(term)),
);
}
ngAfterViewInit() {
super.ngAfterViewInit();
// temporary fix for https://github.com/angular/material2/issues/6728
(<any> this.autocomplete)._formField = this.formField;
}
}