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

72 lines
1.2 KiB
TypeScript
Raw Normal View History

import {
Directive,
ElementRef,
EventEmitter,
Input,
OnInit,
Output,
ViewChild,
} from '@angular/core'
import { ControlValueAccessor } from '@angular/forms'
import { v4 as uuidv4 } from 'uuid'
2020-10-28 18:02:30 +01:00
@Directive()
export class AbstractInputComponent<T> implements OnInit, ControlValueAccessor {
@ViewChild('inputField')
2021-01-05 22:11:42 +01:00
inputField: ElementRef
constructor() {}
2020-10-28 18:02:30 +01:00
onChange = (newValue: T) => {}
onTouched = () => {}
2020-10-28 18:02:30 +01:00
writeValue(newValue: any): void {
this.value = newValue
}
registerOnChange(fn: any): void {
this.onChange = fn
2020-10-28 18:02:30 +01:00
}
registerOnTouched(fn: any): void {
this.onTouched = fn
2020-10-28 18:02:30 +01:00
}
setDisabledState?(isDisabled: boolean): void {
this.disabled = isDisabled
2020-10-28 18:02:30 +01:00
}
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
2020-10-28 18:02:30 +01:00
@Input()
error: string
@Input()
hint: string
@Input()
horizontal: boolean = false
@Input()
removable: boolean = false
@Output()
removed: EventEmitter<AbstractInputComponent<any>> = new EventEmitter()
2020-10-28 18:02:30 +01:00
value: T
ngOnInit(): void {
this.inputId = uuidv4()
}
inputId: string
}