-
Notifications
You must be signed in to change notification settings - Fork 29
/
search-bar.component.ts
58 lines (50 loc) · 1.35 KB
/
search-bar.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
import {
Component,
Output,
EventEmitter,
Input,
ViewChild,
ElementRef,
OnChanges,
SimpleChange,
} from "@angular/core";
import { Subject } from "rxjs";
import { distinctUntilChanged, debounceTime } from "rxjs/operators";
@Component({
selector: "app-search-bar",
templateUrl: "./search-bar.component.html",
styleUrls: ["./search-bar.component.scss"],
})
export class SearchBarComponent implements OnChanges {
private searchDebounce = 300;
searchSubject = new Subject<string>();
@ViewChild("searchBar", { static: true }) searchBar!: ElementRef;
@Input() prefilledValue: string | null = "";
@Input() placeholder = "";
@Input() autocompleteOptions = [];
@Input() clear = false;
@Output() search = this.searchSubject.pipe(
distinctUntilChanged(),
debounceTime(this.searchDebounce),
);
@Output() searchBarFocus = new EventEmitter<string>();
doSearch() {
this.searchSubject.next(this.query);
}
doFocus() {
this.searchBarFocus.emit(this.query);
}
get query() {
return this.searchBar.nativeElement.value;
}
set query(value: string) {
this.searchBar.nativeElement.value = value;
}
ngOnChanges(changes: { [propKey: string]: SimpleChange }) {
for (const propName in changes) {
if (propName === "clear" && changes[propName].currentValue === true) {
this.query = "";
}
}
}
}