UNPKG

informed

Version:

A lightweight framework and utility for building powerful forms in React applications

164 lines (151 loc) 5.9 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _rollupPluginBabelHelpers = require('../_virtual/_rollupPluginBabelHelpers.js'); var React = require('react'); var useFormController = require('../hooks/useFormController.js'); var utils = require('../utils.js'); var FormField = require('./FormField.js'); var Relevant = require('./Relevant.js'); var UpdateFields = require('./UpdateFields.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var React__default = /*#__PURE__*/_interopDefaultLegacy(React); // const logger = Debug('informed:FormFields' + '\t'); var FormFields = function FormFields(_ref) { var schema = _ref.schema, onlyValidateSchema = _ref.onlyValidateSchema; var _useFormController = useFormController.useFormController(), getOptions = _useFormController.getOptions; var _getOptions = getOptions(), componentsMap = _getOptions.components; var fields = React.useMemo(function () { var _computeFieldsFromSch = utils.computeFieldsFromSchema(schema, onlyValidateSchema), properties = _computeFieldsFromSch.properties, conditions = _computeFieldsFromSch.conditions, components = _computeFieldsFromSch.components; var mappedProperties = properties.map(function (name) { // Not for // ui:component:id ---> foobar // Only for // ui:foobar ---> foobar if (name.includes('ui:') && name.split(':').length !== 3) { return { $id: name.split('ui:')[1] }; } // Need to get required off schema var required = schema.required && schema.required.includes(name) ? true : undefined; var uuid = utils.uuidv4(); var Component = /*#__PURE__*/React__default["default"].createElement(FormField.FormField, { name: name, schema: schema, key: "schema-field-".concat(name, "-").concat(uuid), required: required }); return { Component: Component }; }); var mappedComponents = components.map(function (component) { // console.log('WTF', component); if (component['ui:control']) { var RenderedComponent = componentsMap[component['ui:control']]; var Component = /*#__PURE__*/React__default["default"].createElement(RenderedComponent, null, /*#__PURE__*/React__default["default"].createElement(FormFields, { schema: component })); return { Component: Component, $id: component.$id }; } }); // For conditionals var mappedConditionals = conditions.map(function (conditional, j) { // Example then ( its a subschema ) // then: { // properties: { // spouse: { // type: 'string', // title: 'Spouse name', // 'ui:control': 'input' // } // } // } var subSchema = conditional.then; // console.log('SUBSCHEMA', subSchema); var $id = conditional.$id; var thenProps = conditional.thenProps; // Turn the if into a when function for informed // Example if condition // if: { // properties: { // married: { const: 'yes' } // }, // required: ['married'] // }, var conditions = conditional["if"].properties; var when = function when(_ref2) { var formApi = _ref2.formApi, scope = _ref2.scope; // Example key "married, Example condition: "{ const: 'yes' }" return Object.entries(conditions).every(function (_ref3) { var _ref4 = _rollupPluginBabelHelpers.slicedToArray(_ref3, 2), propertyName = _ref4[0], condition = _ref4[1]; return utils.checkCondition(condition, formApi.getValue(scope ? "".concat(scope, ".").concat(propertyName) : propertyName)); }); }; var Component = /*#__PURE__*/React__default["default"].createElement(Relevant.Relevant, { key: "Conditional-ScheamField-".concat(j), when: when }, subSchema ? /*#__PURE__*/React__default["default"].createElement(FormFields, { schema: subSchema }) : null, thenProps ? /*#__PURE__*/React__default["default"].createElement(UpdateFields.UpdateFields, { schema: thenProps }) : null); return { Component: Component, $id: $id }; }); var mappedFields = []; // Iterate through the mapped properties mappedProperties.forEach(function (_ref5) { var $id = _ref5.$id, Component = _ref5.Component; if (Component) { mappedFields.push(Component); } else if ($id) { // Grab the id from the mapped conditionals var conditional = mappedConditionals.find(function (c) { return c.$id === $id; }); var component = mappedComponents.find(function (c) { return c.$id === $id; }); if (conditional) { mappedFields.push(conditional.Component); // Make sure to take it off so we dont render it twice ( defaults at the end ) var index = mappedConditionals.findIndex(function (c) { return c.$id === $id; }); mappedConditionals.splice(index, 1); } else if (component) { mappedFields.push(component.Component); } else { console.error('MappedConditionals', mappedConditionals); throw new Error("Unable to find mapping for ".concat($id)); } } else { throw new Error('Found property with no ID or component...'); } }); // Add whatever is left at the end mappedConditionals.forEach(function (_ref6) { var Component = _ref6.Component; mappedFields.push(Component); }); return mappedFields; }, [schema]); return fields; }; exports.FormFields = FormFields;