ソースを参照

Adding addMissingNames function to utilities

Richard Knight 6 年 前
コミット
51fa6ae261
共有4 個のファイルを変更した41 個の追加16 個の削除を含む
  1. 1 1
      src/app/app.component.html
  2. 4 1
      src/app/app.component.ts
  3. 34 13
      src/app/dynaform/libs/index.ts
  4. 2 1
      tslint.json

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

@@ -11,7 +11,7 @@
 		<app-dynaform formGroupName="dynaformtest" [meta]="formMetaDataObj" [template]="tref"></app-dynaform>
 		<div calss="row">
 			<div class="col-12 pt-4">
-				<json-formatter [data]="form.value" open="2"></json-formatter>
+				<json-formatter [data]="form.value" open="3"></json-formatter>
 			</div>
 		</div>
 	</div>

+ 4 - 1
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 } from './dynaform/libs';
+import { buildFormGroup, addMissingNames } from './dynaform/libs';
 
 import { formMetaDataObj } from './_mock/testfields';
 
@@ -24,6 +24,9 @@ export class AppComponent implements OnInit {
 	}
 
 	ngOnInit() {
+		const test = {a: {prop: 1}, b: {prop: 2}};
+		console.log( addMissingNames(test) );
+		// console.log( addMissingNames(this.formMetaDataObj) );
 		const fg = buildFormGroup({
 			dynaformtest: { name: 'dynaformtest', meta: this.formMetaDataObj }
 		});

+ 34 - 13
src/app/dynaform/libs/index.ts

@@ -1,15 +1,42 @@
 import { FormBuilder, FormControl } from '@angular/forms';
-import { map, reduce } from 'lodash/fp';
+import { reduce } from 'lodash/fp';
+
+/*
+ * FORM UTILITIES
+ *
+ * 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
+ * --------------
+ * metaF   = metadata for Field
+ * metaG   = metadata for Group (possibly nested)
+ * metaFoG = metadata for Field Or Group
+ *
+ */
 
 const fb = new FormBuilder();
 
-// Add name if missing
-const addNameIfMissing = (val, key) => val.name ? val : Object.assign(val, { name: key });
-// TODO
+// 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
+const addNameIfMissing = (metaFoG, key) => metaFoG.name ? metaFoG : Object.assign(metaFoG, { name: key });
+const addNameToSelfAndChildren = ( [key, metaFoG] ) => {
+	metaFoG = addNameIfMissing(metaFoG, key);
+	if (isGroup(metaFoG)) {
+		metaFoG = addMissingNames(metaFoG.meta); // Recursion
+	}
+	return [key, metaFoG];
+};
+const addMissingNames = metaG => Object.entries(metaG)
+	.map(addNameToSelfAndChildren)
+	.reduce((res, [key, val]) => { res[key] = val; return res; }, {});
 
 // Build Form Control
-// metaF = metadata for Field
+// TODO: Flesh out function to build validators
 const buildControlState = metaF => ({ value: metaF.value || '', disabled: metaF.isDisabled });
 const buildValidators = metaF => ({
 	validators: null,
@@ -19,19 +46,13 @@ const buildValidators = metaF => ({
 const buildFormControl = metaF => new FormControl(buildControlState(metaF) /*, buildValidators(metaF) */);
 
 // Build Form Group Member
-// metaFoG = metadata for Field Or Group
-const isGroup = metaFoG => metaFoG.meta && !metaFoG._field;
 const buildFormGroupMember = metaFoG => isGroup(metaFoG) ? buildFormGroup(metaFoG.meta) : buildFormControl(metaFoG);
 
-// metaG = metadata for Group - possibly nested
+// Build Form Group
 const buildFormGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildFormGroupMember(metaFoG) });
 const buildFormGroup = metaG => fb.group(reduce(buildFormGroupReducerIteree, {}, metaG));
 
 // ---------------------------------------------------------------------------------------------------------------------
 // Exports
 
-export { buildFormGroup };
-
-
-
-
+export { addMissingNames, buildFormGroup };

+ 2 - 1
tslint.json

@@ -91,7 +91,8 @@
     "radix": true,
     "semicolon": [
       true,
-      "always"
+      "always",
+      "ignore-bound-class-methods"   
     ],
     "triple-equals": [
       true,