import { Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core'; import { FormControl } from '@angular/forms'; import { Observable } from 'rxjs'; import { Filter, OptionSelect } from '../../interface'; @Component({ selector: 'kit-toolbar-filter-select-state', templateUrl: './kit-toolbar-filter-select-state.component.html', }) export class KitToolbarFilterSelectStateComponent implements OnInit, OnChanges { @Input() filterForm: FormControl = new FormControl({}); public filteredOptions$: Observable | undefined; private _filter!:Filter; public options: OptionSelect[] = []; @Input() public set filter(filter:Filter){ this._filter = filter; } public constructor(){} public get filter(){ return this._filter; } onChange(event: any): void { console.log('Selection changed:', event); } public setFilterObservable(): void { this.options = this.filter.values || []; if (this.filter.value && Array.isArray(this.filter.values)) { const matchingOption = this.filter.values.find(option => option.value === (this.filter.value as OptionSelect)?.value); if (matchingOption) { this.filterForm.setValue(matchingOption.value); } else { this.filterForm.setValue(this.filter.value); } } } public ngOnInit(): void { this.setFilterObservable(); } public isControlRequired(): boolean { if (this.filterForm && this.filterForm.validator) { const validator = this.filterForm.validator({} as any); return !!(validator && validator['required']); } return false; } defaultCompareWithFn = (o1: any, o2: any) => o1 === o2; compareWithFn(o1: any, o2: any): boolean { return o1 && o2 ? o1.value === o2.value : o1 === o2; } public ngOnChanges(changes: SimpleChanges): void { if(changes['options']) { this.setFilterObservable() } } }