|
@@ -1,8 +1,13 @@
|
|
|
import {
|
|
|
Directive, ComponentFactoryResolver, ComponentRef, ViewContainerRef,
|
|
|
- Input, Output, EventEmitter, OnInit, SkipSelf
|
|
|
+ Input, Output, EventEmitter, OnInit, OnDestroy,
|
|
|
+ Optional, Self, SkipSelf, Inject
|
|
|
} from '@angular/core';
|
|
|
-import { Form, FormControl, ControlContainer, NgControl, ControlValueAccessor, ValidatorFn, AsyncValidatorFn } from '@angular/forms';
|
|
|
+import {
|
|
|
+ Form, FormControl, AbstractControl,
|
|
|
+ NgControl, ControlContainer, ControlValueAccessor,
|
|
|
+ NG_VALIDATORS, Validator, Validators, ValidatorFn, AsyncValidatorFn
|
|
|
+} from '@angular/forms';
|
|
|
|
|
|
import * as formFieldComponents from './../components';
|
|
|
|
|
@@ -17,7 +22,7 @@ type FFCCustom = FFC & ControlValueAccessor;
|
|
|
|
|
|
selector: '[dynafield]'
|
|
|
})
|
|
|
-export class DynafieldDirective extends NgControl implements OnInit {
|
|
|
+export class DynafieldDirective extends NgControl implements OnInit, OnDestroy {
|
|
|
|
|
|
@Input()
|
|
|
meta: StringMap;
|
|
@@ -41,7 +46,8 @@ export class DynafieldDirective extends NgControl implements OnInit {
|
|
|
constructor(
|
|
|
private resolver: ComponentFactoryResolver,
|
|
|
private container: ViewContainerRef,
|
|
|
- @SkipSelf() private cc: ControlContainer
|
|
|
+ @SkipSelf() private cc: ControlContainer,
|
|
|
+ @Optional() @Self() @Inject(NG_VALIDATORS) private validators: Array<Validator | ValidatorFn>
|
|
|
) {
|
|
|
super();
|
|
|
}
|
|
@@ -96,6 +102,13 @@ export class DynafieldDirective extends NgControl implements OnInit {
|
|
|
|
|
|
this.name = name;
|
|
|
this.valueAccessor = <FFCCustom>this.component.instance;
|
|
|
+
|
|
|
+ const ngValidators = this.component.injector.get(NG_VALIDATORS, null);
|
|
|
+ console.log('ngValidators', ngValidators);
|
|
|
+ if (ngValidators && ngValidators.some(x => x as any === this.component.instance)) {
|
|
|
+ this.validators = [...(this.validators || []), ...(ngValidators as Array<Validator|ValidatorFn>)];
|
|
|
+ }
|
|
|
+ */
|
|
|
this._control = this.formGroupDirective.addControl(this);
|
|
|
}
|
|
|
} catch (e) {
|
|
@@ -104,6 +117,15 @@ export class DynafieldDirective extends NgControl implements OnInit {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ ngOnDestroy(): void {
|
|
|
+ if (this.formGroupDirective) {
|
|
|
+ this.formGroupDirective.removeControl(this);
|
|
|
+ }
|
|
|
+ if (this.component) {
|
|
|
+ this.component.destroy();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
get path(): string[] {
|
|
|
return [...this.cc.path, this.name];
|
|
|
}
|
|
@@ -112,13 +134,24 @@ export class DynafieldDirective extends NgControl implements OnInit {
|
|
|
return this.cc ? this.cc.formDirective : null;
|
|
|
}
|
|
|
|
|
|
- get validator(): ValidatorFn | null { return null; }
|
|
|
+ get validator(): ValidatorFn | null {
|
|
|
+ return this.validators !== null ? Validators.compose(this.validators.map(this.normalizeValidator)) : null;
|
|
|
+ }
|
|
|
|
|
|
get asyncValidator(): AsyncValidatorFn { return null; }
|
|
|
|
|
|
+
|
|
|
viewToModelUpdate(newValue: any): void {
|
|
|
this.update.emit(newValue);
|
|
|
}
|
|
|
|
|
|
+ normalizeValidator(validator: ValidatorFn | Validator): ValidatorFn {
|
|
|
+ if ((<Validator>validator).validate) {
|
|
|
+ return (c: AbstractControl) => (<Validator>validator).validate(c);
|
|
|
+ } else {
|
|
|
+ return <ValidatorFn>validator;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|