UNPKG

@data-driven-forms/react-form-renderer

Version:

React Form Renderer. Data Driven Forms converts JSON form definitions into fully functional React forms.

169 lines 7.46 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseCondition = exports.unpackMappedCondition = void 0; var isEmpty_1 = __importDefault(require("lodash/isEmpty")); var get_1 = __importDefault(require("lodash/get")); var isEmptyValue = function (value) { if (value instanceof Date) { return isNaN(value.getTime()); } return typeof value === 'number' || value === true ? false : (0, isEmpty_1.default)(value); }; var fieldCondition = function (value, config) { if (config.isNotEmpty) { return !isEmptyValue(value); } if (config.isEmpty) { return isEmptyValue(value); } if (config.pattern) { var regExpPattern = RegExp(config.pattern, config.flags); return config.notMatch ? !regExpPattern.test(value) : regExpPattern.test(value); } if (typeof config.is === 'function') { return config.is(value, config); } if (Object.prototype.hasOwnProperty.call(config, 'greaterThan')) { return value > config.greaterThan; } if (Object.prototype.hasOwnProperty.call(config, 'greaterThanOrEqualTo')) { return value >= config.greaterThanOrEqualTo; } if (Object.prototype.hasOwnProperty.call(config, 'lessThan')) { return value < config.lessThan; } if (Object.prototype.hasOwnProperty.call(config, 'lessThanOrEqualTo')) { return value <= config.lessThanOrEqualTo; } var isMatched = Array.isArray(config.is) ? !!config.is.includes(value) : value === config.is; return config.notMatch ? !isMatched : isMatched; }; var allowedMappedAttributes = ['when', 'is']; var unpackMappedCondition = function (condition, conditionMapper) { if (conditionMapper === void 0) { conditionMapper = {}; } if (typeof condition.mappedAttributes !== 'object' || !condition.mappedAttributes) { return condition; } var mappedAttributes = condition.mappedAttributes; var internalCondition = __assign(__assign({}, condition), { mappedAttributes: undefined }); Object.entries(mappedAttributes).forEach(function (_a) { var key = _a[0], value = _a[1]; if (!allowedMappedAttributes.includes(key)) { console.error("Mapped condition attribute ".concat(key, " is not allowed! Allowed attributes are: ").concat(allowedMappedAttributes.join(', '))); return; } if (Array.isArray(value) && conditionMapper[value === null || value === void 0 ? void 0 : value[0]]) { var fnName = value[0], args = value.slice(1); var fn = conditionMapper[fnName]; internalCondition[key] = fn.apply(void 0, args); } else { console.error("Missing conditionMapper entry for ".concat(value, "!")); } }); return internalCondition; }; exports.unpackMappedCondition = unpackMappedCondition; var parseCondition = function (condition, values, field, conditionMapper) { if (conditionMapper === void 0) { conditionMapper = {}; } var positiveResult = { visible: true, result: true, }; var negativeResult = { visible: false, result: false, }; if (Array.isArray(condition)) { if (typeof condition[0].then === 'object') { positiveResult = __assign(__assign({}, positiveResult), condition[0].then); } if (typeof condition[0].else === 'object') { negativeResult = __assign(__assign({}, negativeResult), condition[0].else); } return !condition.map(function (cond) { return (0, exports.parseCondition)(cond, values, field, conditionMapper); }).some(function (_a) { var result = _a.result; return result === false; }) ? positiveResult : negativeResult; } var conditionInternal = (0, exports.unpackMappedCondition)(condition, conditionMapper); if (conditionInternal.then) { positiveResult = __assign(__assign({}, positiveResult), conditionInternal.then); } if (conditionInternal.else) { negativeResult = __assign(__assign({}, negativeResult), conditionInternal.else); } if (conditionInternal.and) { return !(Array.isArray(conditionInternal.and) ? conditionInternal.and : [conditionInternal.and]) .map(function (cond) { return (0, exports.parseCondition)(cond, values, field, conditionMapper); }) .some(function (_a) { var result = _a.result; return result === false; }) ? positiveResult : negativeResult; } if (conditionInternal.sequence) { return conditionInternal.sequence.reduce(function (acc, curr) { var result = (0, exports.parseCondition)(curr, values, field, conditionMapper); return { sets: __spreadArray(__spreadArray([], (acc.sets || []), true), (result.set ? [result.set] : []), true), visible: acc.visible || result.visible, result: acc.result || result.result, }; }, __assign(__assign({}, negativeResult), { sets: [] })); } if (conditionInternal.or) { return (Array.isArray(conditionInternal.or) ? conditionInternal.or : [conditionInternal.or]) .map(function (cond) { return (0, exports.parseCondition)(cond, values, field, conditionMapper); }) .some(function (_a) { var result = _a.result; return result === true; }) ? positiveResult : negativeResult; } if (conditionInternal.not) { return !(0, exports.parseCondition)(conditionInternal.not, values, field, conditionMapper).result ? positiveResult : negativeResult; } var finalWhen = typeof conditionInternal.when === 'function' ? conditionInternal.when(field.name) : conditionInternal.when; if (typeof finalWhen === 'string') { return fieldCondition((0, get_1.default)(values, finalWhen), conditionInternal) ? positiveResult : negativeResult; } if (Array.isArray(finalWhen)) { return finalWhen .map(function (fieldName) { return fieldCondition((0, get_1.default)(values, typeof fieldName === 'function' ? fieldName(field.name) : fieldName), conditionInternal); }) .find(function (cond) { return !!cond; }) ? positiveResult : negativeResult; } return negativeResult; }; exports.parseCondition = parseCondition; exports.default = exports.parseCondition; //# sourceMappingURL=parse-condition.js.map