import { Component, ChangeDetectionStrategy, Input, forwardRef, ViewEncapsulation } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import { SelectConfig } from 'mayvio-ui/select';
import 'mayvio-ui/select/css';
export interface SelectOption {
label: string;
value: string;
disabled?: boolean;
}
@Component({
selector: 'mv-select',
standalone: true,
imports: [CommonModule],
template: `
`,
styleUrls: [],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectComponent),
multi: true,
},
],
})
export class SelectComponent implements ControlValueAccessor, SelectConfig {
@Input() size: 'sm' | 'md' | 'lg' = 'md';
@Input() error = false;
@Input() disabled = false;
@Input() options: SelectOption[] = [];
@Input() className = '';
value?: string;
onChange = (val: string) => {};
onTouched = () => {};
onChangeEvent(event: Event) {
const val = (event.target as HTMLSelectElement).value;
this.value = val;
this.onChange(val);
}
writeValue(value: string): void {
this.value = value;
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState?(isDisabled: boolean): void {
this.disabled = isDisabled;
}
}