|
@@ -1,23 +1,36 @@
|
|
-import { Component, Input, Optional, OnInit } from '@angular/core';
|
|
|
|
-import { FormGroup, FormGroupName, ControlContainer } from '@angular/forms';
|
|
|
|
-import { KeysPipe } from '@pipes/keys.pipe';
|
|
|
|
|
|
+import { Component, Input, ViewChild, TemplateRef, Optional, OnInit } from '@angular/core';
|
|
|
|
+import { FormGroup, FormGroupName, AbstractControl, ControlContainer } from '@angular/forms';
|
|
|
|
+// import { KeysPipe } from '@pipes/keys.pipe';
|
|
|
|
+
|
|
|
|
+export interface DynarowContext {
|
|
|
|
+ control: AbstractControl;
|
|
|
|
+ meta: StringMap;
|
|
|
|
+}
|
|
|
|
|
|
@Component({
|
|
@Component({
|
|
selector: 'app-dynaform',
|
|
selector: 'app-dynaform',
|
|
templateUrl: './dynaform.component.html',
|
|
templateUrl: './dynaform.component.html',
|
|
styleUrls: ['./dynaform.component.scss'],
|
|
styleUrls: ['./dynaform.component.scss'],
|
|
- providers: [ KeysPipe ]
|
|
|
|
|
|
+ // providers: [ KeysPipe ]
|
|
})
|
|
})
|
|
export class DynaformComponent implements OnInit {
|
|
export class DynaformComponent implements OnInit {
|
|
|
|
|
|
/*
|
|
/*
|
|
- * USAGE
|
|
|
|
- * Supply with either a FormGroup or the (string) name of a FormGroup, and the form meta data
|
|
|
|
|
|
+ * DynaformComponent: <app-dynaform>
|
|
|
|
+ *
|
|
|
|
+ * USAGE:
|
|
|
|
+ *
|
|
|
|
+ * Supply component with either a FormGroup or the name of a FormGroup,
|
|
|
|
+ * the group's meta data,
|
|
|
|
+ * and, optionally, a TemplateRef used as a field row template (overrides the default template)
|
|
|
|
+ *
|
|
* e.g.
|
|
* e.g.
|
|
* <app-dynaform [formGroup]="myForm" [meta]="myFormMetaData"></app-dynaform>
|
|
* <app-dynaform [formGroup]="myForm" [meta]="myFormMetaData"></app-dynaform>
|
|
* <app-dynaform formGroupName="myFormGroupName" [meta]="myFormMetaData"></app-dynaform>
|
|
* <app-dynaform formGroupName="myFormGroupName" [meta]="myFormMetaData"></app-dynaform>
|
|
|
|
+ * <app-dynaform [formGroup]="myForm" [meta]="myFormMetaData" [template]="myFieldTemplate"></app-dynaform>
|
|
*
|
|
*
|
|
* If supplied with just a FormGroupName it will retieve the FormGroup from the injected ControlContainer
|
|
* If supplied with just a FormGroupName it will retieve the FormGroup from the injected ControlContainer
|
|
|
|
+ *
|
|
*/
|
|
*/
|
|
|
|
|
|
@Input()
|
|
@Input()
|
|
@@ -31,29 +44,35 @@ export class DynaformComponent implements OnInit {
|
|
this.formMetaData = data;
|
|
this.formMetaData = data;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Input()
|
|
|
|
+ template?: TemplateRef<any>;
|
|
|
|
+
|
|
formMetaData: any; // TODO: Tighten up type
|
|
formMetaData: any; // TODO: Tighten up type
|
|
- controlNames: Array<string>;
|
|
|
|
|
|
+ controlNames: string[];
|
|
|
|
+
|
|
|
|
+ ctx = {ccc: 123};
|
|
|
|
|
|
constructor(
|
|
constructor(
|
|
@Optional() private cc: ControlContainer
|
|
@Optional() private cc: ControlContainer
|
|
) {}
|
|
) {}
|
|
|
|
|
|
ngOnInit() {
|
|
ngOnInit() {
|
|
- console.log('Dynaform Component');
|
|
|
|
- if (this.formGroup) {
|
|
|
|
- console.log('Initailised with FormGroup dirtectly: Controls >');
|
|
|
|
- console.log(Object.keys(this.formGroup.controls));
|
|
|
|
- }
|
|
|
|
- if (this.formGroupName) {
|
|
|
|
- console.log('Initailised with FormGroupName:', this.formGroupName);
|
|
|
|
- this.formGroup = <FormGroup>this.cc.control; // Get the associated formGroup from the ControlContainer
|
|
|
|
- console.log('Loading Controls from ControlContainer >');
|
|
|
|
- console.log(Object.keys(this.formGroup.controls));
|
|
|
|
|
|
+ if (!this.formGroup && this.formGroupName) {
|
|
|
|
+ this.formGroup = <FormGroup>this.cc.control; // Get theFormGroup from the ControlContainer
|
|
}
|
|
}
|
|
if (!this.formGroup) {
|
|
if (!this.formGroup) {
|
|
throw new Error('Dynaform Component Initialised without [formGroup] or formGroupName');
|
|
throw new Error('Dynaform Component Initialised without [formGroup] or formGroupName');
|
|
}
|
|
}
|
|
|
|
+ this.controlNames = Object.keys(this.formGroup.controls);
|
|
|
|
+ console.log('Dynaform Component: Controls >');
|
|
|
|
+ console.log(this.controlNames);
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ getTemplateContext(controlName: string): DynarowContext {
|
|
|
|
+ return {
|
|
|
|
+ control: this.formGroup.get(controlName),
|
|
|
|
+ meta: this.formMetaData[controlName]
|
|
|
|
+ };
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|