|
|
@@ -1,19 +1,16 @@
|
|
|
-import { Directive, ElementRef, Optional, Self } from '@angular/core';
|
|
|
-import {
|
|
|
- FormControl,
|
|
|
- FormControlDirective,
|
|
|
- FormControlName,
|
|
|
- FormGroup,
|
|
|
- FormGroupDirective,
|
|
|
- NgControl,
|
|
|
- NgForm,
|
|
|
-} from '@angular/forms';
|
|
|
+import { Directive, ElementRef, Optional } from '@angular/core';
|
|
|
+import { FormControl, NgControl } from '@angular/forms';
|
|
|
+
|
|
|
+type InputElement = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
|
|
|
|
|
// tslint:disable:directive-selector
|
|
|
@Directive({ selector: 'input, textarea, select' })
|
|
|
export class FormFieldControlDirective {
|
|
|
formControl: FormControl;
|
|
|
- constructor(@Optional() private formControlName: NgControl) {}
|
|
|
+ constructor(
|
|
|
+ private elementRef: ElementRef<InputElement>,
|
|
|
+ @Optional() private formControlName: NgControl,
|
|
|
+ ) {}
|
|
|
|
|
|
get valid(): boolean {
|
|
|
return !!this.formControlName && !!this.formControlName.valid;
|
|
|
@@ -22,4 +19,17 @@ export class FormFieldControlDirective {
|
|
|
get touched(): boolean {
|
|
|
return !!this.formControlName && !!this.formControlName.touched;
|
|
|
}
|
|
|
+
|
|
|
+ setReadOnly(value: boolean) {
|
|
|
+ const input = this.elementRef.nativeElement;
|
|
|
+ if (isSelectElement(input)) {
|
|
|
+ input.disabled = value;
|
|
|
+ } else {
|
|
|
+ input.readOnly = value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function isSelectElement(value: InputElement): value is HTMLSelectElement {
|
|
|
+ return value.hasOwnProperty('selectedIndex');
|
|
|
}
|