paperless-ngx/src-ui/src/app/components/common/input/abstract-input.ts

56 lines
1,015 B
TypeScript
Raw Normal View History

2021-01-05 22:11:42 +01:00
import { Directive, ElementRef, Input, OnInit, ViewChild } from '@angular/core';
2020-12-14 20:20:35 +01:00
import { ControlValueAccessor } from '@angular/forms';
2020-10-28 18:02:30 +01:00
import { v4 as uuidv4 } from 'uuid';
@Directive()
export class AbstractInputComponent<T> implements OnInit, ControlValueAccessor {
2021-01-05 22:11:42 +01:00
@ViewChild("inputField")
inputField: ElementRef
2020-10-28 18:02:30 +01:00
constructor() { }
onChange = (newValue: T) => {};
onTouched = () => {};
writeValue(newValue: any): void {
this.value = newValue
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState?(isDisabled: boolean): void {
this.disabled = isDisabled;
}
2021-01-05 22:11:42 +01:00
focus() {
if (this.inputField && this.inputField.nativeElement) {
this.inputField.nativeElement.focus()
}
}
2020-10-28 18:02:30 +01:00
@Input()
title: string
@Input()
disabled = false;
@Input()
error: string
2020-10-28 18:02:30 +01:00
value: T
ngOnInit(): void {
this.inputId = uuidv4()
}
inputId: string
@Input()
hint: string
}