UNPKG

maycur-business

Version:

maycur business react components of web

141 lines (107 loc) 4.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _ConditionPaser = _interopRequireDefault(require("../ConditionPaser")); var _lodash = _interopRequireDefault(require("lodash")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var cacheProperty = ['isFieldHide', 'required', 'isFieldReadOnly']; var DynamicFormCtrl = /*#__PURE__*/ function () { function DynamicFormCtrl(_ref) { var _this = this; var updateFormFields = _ref.updateFormFields; _classCallCheck(this, DynamicFormCtrl); this.init = function (formFields) { _lodash["default"].forEach(formFields, function (field) { if (!_lodash["default"].isEmpty(field.conditions)) { _this.hasCondistion = true; field.property["default"] = {}; cacheProperty.forEach(function (p) { field.property["default"][p] = field.property[p]; }); } }); return formFields; }; this.updateFormFields = updateFormFields; this.hasCondistion = false; } /** * 1.将有条件的字段的(isFieldHide, required, isFieldReadOnly)在property里面缓存一份,作为默认值 * 2.将所有条件都执行一遍 */ // NOTE 初始化formFields就可以判断表单是否包含条件 _createClass(DynamicFormCtrl, [{ key: "updateFieldProperty", value: function updateFieldProperty(formFields, field, result) { var targetField = _lodash["default"].find(formFields, { code: field.code }); //property中的default为空说明没有做init,直接返回 if (_lodash["default"].isEmpty(targetField.property["default"])) { return; } if (result) { cacheProperty.forEach(function (p) { targetField.property[p] = targetField.property.synthetic[p]; }); } else { targetField.property["default"] = targetField.property["default"] ? targetField.property["default"] : {}; cacheProperty.forEach(function (p) { targetField.property[p] = targetField.property["default"][p]; }); } targetField.lastResult = result; } /** * 检查更改的字段是否包含条件 */ }, { key: "checkFieldCondition", value: function checkFieldCondition(formFields, formData, changeFields, isFirstUpdate) { var _this2 = this; var _formFields = _lodash["default"].cloneDeep(formFields); var isUpdate = false; _lodash["default"].forEach(changeFields, function (val, identifier) { _lodash["default"].forEach(_formFields, function (field) { if (field.conditions) { var condition = _lodash["default"].find(_lodash["default"].flatten(field.conditions), { identifier: identifier }); //有条件依赖这个更改的identifier,则重新执行条件组 if (!_lodash["default"].isEmpty(condition)) { var _getResult = new _ConditionPaser["default"](field.conditions, formData).getResult(), conditionResult = _getResult.conditionResult; //如果和上次条件执行的结果一致,则不执行条件 // 如果第一次执行 没有上一次结果 if (!field.lastResult) { isUpdate = true; _this2.updateFieldProperty(_formFields, field, conditionResult); } else if (conditionResult !== field.lastResult) { isUpdate = true; _this2.updateFieldProperty(_formFields, field, conditionResult); } } } }); }); if (isUpdate || isFirstUpdate) { this.updateFormFields && this.updateFormFields(_formFields); } } /** * 表单中字段值改变回调 */ }, { key: "onFieldChange", value: function onFieldChange(formFields, formData, changedFields, isFirstUpdate) { this.checkFieldCondition(formFields, formData, changedFields, isFirstUpdate); } }]); return DynamicFormCtrl; }(); var _default = DynamicFormCtrl; exports["default"] = _default;