Pārlūkot izejas kodu

Generating auto meta for array members

Richard Knight 6 gadi atpakaļ
vecāks
revīzija
171b414fda

+ 7 - 2
src/app/_mock/testfields.v12.ts

@@ -1,7 +1,7 @@
 // TESTS: Repeating Groups Of Fields ( 1 -> N arrays of containers for multiple fields )
 
 const model = {
-	container: [
+	repeating: [
 		{
 			a: 1,
 			b: 2,
@@ -14,9 +14,11 @@ const model = {
 			c: 7,
 			d: 8
 		}
-	]
+	],
+	standard: 9
 };
 
+/*
 const meta = {
 	container: {
 		label: 'Repating Group',
@@ -36,5 +38,8 @@ const meta = {
 		]
 	}
 };
+*/
+
+const meta = {};
 
 export { model, meta };

+ 36 - 34
src/app/dynaform/services/_formdata-utils.ts

@@ -41,8 +41,15 @@ const keyValPairToMetaRecursive = ( [key, val] ) => {
 	const metaVal = keyValPairToMeta(innerVal, key);
 	return [key, metaVal];
 };
-const arrayMemberAutoName = val => isScalar(val) ? val : val.__typename || val.constructor.name;
-const arrayToMeta = array => array.map(val => ({ name: arrayMemberAutoName(val), 'value' : val }));
+
+// Here, if array member is an object, generate autoMeta for the object members 
+
+const arrayMemberAutoName = (val, i) => {
+	isScalar(val) ? val : val.__typename || val.constructor.name;
+}
+const arrayToMeta = array => array.map((val, i) => {
+	return { name: arrayMemberAutoName(val, i), 'value' : val };
+});
 
 const autoMeta = model => Object.entries(model)
 	.filter(kvPair => kvPair[0] !== '__')
@@ -70,50 +77,45 @@ const combineExtraMeta = (metaG, extraMeta, createFromExtra = false, containerSe
 			const metaFoG = metaG[key] || {};
 			const seed = isCon ? {} : containerSeed; // Container's don't seed themselves, only their children
 			
-			// ---------------------------------------
 
 			// OK, so we need to... expand repeating containers here
 			if (isRepeating) {
-				const repeatInAutoMeta = Array.isArray(metaG[key].meta) ? metaG[key].meta.length : 0;
+				
+				const repeatInAutoMeta = Array.isArray(metaFoG.meta) ? metaFoG.meta.length : 0;
 				const repeatInExtraMeta = val['initialRepeat'] || val['minRepeat'];
 				const repeat = Math.max(repeatInAutoMeta, repeatInExtraMeta);
+				console.log('repeat is', repeat);
 
-				// Extend array from model (if any) to length repeat, apdding with empty values
+				// Extend array from model (if any) to length repeat, adding with empty values
 				const repeatingGroup = repeatInAutoMeta ? 
-					[ ...metaG[key].meta, ...Array(repeat - repeatInAutoMeta).fill(metaG[key].meta[0]) ] // RETHING DEFAULT HERE - its first val from data repeated
+					[ ...metaFoG.meta, ...Array(repeat - repeatInAutoMeta).fill(metaFoG.meta[0]) ] // RETHING DEFAULT HERE - its first val from data repeated
 					:
-					arrayMemberAutoName(repeat).fill(val['meta'][0]);
-
-				if (Array.isArray(metaG[key])) {
-					combinedMeta[key] = combineExtraMeta(metaG[key], val['meta'][0], createFromExtra, val['seed']);
-				}
-
+					Array(repeat).fill({});
+					console.log('repeatingGroup is', repeatingGroup);
 
+				combinedMeta[key] = repeatingGroup.map(metaG => combineExtraMeta(metaG, val['meta'][0], createFromExtra, val['seed']));
+				console.log('combinedMeta is', combinedMeta[key]);
+			}
+			
+			else
+
+			{
+				const extra = isCon ?
+					{
+						...val,
+						meta: combineExtraMeta( // RECURSION
+							metaFoG.meta || {},
+							val['meta'],
+							createFromExtra,
+							val['seed'] || containerSeed // Inherit seeded data if this group's seed isn't set
+						)
+					}
+					:
+					val;
+				combinedMeta[key] = combineMetaForField(metaFoG, seed, extra);
 			}
 
-
-
-
-
-
-
-
-			// ---------------------------------------
-			// Maybe else?
 			
-			const extra = isCon ?
-				{
-					...val,
-					meta: combineExtraMeta( // RECURSION
-						metaFoG.meta || {},
-						val['meta'],
-						createFromExtra,
-						val['seed'] || containerSeed // Inherit seeded data if this group's seed isn't set
-					)
-				}
-				:
-				val;
-			combinedMeta[key] = combineMetaForField(metaFoG, seed, extra);
 		}
 	});
 	return { ...metaG, ...combinedMeta };