UNPKG

@fe6/water-pro

Version:

An enterprise-class UI design language and Vue-based implementation

643 lines (516 loc) 21.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useFormEvents = useFormEvents; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _tslib = require("tslib"); var _vue = require("vue"); var _lodashEs = require("lodash"); var _shared = require("@fe6/shared"); var _warning = _interopRequireDefault(require("../../../_util/warning")); var _helper = require("../helper"); var _date = require("../date"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function useFormEvents(_ref) { var emit = _ref.emit, getProps = _ref.getProps, formModel = _ref.formModel, getSchema = _ref.getSchema, defaultValueRef = _ref.defaultValueRef, formElRef = _ref.formElRef, schemaRef = _ref.schemaRef, getOriginSchema = _ref.getOriginSchema, handleFormValues = _ref.handleFormValues; function resetFields() { var emitReset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; var triggerSubmit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee() { var _unref, resetFunc, resetOnSubmit, formEl; return _regenerator.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _unref = (0, _vue.unref)(getProps), resetFunc = _unref.resetFunc, resetOnSubmit = _unref.resetOnSubmit; _context.t0 = resetFunc && (0, _shared.isFunction)(resetFunc); if (!_context.t0) { _context.next = 5; break; } _context.next = 5; return resetFunc(); case 5: formEl = (0, _vue.unref)(formElRef); if (formEl) { _context.next = 8; break; } return _context.abrupt("return"); case 8: Object.keys(formModel).forEach(function (key) { formModel[key] = defaultValueRef.value[key]; }); clearValidate(); if (emitReset) { emit('reset', (0, _vue.toRaw)(formModel)); } triggerSubmit && resetOnSubmit && handleSubmit(false); case 12: case "end": return _context.stop(); } } }, _callee); })); } /** * @description: Set form value */ function setFieldsValue(values) { return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee2() { var fields, validKeys; return _regenerator.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: fields = (0, _vue.unref)(getSchema).map(function (item) { return item.field; }).filter(Boolean); validKeys = []; Object.keys(values).forEach(function (key) { var schema = (0, _vue.unref)(getSchema).find(function (item) { return item.field === key; }); var value = values[key]; var hasKey = Reflect.has(values, key); value = (0, _helper.handleInputNumberValue)(schema === null || schema === void 0 ? void 0 : schema.component, value); // 0| '' is allow if (hasKey && fields.includes(key)) { // time type if (itemIsDateType(key)) { if (Array.isArray(value)) { var arr = []; var _iterator = _createForOfIteratorHelper(value), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var ele = _step.value; arr.push(schema.component === 'TimeRangePicker' ? ele : (0, _date.dateUtil)(ele)); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } formModel[key] = arr; } else { formModel[key] = (0, _date.dateUtil)(value); } } else { formModel[key] = value; } validKeys.push(key); } }); validateFields(validKeys); case 4: case "end": return _context2.stop(); } } }, _callee2); })); } /** * @description: Delete based on field name */ function removeSchemaByFiled(fields) { return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee3() { var schemaList, fieldList, _iterator2, _step2, field; return _regenerator.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: schemaList = (0, _lodashEs.cloneDeep)((0, _vue.unref)(getSchema)); if (fields) { _context3.next = 3; break; } return _context3.abrupt("return"); case 3: fieldList = (0, _shared.isString)(fields) ? [fields] : fields; if ((0, _shared.isString)(fields)) { fieldList = [fields]; } _iterator2 = _createForOfIteratorHelper(fieldList); try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { field = _step2.value; _removeSchemaByFiled(field, schemaList); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } schemaRef.value = schemaList; case 8: case "end": return _context3.stop(); } } }, _callee3); })); } /** * @description: Delete based on field name */ function _removeSchemaByFiled(field, schemaList) { if ((0, _shared.isString)(field)) { var index = schemaList.findIndex(function (schema) { return schema.field === field; }); if (index !== -1) { schemaList.splice(index, 1); } } } /** * @description: Insert after a certain field, if not insert the last */ function appendSchemaByField(schema, prefixField) { var first = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee4() { var schemaList, index, hasInList; return _regenerator.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: schemaList = (0, _lodashEs.cloneDeep)((0, _vue.unref)(getSchema)); index = schemaList.findIndex(function (schema) { return schema.field === prefixField; }); hasInList = schemaList.some(function (item) { return item.field === prefixField || schema.field; }); if (hasInList) { _context4.next = 5; break; } return _context4.abrupt("return"); case 5: if (!(!prefixField || index === -1 || first)) { _context4.next = 9; break; } first ? schemaList.unshift(schema) : schemaList.push(schema); schemaRef.value = schemaList; return _context4.abrupt("return"); case 9: if (index !== -1) { schemaList.splice(index + 1, 0, schema); } schemaRef.value = schemaList; case 11: case "end": return _context4.stop(); } } }, _callee4); })); } function updateSchema(data) { var replace = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee5() { var updateData, hasField, schema, oldSchema; return _regenerator.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: updateData = []; if ((0, _shared.isPlainObject)(data)) { updateData.push(data); } if ((0, _shared.isArray)(data)) { updateData = _toConsumableArray(data); } hasField = updateData.every(function (item) { return Reflect.has(item, 'field') && item.field; }); if (hasField) { _context5.next = 7; break; } (0, _warning.default)('All children of the form Schema array that need to be updated must contain the `field` field'); return _context5.abrupt("return"); case 7: schema = []; oldSchema = (0, _vue.unref)(getSchema); updateData.forEach(function (item) { if (oldSchema.length) { if (replace) { schema = updateData.slice(); } else { oldSchema.forEach(function (val) { if (val.field === item.field) { var newSchema = (0, _shared.deepMerge)(val, item); schema.push(newSchema); } else { schema.push(val); } }); } } else { schema = updateData.slice(); } }); schemaRef.value = (0, _lodashEs.uniqBy)(schema, 'field'); case 11: case "end": return _context5.stop(); } } }, _callee5); })); } function getShow(schema) { var show = schema.show, ifShow = schema.ifShow; var _unref2 = (0, _vue.unref)(getProps), showAdvancedButton = _unref2.showAdvancedButton, mergeDynamicData = _unref2.mergeDynamicData; var itemIsAdvanced = showAdvancedButton ? (0, _shared.isBoolean)(schema.isAdvanced) ? schema.isAdvanced : true : true; var values = _extends(_extends(_extends(_extends({}, mergeDynamicData), (0, _vue.unref)(defaultValueRef)), (0, _vue.unref)(formModel)), handleFormValues((0, _vue.toRaw)((0, _vue.unref)(formModel)))); var getValues = (0, _vue.ref)({ field: schema.field, model: formModel, values: values, schema: schema }); var isShow = true; var isIfShow = true; if ((0, _shared.isBoolean)(show)) { isShow = show; } if ((0, _shared.isBoolean)(ifShow)) { isIfShow = ifShow; } if ((0, _shared.isFunction)(show)) { isShow = show(getValues); } if ((0, _shared.isFunction)(ifShow)) { isIfShow = ifShow(getValues); } isShow = isShow && itemIsAdvanced; return { isShow: isShow, isIfShow: isIfShow }; } function getFieldsValue(filterHidden) { var formEl = (0, _vue.unref)(formElRef); if (!formEl) { return {}; } var myValue = handleFormValues((0, _vue.toRaw)((0, _vue.unref)(formModel))); if (filterHidden) { var myNewValue = {}; getOriginSchema.value.forEach(function (pItem) { var _getShow = getShow(pItem), isParentIfShow = _getShow.isIfShow; if (isParentIfShow && (0, _shared.hasOwn)(myValue, pItem.field)) { myNewValue[pItem.field] = myValue[pItem.field]; } if ((0, _shared.hasOwn)(pItem, 'children')) { pItem.children.forEach(function (cItem) { var _getShow2 = getShow(cItem), isChildIfShow = _getShow2.isIfShow; if (isChildIfShow && isParentIfShow && (0, _shared.hasOwn)(myValue, cItem.field)) { myNewValue[cItem.field] = myValue[cItem.field]; } }); } }); return myNewValue; } return myValue; } function getChildrenFieldsValue(filterHidden) { var formEl = (0, _vue.unref)(formElRef); if (!formEl) { return {}; } var myValue = handleFormValues((0, _vue.toRaw)((0, _vue.unref)(formModel))); var myNewValue = {}; getOriginSchema.value.forEach(function (pItem) { var _getShow3 = getShow(pItem), isParentIfShow = _getShow3.isIfShow; if ((!filterHidden || isParentIfShow) && (0, _shared.hasOwn)(myValue, pItem.field)) { myNewValue[pItem.field] = myValue[pItem.field]; } if ((0, _shared.hasOwn)(pItem, 'children')) { pItem.children.forEach(function (cItem) { var _getShow4 = getShow(cItem), isChildIfShow = _getShow4.isIfShow; if ((!filterHidden || isChildIfShow && isParentIfShow) && (0, _shared.hasOwn)(myValue, cItem.field)) { if (!(0, _shared.hasOwn)(myNewValue, pItem.field)) { myNewValue[pItem.field] = {}; } myNewValue[pItem.field][cItem.field] = myValue[cItem.field]; } }); } }); return myNewValue; } /** * @description: Is it time */ function itemIsDateType(key) { return (0, _vue.unref)(getSchema).some(function (item) { return item.field === key ? _helper.dateItemType.includes(item.component) : false; }); } function validateFields(nameList) { var _a; return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee6() { return _regenerator.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: return _context6.abrupt("return", (_a = (0, _vue.unref)(formElRef)) === null || _a === void 0 ? void 0 : _a.validateFields(nameList)); case 1: case "end": return _context6.stop(); } } }, _callee6); })); } function validate(nameList) { var _a; return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee7() { return _regenerator.default.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: _context7.next = 2; return (_a = (0, _vue.unref)(formElRef)) === null || _a === void 0 ? void 0 : _a.validate(nameList); case 2: return _context7.abrupt("return", handleFormValues((0, _vue.toRaw)((0, _vue.unref)(formModel)))); case 3: case "end": return _context7.stop(); } } }, _callee7); })); } function clearValidate(name) { var _a; return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee8() { return _regenerator.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: _context8.next = 2; return (_a = (0, _vue.unref)(formElRef)) === null || _a === void 0 ? void 0 : _a.clearValidate(name); case 2: case "end": return _context8.stop(); } } }, _callee8); })); } function scrollToField(name, options) { var _a; return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee9() { return _regenerator.default.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: _context9.next = 2; return (_a = (0, _vue.unref)(formElRef)) === null || _a === void 0 ? void 0 : _a.scrollToField(name, options); case 2: case "end": return _context9.stop(); } } }, _callee9); })); } /** * @description: Form submission */ function handleSubmit(e) { var _a; return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee10() { var _unref3, submitFunc, submitOnReset, formEl, values; return _regenerator.default.wrap(function _callee10$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: !(0, _shared.isBoolean)(e) && ((_a = e) === null || _a === void 0 ? void 0 : _a.preventDefault) && e.preventDefault(); _unref3 = (0, _vue.unref)(getProps), submitFunc = _unref3.submitFunc, submitOnReset = _unref3.submitOnReset; if (!(submitFunc && (0, _shared.isFunction)(submitFunc))) { _context10.next = 6; break; } _context10.next = 5; return submitFunc(); case 5: return _context10.abrupt("return"); case 6: formEl = (0, _vue.unref)(formElRef); if (formEl) { _context10.next = 9; break; } return _context10.abrupt("return"); case 9: _context10.prev = 9; _context10.next = 12; return validate(); case 12: values = _context10.sent; emit('submit', values); if (!(0, _shared.isBoolean)(e) && submitOnReset) { resetFields(true, false); } _context10.next = 20; break; case 17: _context10.prev = 17; _context10.t0 = _context10["catch"](9); emit('submit-error', _context10.t0); case 20: case "end": return _context10.stop(); } } }, _callee10, null, [[9, 17]]); })); } return { handleSubmit: handleSubmit, clearValidate: clearValidate, validate: validate, validateFields: validateFields, getFieldsValue: getFieldsValue, getChildrenFieldsValue: getChildrenFieldsValue, updateSchema: updateSchema, appendSchemaByField: appendSchemaByField, removeSchemaByFiled: removeSchemaByFiled, resetFields: resetFields, setFieldsValue: setFieldsValue, scrollToField: scrollToField }; }