Files
pireon-ui/libs/ui/src/lib/util/form-control/form-control.ts

53 lines
1.0 KiB
TypeScript

import { Injectable, Injector } from '@angular/core';
import { ControlContainer, ControlValueAccessor } from '@angular/forms';
@Injectable()
export class PiFormControl implements ControlValueAccessor {
value!: number | string | Array<string> | boolean;
onChange = (value: number | string | Array<string> | boolean) => {};
onTouched = () => {};
disabled = false;
focused = false;
touched = false;
constructor(protected injector: Injector) {}
get controlContainer(): any {
return this.injector.get(ControlContainer);
}
writeValue(value: number | string | Array<string> | boolean) {
this.value = value;
}
registerOnChange(onChange: any) {
this.onChange = onChange;
}
registerOnTouched(onTouched: any) {
this.onTouched = onTouched;
}
markAsTouched() {
if (!this.touched) {
this.onTouched();
this.touched = true;
}
}
setDisabledState(disabled: boolean) {
this.disabled = disabled;
}
onFocus(): void {
this.focused = true;
}
onBlur(): void {
this.focused = false;
}
}