@fe6/water-pro
Version:
An enterprise-class UI design language and Vue-based implementation
628 lines (511 loc) • 20.7 kB
JavaScript
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; }
import _regeneratorRuntime from "@babel/runtime/regenerator";
/** @format */
import { __awaiter } from "tslib";
import { unref, toRaw, ref } from 'vue';
import { cloneDeep, uniqBy } from 'lodash-es';
import { isArray, isBoolean, isFunction, isPlainObject, isString, deepMerge, hasOwn } from '@fe6/shared';
import warning from '../../../_util/warning';
import { dateItemType, handleInputNumberValue } from '../helper';
import { dateUtil } from '../date';
export 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 __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var _unref, resetFunc, resetOnSubmit, formEl;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_unref = unref(getProps), resetFunc = _unref.resetFunc, resetOnSubmit = _unref.resetOnSubmit;
_context.t0 = resetFunc && isFunction(resetFunc);
if (!_context.t0) {
_context.next = 5;
break;
}
_context.next = 5;
return resetFunc();
case 5:
formEl = 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', toRaw(formModel));
}
triggerSubmit && resetOnSubmit && handleSubmit(false);
case 12:
case "end":
return _context.stop();
}
}
}, _callee);
}));
}
/**
* @description: Set form value
*/
function setFieldsValue(values) {
return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
var fields, validKeys;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
fields = unref(getSchema).map(function (item) {
return item.field;
}).filter(Boolean);
validKeys = [];
Object.keys(values).forEach(function (key) {
var schema = unref(getSchema).find(function (item) {
return item.field === key;
});
var value = values[key];
var hasKey = Reflect.has(values, key);
value = 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 : dateUtil(ele));
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
formModel[key] = arr;
} else {
formModel[key] = 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 __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
var schemaList, fieldList, _iterator2, _step2, field;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
schemaList = cloneDeep(unref(getSchema));
if (fields) {
_context3.next = 3;
break;
}
return _context3.abrupt("return");
case 3:
fieldList = isString(fields) ? [fields] : fields;
if (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 (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 __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
var schemaList, index, hasInList;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
schemaList = cloneDeep(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 __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
var updateData, hasField, schema, oldSchema;
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
updateData = [];
if (isPlainObject(data)) {
updateData.push(data);
}
if (isArray(data)) {
updateData = _toConsumableArray(data);
}
hasField = updateData.every(function (item) {
return Reflect.has(item, 'field') && item.field;
});
if (hasField) {
_context5.next = 7;
break;
}
warning('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 = 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 = deepMerge(val, item);
schema.push(newSchema);
} else {
schema.push(val);
}
});
}
} else {
schema = updateData.slice();
}
});
schemaRef.value = uniqBy(schema, 'field');
case 11:
case "end":
return _context5.stop();
}
}
}, _callee5);
}));
}
function getShow(schema) {
var show = schema.show,
ifShow = schema.ifShow;
var _unref2 = unref(getProps),
showAdvancedButton = _unref2.showAdvancedButton,
mergeDynamicData = _unref2.mergeDynamicData;
var itemIsAdvanced = showAdvancedButton ? isBoolean(schema.isAdvanced) ? schema.isAdvanced : true : true;
var values = _extends(_extends(_extends(_extends({}, mergeDynamicData), unref(defaultValueRef)), unref(formModel)), handleFormValues(toRaw(unref(formModel))));
var getValues = ref({
field: schema.field,
model: formModel,
values: values,
schema: schema
});
var isShow = true;
var isIfShow = true;
if (isBoolean(show)) {
isShow = show;
}
if (isBoolean(ifShow)) {
isIfShow = ifShow;
}
if (isFunction(show)) {
isShow = show(getValues);
}
if (isFunction(ifShow)) {
isIfShow = ifShow(getValues);
}
isShow = isShow && itemIsAdvanced;
return {
isShow: isShow,
isIfShow: isIfShow
};
}
function getFieldsValue(filterHidden) {
var formEl = unref(formElRef);
if (!formEl) {
return {};
}
var myValue = handleFormValues(toRaw(unref(formModel)));
if (filterHidden) {
var myNewValue = {};
getOriginSchema.value.forEach(function (pItem) {
var _getShow = getShow(pItem),
isParentIfShow = _getShow.isIfShow;
if (isParentIfShow && hasOwn(myValue, pItem.field)) {
myNewValue[pItem.field] = myValue[pItem.field];
}
if (hasOwn(pItem, 'children')) {
pItem.children.forEach(function (cItem) {
var _getShow2 = getShow(cItem),
isChildIfShow = _getShow2.isIfShow;
if (isChildIfShow && isParentIfShow && hasOwn(myValue, cItem.field)) {
myNewValue[cItem.field] = myValue[cItem.field];
}
});
}
});
return myNewValue;
}
return myValue;
}
function getChildrenFieldsValue(filterHidden) {
var formEl = unref(formElRef);
if (!formEl) {
return {};
}
var myValue = handleFormValues(toRaw(unref(formModel)));
var myNewValue = {};
getOriginSchema.value.forEach(function (pItem) {
var _getShow3 = getShow(pItem),
isParentIfShow = _getShow3.isIfShow;
if ((!filterHidden || isParentIfShow) && hasOwn(myValue, pItem.field)) {
myNewValue[pItem.field] = myValue[pItem.field];
}
if (hasOwn(pItem, 'children')) {
pItem.children.forEach(function (cItem) {
var _getShow4 = getShow(cItem),
isChildIfShow = _getShow4.isIfShow;
if ((!filterHidden || isChildIfShow && isParentIfShow) && hasOwn(myValue, cItem.field)) {
if (!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 unref(getSchema).some(function (item) {
return item.field === key ? dateItemType.includes(item.component) : false;
});
}
function validateFields(nameList) {
var _a;
return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee6() {
return _regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
return _context6.abrupt("return", (_a = 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 __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee7() {
return _regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.next = 2;
return (_a = unref(formElRef)) === null || _a === void 0 ? void 0 : _a.validate(nameList);
case 2:
return _context7.abrupt("return", handleFormValues(toRaw(unref(formModel))));
case 3:
case "end":
return _context7.stop();
}
}
}, _callee7);
}));
}
function clearValidate(name) {
var _a;
return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
return _regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
_context8.next = 2;
return (_a = 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 __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee9() {
return _regeneratorRuntime.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
_context9.next = 2;
return (_a = 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 __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee10() {
var _unref3, submitFunc, submitOnReset, formEl, values;
return _regeneratorRuntime.wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
!isBoolean(e) && ((_a = e) === null || _a === void 0 ? void 0 : _a.preventDefault) && e.preventDefault();
_unref3 = unref(getProps), submitFunc = _unref3.submitFunc, submitOnReset = _unref3.submitOnReset;
if (!(submitFunc && isFunction(submitFunc))) {
_context10.next = 6;
break;
}
_context10.next = 5;
return submitFunc();
case 5:
return _context10.abrupt("return");
case 6:
formEl = 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 (!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
};
}