Browse Source

Adding tests for lazy metadata combination + default optios for RadioField

Richard Knight 6 years ago
parent
commit
5fc1956ba6

+ 3 - 1
src/app/_mock/testfields.v1.ts

@@ -1,3 +1,5 @@
+// TESTS: Generation of Modeled MetaData using field models
+
 import { ValueTransformer } from './../dynaform/interfaces';
 import * as fmd from './../dynaform/models'; // fmd = Form Meta Data
 
@@ -46,7 +48,7 @@ const disabledTextField = new fmd.TextField({
 const radioFieldHorizontal = new fmd.RadioField({
 	name: 'radioFieldHorizontal',
 	options: ['Fish', 'Fowl', 'Neither'],
-	horizontal: true
+		horizontal: true
 });
 
 

+ 2 - 0
src/app/_mock/testfields.v2.ts

@@ -1,3 +1,5 @@
+// TESTS: Generation of Modeled MetaData using buildModeledMeta library function
+
 import { ValueTransformer } from './../dynaform/interfaces';
 import { buildModeledMeta } from './../dynaform/libs';
 

+ 30 - 0
src/app/_mock/testfields.v3.ts

@@ -0,0 +1,30 @@
+// TESTS: Generation of Automatic MetaData from model using autoMeta library function
+// All fields will defatlt to type 'text'
+
+import { buildModeledMeta, autoMeta } from './../dynaform/libs';
+
+const model1 = {
+	a: 'Value 1',
+	b: 'Value 2',
+	c: 'Value 3',
+	d: ''
+};
+
+const model2 = {
+	a: 'Value 1',
+	b: 'Value 2',
+	c: 'Value 3',
+	d: {
+		e: 444,
+		f: 555,
+		g: {
+			h: true,
+			i: false
+		}
+	},
+	z: 'THE END'
+};
+
+export const formMetaDataObj = buildModeledMeta(autoMeta(model2));
+
+console.log(formMetaDataObj);

+ 36 - 0
src/app/_mock/testfields.v4.ts

@@ -0,0 +1,36 @@
+// TESTS: Lazy combination of Automatic MetaData with extra MetaData
+
+import { buildModeledMeta, autoMeta, combineExtraMeta } from './../dynaform/libs';
+
+const model2 = {
+	a: 'Value 1',
+	b: 'Value 2',
+	c: 'Maybe',
+	d: {
+		e: 444,
+		f: 555,
+		g: {
+			h: true,
+			i: false
+		}
+	},
+	z: 'THE END'
+};
+
+const extra2 = {
+	b: { type: 'checkbutton' },
+	c: { label: 'Property Three', type: 'radio', options: ['Yes', 'No', 'Maybe'], horizontal: 1 },
+	d: {
+		meta: {
+			e: { type: 'radio', 'label': 'Does it work yet?' },
+			f: { type: 'datepicker' }
+		}
+	}
+};
+
+
+const auto2 = autoMeta(model2);
+const combined2 = combineExtraMeta(auto2, extra2);
+export const formMetaDataObj = buildModeledMeta(combined2);
+
+console.log(formMetaDataObj);

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

@@ -2,7 +2,7 @@ import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
 import { FormBuilder, FormGroup } from '@angular/forms';
 import { buildFormGroup } from './dynaform/libs';
 
-import { formMetaDataObj } from './_mock/testfields.v2';
+import { formMetaDataObj } from './_mock/testfields.v4';
 
 @Component({
 	selector: 'app-root',

+ 8 - 9
src/app/dynaform/libs/index.ts

@@ -33,8 +33,8 @@ const addProp = (obj, key, val) => { obj[key] = val; return obj; };
 const isGroup = metaFoG => metaFoG.meta;
 
 // Is Container
-// Helper function to distinguish container group
-const isContainer = metaFoG => metaFoG.type === 'container';
+// Helper function to distinguish container group (a group of child fields)
+const isContainer = metaFoG => isGroup(metaFoG) && (metaFoG.type === 'container' || typeof metaFoG.type === 'undefined');
 
 // ---------------------------------------------------------------------------------------------------------------------
 // Add Missing Names
@@ -77,7 +77,7 @@ const combineExtraMeta = (metaG, metaExtra) => {
 	Object.entries(metaExtra).forEach(([key, val]) => {
 		if (typeof metaG[key] === 'object') {
 			combinedMeta[key] = (<any>val).meta ?
-				combineExtraMeta(metaG[key].meta, (<any>val).meta) :
+				combineMetaForField(metaG[key], { meta: combineExtraMeta(metaG[key].meta, (<any>val).meta) }) :
 				combineMetaForField(metaG[key], val);
 		}
 	});
@@ -95,14 +95,13 @@ const buildClassName = (t = 'text') => {
 	return start + 'Field';
 };
 
-const buildModeledField = metaF => {
-	// TODO: Add validity check
-	const fieldType = buildClassName(metaF.type);
+const buildModeledField = metaFoG => {
+	const type = isContainer(metaFoG) ? 'container' : metaFoG.type;
+	const fieldType = buildClassName(type);
 	if (!fmdModels[fieldType]) {
-		throw new Error('Unknown Field Type');
+		throw new Error(`No model for field type "${type}"`);
 	}
-	const modeledMetaF = new fmdModels[fieldType](metaF);
-	return modeledMetaF;
+	return new fmdModels[fieldType](metaFoG);
 };
 
 // Build Form Group Member

+ 7 - 2
src/app/dynaform/models/index.ts

@@ -26,7 +26,7 @@ interface SimpleFieldMetaData {
 
 interface Option {
 	label: string;
-	value: string;
+	value: string | number | boolean;
 }
 
 interface OptionsFieldMetaData extends SimpleFieldMetaData {
@@ -52,7 +52,7 @@ interface TimePickerFieldMetaData extends SimpleFieldMetaData {
 
 abstract class SimpleField {
 	type = 'text';
-	name: string;
+	name = 'missingName';
 	origin?: string;
 	label?: string;
 	value;
@@ -97,6 +97,11 @@ abstract class OptionsField extends SimpleField {
 		super(meta);
 		if (Array.isArray(meta.options)) {
 			this.options = meta.options.reduce((acc, opt) => { acc.push(new Option(opt)); return acc; }, []);
+		} else {
+			this.options = [
+				new Option({ label: 'Yes', value: true }),
+				new Option({ label: 'No', value: false })
+			];
 		}
 	}
 }