UNPKG

@timmons-group/config-form

Version:

React Components and helpers to build a form via configuration with react-hook-form and MUI

81 lines (78 loc) 2.67 kB
import { FIELD_TYPES } from '../constants.js'; import { checkConditional } from './formHelpers.js'; const createTextModel = (name, label, required = false, otherThings = {}, dataThings = {}) => ({ label, path: name, type: 0, model: { name, type: 0, data: dataThings }, required, ...otherThings }); const createLongTextModel = (name, label, required = false, otherThings = {}, dataThings = {}) => ({ label, path: name, type: FIELD_TYPES.LONG_TEXT, model: { name, type: FIELD_TYPES.LONG_TEXT, data: dataThings }, required, ...otherThings }); const createCurrencyModel = (name, label, required = false, otherThings = {}) => { const nonNeg = { ...otherThings, minValue: 0 }; return createAnyModel(FIELD_TYPES.CURRENCY, name, label, required, nonNeg); }; const createPositiveCountModel = (name, label, required = false, otherThings = {}) => createAnyModel(FIELD_TYPES.INT, name, label, required, { ...otherThings, minValue: 0 }); const createAcresModel = (name, label, required = false, otherThings = {}) => createAnyModel(FIELD_TYPES.FLOAT, name, label, required, { ...otherThings, minValue: 0, fractionalDigits: 1 }); const createAnyModel = (fieldType, name, label, required = false, otherThings = {}) => { const type = fieldType ?? FIELD_TYPES.TEXT; return { label, path: name, type, model: { name, id: 5, type }, required, ...otherThings }; }; const passesConditionals = (conditions, data = {}, nested = "") => { const indent = nested; console.log(`${indent}`, "conditions", conditions); let result = true; const toProcess = Array.isArray(conditions) ? conditions : [conditions]; for (const element of toProcess) { const condition = element; const { and, or } = condition; if (!checkConditional(condition, data)) { result = false; if (and || !or) { console.log(`${indent}`, " Failed test. Has ands or no ors"); break; } if (or) { console.log(`${indent}`, " Maybe Failed test. HAS ors, checking those conditions"); result = passesConditionals(or, data, nested + " "); } } else { console.log(`${indent}`, " Passed this condition. Checking for ands"); } if (and) { console.log(`${indent}`, " Has ands"); result = passesConditionals(and, data, nested + " "); } } console.log(`${indent}`, "result of", result, "for", conditions); return result; }; export { createAcresModel, createAnyModel, createCurrencyModel, createLongTextModel, createPositiveCountModel, createTextModel, passesConditionals }; //# sourceMappingURL=layoutHelpers.js.map