common-ui/src/lib/inputs/form-field/form-field-component.directive.ts
Dan Percic 85d4207617 try v5
2022-06-20 22:13:31 +03:00

47 lines
1.1 KiB
TypeScript

import { Directive } from '@angular/core';
import { ControlValueAccessor, ValidationErrors, Validator } from '@angular/forms';
@Directive()
export abstract class FormFieldComponent<I> implements ControlValueAccessor, Validator {
touched = false;
disabled = false;
protected _value: I | undefined;
get value(): I | undefined {
return this._value;
}
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
onChange = (value?: I) => {};
onTouched = () => {};
markAsTouched() {
if (!this.touched) {
this.onTouched();
this.touched = true;
}
}
setDisabledState(disabled: boolean) {
this.disabled = disabled;
}
registerOnChange(onChange: (value?: I) => void): void {
this.onChange = onChange;
}
registerOnTouched(onTouched: () => void): void {
this.onTouched = onTouched;
}
writeValue(option: I): void {
this._value = option;
}
validate(): ValidationErrors | null {
return null;
}
}