2025-12-10 16:54:25 +03:30
|
|
|
import { Maybe } from '@/core';
|
2025-12-23 20:35:20 +03:30
|
|
|
import { Component, EventEmitter, Input, model, Output, signal } from '@angular/core';
|
2025-12-10 16:54:25 +03:30
|
|
|
import { FormControl } from '@angular/forms';
|
2025-12-28 17:51:54 +03:30
|
|
|
import { Observable } from 'rxjs';
|
2025-12-10 16:54:25 +03:30
|
|
|
|
|
|
|
|
export interface ISelectItem {
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'abstract-select-field',
|
|
|
|
|
template: '',
|
|
|
|
|
})
|
2025-12-28 17:51:54 +03:30
|
|
|
export abstract class AbstractSelectComponent<T = ISelectItem, serviceResponse = T[]> {
|
2026-03-10 20:23:56 +03:30
|
|
|
@Input() control = new FormControl<Maybe<number | string | T>>(null);
|
2025-12-10 16:54:25 +03:30
|
|
|
@Input() canInsert: boolean = false;
|
|
|
|
|
@Input() showLabel: boolean = true;
|
|
|
|
|
@Input() showErrors: boolean = true;
|
2025-12-26 01:20:44 +03:30
|
|
|
@Input() showClear: boolean = true;
|
2025-12-10 16:54:25 +03:30
|
|
|
@Input() isFullDataOptionValue: boolean = false;
|
|
|
|
|
@Input() optionValue = 'id';
|
2025-12-23 20:35:20 +03:30
|
|
|
@Output() onChange = new EventEmitter<Maybe<T>>();
|
2025-12-28 17:51:54 +03:30
|
|
|
@Output() onSetDefaultDataItem = new EventEmitter<T>();
|
2025-12-10 16:54:25 +03:30
|
|
|
|
|
|
|
|
loading = signal(false);
|
|
|
|
|
items = signal<Maybe<T[]>>(null);
|
|
|
|
|
isOpenFormDialog = signal(false);
|
2025-12-15 18:00:45 +03:30
|
|
|
value = model<Maybe<T>>(null);
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
if (this.value()) {
|
|
|
|
|
this.control.setValue(this.value());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.control.valueChanges.subscribe((val) => {
|
|
|
|
|
this.value.set(val as Maybe<T>);
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-12-10 16:54:25 +03:30
|
|
|
|
2025-12-28 17:51:54 +03:30
|
|
|
abstract getDataService(): Observable<serviceResponse>;
|
2026-03-10 20:23:56 +03:30
|
|
|
|
2025-12-28 17:51:54 +03:30
|
|
|
getData() {
|
|
|
|
|
this.loading.set(true);
|
|
|
|
|
this.getDataService().subscribe({
|
|
|
|
|
next: (res) => {
|
|
|
|
|
if (Array.isArray(res)) {
|
|
|
|
|
this.items.set(res);
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
} else if ('data' in res) {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
this.items.set(res.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const defaultValue = this.control.value;
|
|
|
|
|
if (defaultValue) {
|
|
|
|
|
const selectedItem = this.items()?.find(
|
|
|
|
|
(item) => (item as Record<string, any>)[this.optionValue] == defaultValue,
|
|
|
|
|
) as T;
|
|
|
|
|
|
|
|
|
|
if (selectedItem) {
|
|
|
|
|
this.onSetDefaultDataItem.emit(selectedItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.loading.set(false);
|
|
|
|
|
},
|
|
|
|
|
error: () => {
|
|
|
|
|
this.loading.set(false);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDefaultDataItem(item: T) {
|
|
|
|
|
this.onSetDefaultDataItem.emit(item);
|
|
|
|
|
}
|
2025-12-10 16:54:25 +03:30
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
|
this.getData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onOpenForm() {
|
2025-12-15 18:00:45 +03:30
|
|
|
this.isOpenFormDialog.set(true);
|
2025-12-10 16:54:25 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get selectOptionValue() {
|
|
|
|
|
if (this.isFullDataOptionValue) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
return this.optionValue;
|
|
|
|
|
}
|
2025-12-23 20:35:20 +03:30
|
|
|
|
|
|
|
|
change(value: Maybe<T>) {
|
|
|
|
|
if (typeof value !== 'object') {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
this.onChange.emit(this.items()?.find((item) => item[this.optionValue] === value));
|
|
|
|
|
} else {
|
|
|
|
|
this.onChange.emit(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-10 16:54:25 +03:30
|
|
|
}
|