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(); toggle() { if(!this.selected) { 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; } public search(emit = true) { this.trigger.closePanel(); this.searchEmitter.emit(emit); } public reset() { this.showSearch = true; setTimeout(() => { this.input.nativeElement.focus(); }, 0); } }