Explorar el Código

Miing names added automatically when calling buildFormGroup

Richard Knight hace 6 años
padre
commit
aae63b0806
Se han modificado 2 ficheros con 9 adiciones y 11 borrados
  1. 3 5
      src/app/app.component.ts
  2. 6 6
      src/app/dynaform/libs/index.ts

+ 3 - 5
src/app/app.component.ts

@@ -1,6 +1,6 @@
 import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
 import { FormBuilder, FormGroup } from '@angular/forms';
-import { buildFormGroup, addMissingNames } from './dynaform/libs';
+import { buildFormGroup } from './dynaform/libs';
 
 import { formMetaDataObj } from './_mock/testfields';
 
@@ -27,10 +27,8 @@ export class AppComponent implements OnInit {
 		const fullFormMeta = {
 			dynaformtest: { meta: this.formMetaDataObj }
 		};
-		const withNamesAdded = addMissingNames(fullFormMeta);
-		const fg = buildFormGroup(withNamesAdded);
-		console.log(fg);
-		this.form = fg;
+		this.form = buildFormGroup(fullFormMeta);
+		console.log(this.form);
 	}
 }
 

+ 6 - 6
src/app/dynaform/libs/index.ts

@@ -7,7 +7,6 @@ import { reduce } from 'lodash/fp';
  * Exports
  * -------
  * buildFormGroup(metadata)  - builds FormGroups from metdata, recursively if necessary
- * addMissingNames(metadata) - adds any missing 'name' properties to Fields and Groups using property's key, recursively
  *
  * Variable names
  * --------------
@@ -24,7 +23,7 @@ const addProp = (obj, key, val) => { obj[key] = val; return obj; };
 // Helper function to distinguish group from field
 const isGroup = metaFoG => metaFoG.meta && !metaFoG._field;
 
-// Add names to both Fields and Groups if missing, using property's key
+// Adds any missing 'name' properties to Fields and Groups using property's key, recursively
 const addNameIfMissing = (metaFoG, key) => metaFoG.name ? metaFoG : addProp(metaFoG, 'name', key);
 const addNameToSelfAndChildren = ( [key, metaFoG] ) => {
 	metaFoG = addNameIfMissing(metaFoG, key);
@@ -35,7 +34,7 @@ const addNameToSelfAndChildren = ( [key, metaFoG] ) => {
 };
 const addMissingNames = metaG => Object.entries(metaG)
 	.map(addNameToSelfAndChildren)
-	.reduce((res, [key, val]) => { res[key] = val; return res; }, {});
+	.reduce((res, [key, val]) => addProp(res, key, val), {});
 
 // Build Form Control
 // TODO: Flesh out function to build validators
@@ -48,13 +47,14 @@ const buildValidators = metaF => ({
 const buildFormControl = metaF => new FormControl(buildControlState(metaF) /*, buildValidators(metaF) */);
 
 // Build Form Group Member
-const buildFormGroupMember = metaFoG => isGroup(metaFoG) ? buildFormGroup(metaFoG.meta) : buildFormControl(metaFoG);
+const buildFormGroupMember = metaFoG => isGroup(metaFoG) ? _buildFormGroup(metaFoG.meta) : buildFormControl(metaFoG);
 
 // Build Form Group
 const buildFormGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildFormGroupMember(metaFoG) });
-const buildFormGroup = metaG => fb.group(reduce(buildFormGroupReducerIteree, {}, metaG));
+const _buildFormGroup = metaG => fb.group(reduce(buildFormGroupReducerIteree, {}, metaG));
+const buildFormGroup = metaG => _buildFormGroup(addMissingNames(metaG));
 
 // ---------------------------------------------------------------------------------------------------------------------
 // Exports
 
-export { addMissingNames, buildFormGroup };
+export { buildFormGroup };