Bladeren bron

Modelled metadata generation now working for Repeating Containers (array type)

Richard Knight 6 jaren geleden
bovenliggende
commit
a4d96d7ef2
2 gewijzigde bestanden met toevoegingen van 15 en 9 verwijderingen
  1. 1 1
      src/app/app.component.html
  2. 14 8
      src/app/dynaform/services/_formdata-utils.ts

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

@@ -4,7 +4,7 @@
 			<h1>NgDynaform</h1>
 			<p>
 			Dynamic Form Layout Module<br>
-			Load different tests by appending a query param to the URL <b>?test=N</b> (where N is a number between 1 and 11).<br>
+			Load different tests by appending a query param to the URL <b>?test=N</b> (where N is a number between 1 and 12).<br>
 			NOTE: Model set to update on change, but this can be set to blur or submit for less re-rendering.
 			</p>
 		</div>

+ 14 - 8
src/app/dynaform/services/_formdata-utils.ts

@@ -17,6 +17,7 @@
  * metaF   = metadata for Field
  * metaG   = metadata for Group (possibly nested)
  * metaFoG = metadata for Field or Group
+ * rcMem   = Repeating Container member
  *
  */
 
@@ -42,11 +43,6 @@ const keyValPairToMetaRecursive = ( [key, val] ) => {
 	return [key, metaVal];
 };
 
-/*
-const arrayMemberAutoName = (val, i) => {
-	isScalar(val) ? val : val.__typename || val.constructor.name;
-}
-*/
 const arrayToMeta = array => array.map((val, i) => {
 	if (isScalar(val)) {
 		return { name: `item${i + 1}`, 'value' : val };
@@ -137,6 +133,9 @@ const buildFieldClassName = (t = 'text') => {
 
 const buildModeledField = metaFoG => {
 	const type = isContainer(metaFoG) ? (isRepeatingContainer(metaFoG) ? 'repeatingContainer' : 'container') : metaFoG.type;
+	// console.log('-----------------');
+	// console.log(metaFoG);
+	// console.log(type);
 	const className = buildFieldClassName(type);
 	if (!fmdModels[className]) {
 		throw new Error(`No metadata model "${className}" for type "${type}"`);
@@ -153,9 +152,11 @@ const buildModeledFieldGroupMember = metaFoG => {
 	return modeledGroupMember;
 };
 
-// Build Form Group
+// Build Form Group ********* CONTINUE WORK HERE
 const buildModeledFieldGroupReducerIteree = (res, metaFoG) => ({ ...res, [metaFoG.name]: buildModeledFieldGroupMember(metaFoG) });
-const _buildFieldSpecificMeta = metaG => reduce(buildModeledFieldGroupReducerIteree, {}, metaG);
+const _buildFieldSpecificMeta = metaG => Array.isArray(metaG) ?
+	metaG.map(rcMem => _buildFieldSpecificMeta(rcMem)) :
+	reduce(buildModeledFieldGroupReducerIteree, {}, metaG);
 const buildFieldSpecificMeta = metaG => _buildFieldSpecificMeta(addMissingNames(metaG));
 
 
@@ -456,9 +457,14 @@ const isContainer = (metaFoG): boolean => isGroup(metaFoG) && (!metaFoG.type ||
 // Helper function to distinguish a repeating container group (a group of child fields that can be repeated 1...N times)
 const isRepeatingContainer = (metaFoG): boolean => Array.isArray(metaFoG.meta);
 
+// Is Repeating Container Member
+// It looks like one if it has neither a value or meta
+const isRepeatingContainerMember = (possibleRcMem): boolean => possibleRcMem.value === undefined && possibleRcMem.meta === undefined;
+
 // Add Missing Names
 // Helper function to add any missing 'name' properties to Fields and Groups using property's key, recursively
-const addNameIfMissing = (metaFoG, key) => metaFoG.name ? metaFoG : addProp(metaFoG, 'name', key);
+// BUT not to repeatingContainer members
+const addNameIfMissing = (metaFoG, key) => metaFoG.name || isRepeatingContainerMember(metaFoG) ? metaFoG : addProp(metaFoG, 'name', key);
 const addNameToSelfAndChildren = ( [key, metaFoG] ) => {
 	metaFoG = addNameIfMissing(metaFoG, key);
 	if (isGroup(metaFoG)) {