import {Component, ElementRef, EventEmitter, Input, Output, ViewChild} from '@angular/core'; import {AbstractControl} from '@angular/forms'; import {MatAutocompleteTrigger} from '@angular/material/autocomplete'; @Component({ selector: '[search-input]', styleUrls: ['search-input.component.css'], template: `
{{option}}
{{selected}}
` }) export class SearchInputComponent { @Input() showSearch: boolean = true; @Input() control: AbstractControl; @Input() placeholder: string; @Input() loading: boolean = false; @Input() selected: any; @Input() list: any = null; @Input() colorClass: string = 'portal-color'; @Input() bordered: boolean = false; @Input() toggleTitle: string = 'search'; @ViewChild('input') input: ElementRef; @ViewChild(MatAutocompleteTrigger) trigger: MatAutocompleteTrigger; @Output() searchEmitter: EventEmitter = new EventEmitter(); @Output() resetEmitter: EventEmitter = new EventEmitter(); @Output() closeEmitter: EventEmitter = new EventEmitter(); toggle() { this.showSearch = !this.showSearch; if (this.showSearch) { setTimeout(() => { // this will make the execution after the above boolean has changed this.input.nativeElement.focus(); }, 0); } } closeSearch() { this.showSearch = false; this.closeEmitter.emit(); } public search(emit = true) { this.trigger.closePanel(); this.searchEmitter.emit(emit); } public reset() { this.control.setValue(''); this.input.nativeElement.value = ''; this.showSearch = true; setTimeout(() => { this.input.nativeElement.focus(); }, 0); } }