import {Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild} from "@angular/core"; import {AbstractControl, FormArray, FormControl} from "@angular/forms"; import {HelperFunctions} from "../../utils/HelperFunctions.class"; import {Observable, of, Subscription} from "rxjs"; import {MatSelect} from "@angular/material/select"; import {MatAutocompleteSelectedEvent} from "@angular/material/autocomplete"; import {map, startWith} from "rxjs/operators"; export interface Option { icon?: string, iconClass?: string, value: any, label: string } @Component({ selector: '[dashboard-input]', template: `
{{label + (required ? ' *' : '')}}
{{hint}}
{{placeholder}} {{option.label}}
{{chip.value[chipLabel]}}
{{placeholder}}
{{option.label}}
{{formControl.errors.error}} {{warning}} {{note}}
{{label}} `, styleUrls: ['input.component.css'] }) export class InputComponent implements OnInit, OnDestroy, OnChanges { @Input('formInput') formControl: AbstractControl; @Input('type') type: 'text' | 'textarea' | 'select' | 'checkbox' | 'chips' = 'text'; @Input('label') label: string; @Input('rows') rows: number = 3; @Input('options') options: Option[]; @Input('hint') hint = null; @Input('placeholder') placeholder = ''; @ViewChild('select') select: MatSelect; @Input() extraLeft: boolean = true; @Input() gridSmall: boolean = false; @Input() hideControl: boolean = false; @Input() icon: string = null; @Input() iconLeft: boolean = false; @Input() warning: string = null; @Input() note: string = null; @Input() removable: boolean = true; @Input() chipLabel: string = null; public filteredOptions: Observable; public searchControl: FormControl; public required: boolean = false; private initValue: any; private subscriptions: any[] = []; @ViewChild('searchInput') searchInput; constructor() { } ngOnInit(): void { this.reset(); } ngOnChanges(changes: SimpleChanges) { if(changes.formControl) { this.reset(); } } get formAsArray(): FormArray { return ( this.formControl); } reset() { this.unsubscribe(); this.initValue = HelperFunctions.copy(this.formControl.value); if(this.options && this.type === 'chips') { this.filteredOptions = of(this.options); this.searchControl = new FormControl(''); this.filteredOptions = this.searchControl.valueChanges.pipe(startWith(''), map(option => this.filter(option))); } if (this.formControl && this.formControl.validator) { let validator = this.formControl.validator({} as AbstractControl); this.required = (validator && validator.required); } this.subscriptions.push(this.formControl.valueChanges.subscribe(value => { value = (value === '')?null:value; if(this.initValue === value || (this.initValue === '' && value === null)) { this.formControl.markAsPristine(); } })); if (!this.formControl.value) { this.formControl.setValue(''); } } unsubscribe() { this.subscriptions.forEach(subscription => { if(subscription instanceof Subscription) { subscription.unsubscribe(); } }); } openSelect() { if(this.select) { this.select.open(); } } ngOnDestroy(): void { this.unsubscribe(); } stopPropagation() { event.stopPropagation(); } removed(index: number) { this.formAsArray.removeAt(index); this.formAsArray.markAsDirty(); } selected(event: MatAutocompleteSelectedEvent): void { this.formAsArray.push(new FormControl(event.option.value)); this.formAsArray.markAsDirty(); this.searchControl.setValue(''); this.searchInput.nativeElement.value = ''; } private filter(value: string): Option[] { let options = this.options.filter(option => !this.formAsArray.value.find(value => option.label === value[this.chipLabel])); if (!value || value.length == 0) { return options; } const filterValue = value.toString().toLowerCase(); return options.filter(option => option.label.toLowerCase().indexOf(filterValue) != -1); } }