Преглед на файлове

Adding dedicated getFormGroup path function

Richard Knight преди 6 години
родител
ревизия
8c5bcaec15
променени са 2 файла, в които са добавени 22 реда и са изтрити 20 реда
  1. 2 2
      src/app/app.component.html
  2. 20 18
      src/app/dynaform/dynaform.component.ts

+ 2 - 2
src/app/app.component.html

@@ -10,8 +10,8 @@
 	<form [formGroup]="form">
 		<span formGroupName="dynaformtest">
 			<span formGroupName="d">
-				<app-dynaform [formGroup]="form" [meta]="meta" [template]="tref"></app-dynaform>
-				<!-- <app-dynaform formGroupName="g" [meta]="meta" [template]="tref"></app-dynaform> -->
+				<!-- <app-dynaform [formGroup]="form.controls.dynaformtest.controls.d.controls.g" [meta]="meta" [template]="tref"></app-dynaform> -->
+				<app-dynaform formGroupName="g" [meta]="meta" [template]="tref"></app-dynaform>
 			</span>
 		</span>
 	</form>

+ 20 - 18
src/app/dynaform/dynaform.component.ts

@@ -58,24 +58,6 @@ export class DynaformComponent implements OnInit {
 	) {}
 
 	ngOnInit() {
-		
-		// Get the full path in the tree (used to match metadata)
-		let parents = [];
-		if (this.formGroup) {
-			// formGroup supplied directly, so reconstruct current FormGroup's full path
-			let fg = this.formGroup;
-			while (fg.parent) {
-				// Find the identity (i.e. object key) of the parent FormGroup that IS FormGroup 'fg'
-				const fgIdentity = Object.entries(fg.parent.controls).filter(([key, candidate]) => candidate === fg);
-				parents.push(fgIdentity[0][0]);
-				fg = <FormGroup>fg.parent;
-			}
-		} else if (this.formGroupName) {
-			// formGroupName supplied, so get the full path from the ControlContainer
-			parents = this.cc.path.reverse();
-		}
-		const path = parents;
-		
 		// Get the formGroup from the formGroupName if necessary
 		if (!this.formGroup && this.formGroupName) {
 			this.formGroup = <FormGroup>this.cc.control; // Get theFormGroup from the ControlContainer
@@ -87,6 +69,7 @@ export class DynaformComponent implements OnInit {
 
 		// If we're given a formGroupName or nested FormFroup, and the form's full (or partial but fuller) metadata tree,
 		// drill down to find the corresponding FormGroup's metadata
+		const path = this.getFormGroupPath();
 		const metaDataKeysExpected = this.controlNames.join(',');
 		while (path.length && metaDataKeysExpected !== Object.keys(this.formMetaData).join(',')) {
 			const branch = path.pop();
@@ -104,6 +87,25 @@ export class DynaformComponent implements OnInit {
 		}
 	}
 
+	getFormGroupPath(): string[] {
+		// Get the full path of the FormGroup (used to match and validate metadata)
+		let path = [];
+		if (!this.formGroup && this.formGroupName) {
+			// formGroupName supplied, so get the full path from the ControlContainer
+			path = this.cc.path.reverse();
+		} else {
+			// formGroup supplied directly, so reconstruct current FormGroup's full path
+			let fg = this.formGroup;
+			while (fg.parent) {
+				// Find the identity of 'fg' in the parent FormGroup's controls
+				const fgIdentity = Object.entries(fg.parent.controls).filter(([key, candidate]) => candidate === fg).pop();
+				path.push(fgIdentity[0]);
+				fg = <FormGroup>fg.parent;
+			}
+		}
+		return path;
+	}
+	
 	getTemplateContext(controlName: string): DynarowContext {
 		return {
 			control: this.formGroup.get(controlName),