1
|
import {Component, ElementRef, EventEmitter, Input, Output, ViewChild} from '@angular/core';
|
2
|
import {AbstractControl} from '@angular/forms';
|
3
|
import {MatAutocompleteTrigger} from '@angular/material/autocomplete';
|
4
|
|
5
|
@Component({
|
6
|
selector: '[search-input]',
|
7
|
styleUrls: ['search-input.component.css'],
|
8
|
template: `
|
9
|
<div class="uk-flex uk-flex-middle uk-flex-center search-mobile" [ngClass]="colorClass">
|
10
|
<div *ngIf="control" class="uk-width-expand" [class.bordered]="bordered">
|
11
|
<form (ngSubmit)="search()">
|
12
|
<input #input type="text" class="uk-width-1-1"
|
13
|
[class.uk-animation-slide-right-medium]="showSearch"
|
14
|
[class.uk-hidden@m]="!showSearch"
|
15
|
[class.uk-hidden]="selected"
|
16
|
[placeholder]="placeholder"
|
17
|
(blur)="closeSearch()"
|
18
|
[formControl]="control"
|
19
|
[matAutocomplete]="auto">
|
20
|
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="search()">
|
21
|
<mat-option *ngFor="let option of list | async" [value]="option">
|
22
|
{{option}}
|
23
|
</mat-option>
|
24
|
</mat-autocomplete>
|
25
|
</form>
|
26
|
<div *ngIf="selected" class="uk-flex uk-flex-left selected">
|
27
|
<span class="uk-flex uk-flex-middle">
|
28
|
<span>{{selected}}</span>
|
29
|
<span class="uk-icon clickable space" uk-icon="icon: close; ratio: 0.8" (click)="resetEmitter.emit()">
|
30
|
<svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" data-svg="close"><path
|
31
|
fill="none" stroke="#000" stroke-width="1.06" d="M16,16 L4,4"></path>
|
32
|
<path fill="none" stroke="#000" stroke-width="1.06" d="M16,4 L4,16"></path>
|
33
|
</svg>
|
34
|
</span>
|
35
|
</span>
|
36
|
</div>
|
37
|
</div>
|
38
|
<button [disabled]="loading" class="search uk-flex uk-flex-middle uk-margin-medium-left uk-visible@m"
|
39
|
(mousedown)="$event.preventDefault()" (click)="toggle()">
|
40
|
<span [ngClass]="colorClass" class="icon-bg">
|
41
|
<icon class="uk-position-center" name="search"></icon>
|
42
|
</span>
|
43
|
<span class="uk-text-uppercase overlay">search</span>
|
44
|
</button>
|
45
|
<button [disabled]="loading" class="search uk-flex uk-flex-middle uk-hidden@m"
|
46
|
(mousedown)="$event.preventDefault()" (click)="search()">
|
47
|
<span [ngClass]="colorClass" class="icon-bg">
|
48
|
<icon class="uk-position-center" name="search"></icon>
|
49
|
</span>
|
50
|
<span class="uk-text-uppercase overlay">search</span>
|
51
|
</button>
|
52
|
</div>`
|
53
|
})
|
54
|
export class SearchInputComponent {
|
55
|
@Input()
|
56
|
showSearch: boolean = true;
|
57
|
@Input()
|
58
|
control: AbstractControl;
|
59
|
@Input()
|
60
|
placeholder: string;
|
61
|
@Input()
|
62
|
loading: boolean = false;
|
63
|
@Input()
|
64
|
selected: any;
|
65
|
@Input()
|
66
|
list: any = null;
|
67
|
@Input()
|
68
|
colorClass: string = 'portal-color';
|
69
|
@Input()
|
70
|
bordered: boolean = false;
|
71
|
@ViewChild('input') input: ElementRef;
|
72
|
@ViewChild(MatAutocompleteTrigger) trigger: MatAutocompleteTrigger;
|
73
|
@Output()
|
74
|
searchEmitter: EventEmitter<boolean> = new EventEmitter<boolean>();
|
75
|
@Output()
|
76
|
resetEmitter: EventEmitter<any> = new EventEmitter<any>();
|
77
|
|
78
|
toggle() {
|
79
|
if(!this.selected) {
|
80
|
this.showSearch = !this.showSearch;
|
81
|
if (this.showSearch) {
|
82
|
setTimeout(() => { // this will make the execution after the above boolean has changed
|
83
|
this.input.nativeElement.focus();
|
84
|
}, 0);
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
|
89
|
closeSearch() {
|
90
|
this.showSearch = false;
|
91
|
}
|
92
|
|
93
|
public search(emit = true) {
|
94
|
this.trigger.closePanel();
|
95
|
this.searchEmitter.emit(emit);
|
96
|
}
|
97
|
|
98
|
public reset() {
|
99
|
this.showSearch = true;
|
100
|
setTimeout(() => {
|
101
|
this.input.nativeElement.focus();
|
102
|
}, 0);
|
103
|
}
|
104
|
}
|