import { Component, ChangeDetectionStrategy, Input, forwardRef, ViewEncapsulation, ViewChild, ElementRef, OnChanges, SimpleChanges } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import { CheckboxConfig } from 'mayvio-ui/checkbox';
import 'mayvio-ui/checkbox/css';
@Component({
selector: 'mv-checkbox',
standalone: true,
imports: [CommonModule],
template: `
`,
styleUrls: [],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CheckboxComponent),
multi: true,
},
],
})
export class CheckboxComponent implements ControlValueAccessor, CheckboxConfig, OnChanges {
@Input() label?: string;
@Input() error = false;
@Input() disabled = false;
@Input() indeterminate = false;
@Input() checked = false;
@Input() className = '';
@ViewChild('inputRef') inputRef!: ElementRef;
onChange = (val: boolean) => {};
onTouched = () => {};
ngOnChanges(changes: SimpleChanges) {
if (changes['indeterminate'] && this.inputRef) {
this.inputRef.nativeElement.indeterminate = this.indeterminate;
}
}
onChangeEvent(event: Event) {
const val = (event.target as HTMLInputElement).checked;
this.checked = val;
this.onChange(val);
}
writeValue(value: boolean): void {
this.checked = !!value;
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState?(isDisabled: boolean): void {
this.disabled = isDisabled;
}
}