12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import { FormBuilder, FormControl } from '@angular/forms';
- import { reduce } from 'lodash/fp';
- const fb = new FormBuilder();
- const addProp = (obj, key, val) => { obj[key] = val; return obj; };
- const isGroup = metaFoG => metaFoG.meta && !metaFoG._field;
- const addNameIfMissing = (metaFoG, key) => metaFoG.name ? metaFoG : addProp(metaFoG, 'name', key);
- const addNameToSelfAndChildren = ( [key, metaFoG] ) => {
- metaFoG = addNameIfMissing(metaFoG, key);
- if (isGroup(metaFoG)) {
- metaFoG.meta = addMissingNames(metaFoG.meta);
- }
- return [key, metaFoG];
- };
- const addMissingNames = metaG => Object.entries(metaG)
- .map(addNameToSelfAndChildren)
- .reduce((res, [key, val]) => { res[key] = val; return res; }, {});
- 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) );
- const buildFormGroupMember = metaFoG => isGroup(metaFoG) ? buildFormGroup(metaFoG.meta) : buildFormControl(metaFoG);
- const buildFormGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildFormGroupMember(metaFoG) });
- const buildFormGroup = metaG => fb.group(reduce(buildFormGroupReducerIteree, {}, metaG));
- export { addMissingNames, buildFormGroup };
|