|
@@ -1,24 +1,32 @@
|
|
|
-import { FormBuilder, FormGroup } from '@angular/forms';
|
|
|
+import { FormBuilder, FormControl } from '@angular/forms';
|
|
|
import { reduce } from 'lodash/fp';
|
|
|
|
|
|
-// Utility function for extracting a FormGroup from a form metadata object - currently non-nested only
|
|
|
-const reducerIteree = (res, field) => Object.assign(res, { [field.name]: field.value || '' });
|
|
|
-const _extractFormGroupData = reduce(reducerIteree, {});
|
|
|
-const extractFormGroupData = nonNestedFormMetaData => _extractFormGroupData(nonNestedFormMetaData);
|
|
|
-
|
|
|
-// Build Form Group
|
|
|
const fb = new FormBuilder();
|
|
|
-const buildNonNestedFormGroup = nonNestedFormMetaData => fb.group(extractFormGroupData(nonNestedFormMetaData));
|
|
|
-const nestedReducerIteree = (acc, fieldOrGroup) => {
|
|
|
- console.log(Array.isArray(fieldOrGroup.meta));
|
|
|
- return Array.isArray(fieldOrGroup.meta) ?
|
|
|
- buildNestedFormGroup(fieldOrGroup.meta) :
|
|
|
- buildNonNestedFormGroup(fieldOrGroup);
|
|
|
-};
|
|
|
-const _buildNestedFormGroup = reduce(nestedReducerIteree, {});
|
|
|
-const buildNestedFormGroup = nestedFormMetaData => _buildNestedFormGroup(nestedFormMetaData);
|
|
|
+
|
|
|
+// Build Form Control
|
|
|
+// metaF = metadata for Field
|
|
|
+const buildControlState = metaF => ({ value: metaF.value || '', disabled: metaF.isDisabled });
|
|
|
+const buildValidators = metaF => ({
|
|
|
+ validators: null,
|
|
|
+ asyncValidators: null,
|
|
|
+ updateOn: 'blur'
|
|
|
+});
|
|
|
+const buildFormControl = metaF => new FormControl(buildControlState(metaF) /*, buildValidators(metaF) */);
|
|
|
+
|
|
|
+// Build Form Group Member
|
|
|
+// metaFoG = metadata for Field Or Group
|
|
|
+const isGroup = metaFoG => Array.isArray(metaFoG.meta);
|
|
|
+const buildFormGroupMember = metaFoG => isGroup(metaFoG) ? buildFormGroup(metaFoG.meta) : buildFormControl(metaFoG);
|
|
|
+
|
|
|
+// metaG = metadata for Group - possibly nested
|
|
|
+const buildFormGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildFormGroupMember(metaFoG) });
|
|
|
+const buildFormGroup = metaG => fb.group(reduce(buildFormGroupReducerIteree, {}, metaG));
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
|
// Exports
|
|
|
|
|
|
-export { extractFormGroupData, buildNestedFormGroup };
|
|
|
+export { buildFormGroup };
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|