import { Maybe } from '@/core'; import { Component, EventEmitter, Input, model, Output, signal } from '@angular/core'; import { FormControl } from '@angular/forms'; export interface ISelectItem { id: number; name: string; } @Component({ selector: 'abstract-select-field', template: '', }) export abstract class AbstractSelectComponent { @Input() control = new FormControl>(null); @Input() canInsert: boolean = false; @Input() showLabel: boolean = true; @Input() showErrors: boolean = true; @Input() isFullDataOptionValue: boolean = false; @Input() optionValue = 'id'; @Output() onChange = new EventEmitter>(); loading = signal(false); items = signal>(null); isOpenFormDialog = signal(false); value = model>(null); constructor() { if (this.value()) { this.control.setValue(this.value()); } this.control.valueChanges.subscribe((val) => { this.value.set(val as Maybe); }); } abstract getData(): void; refresh() { this.getData(); } onOpenForm() { this.isOpenFormDialog.set(true); } get selectOptionValue() { if (this.isFullDataOptionValue) { return undefined; } return this.optionValue; } change(value: Maybe) { if (typeof value !== 'object') { // @ts-ignore this.onChange.emit(this.items()?.find((item) => item[this.optionValue] === value)); } else { this.onChange.emit(value); } } }