@fe6/water-pro
Version:
An enterprise-class UI design language and Vue-based implementation
357 lines (319 loc) • 11.2 kB
JavaScript
import _regeneratorRuntime from "@babel/runtime/regenerator";
/** @format */
import { __awaiter } from "tslib";
import { ref, onUnmounted, unref, nextTick, watch } from 'vue';
import { isProdMode } from '../../../_util/env';
import warning from '../../../_util/warning';
import { getDynamicProps } from '../../../_util/use';
export function useForm(props) {
var _this = this;
var formRef = ref(null);
var loadedRef = ref(false);
function getForm() {
return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var form;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
form = unref(formRef);
if (!form) {
warning('The form instance has not been obtained, please make sure that the form has been rendered when performing the form operation!');
}
_context.next = 4;
return nextTick();
case 4:
return _context.abrupt("return", form);
case 5:
case "end":
return _context.stop();
}
}
}, _callee);
}));
}
function register(instance) {
isProdMode() && onUnmounted(function () {
formRef.value = null;
loadedRef.value = null;
});
if (unref(loadedRef) && isProdMode() && instance === unref(formRef)) {
return;
}
formRef.value = instance;
loadedRef.value = true;
watch(function () {
return props;
}, function () {
props && instance.setProps(getDynamicProps(props));
}, {
immediate: true,
deep: true
});
}
var methods = {
scrollToField: function scrollToField(name, options) {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
var form;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return getForm();
case 2:
form = _context2.sent;
form.scrollToField(name, options);
case 4:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
},
setProps: function setProps(formProps) {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
var form;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return getForm();
case 2:
form = _context3.sent;
form.setProps(formProps);
case 4:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
},
resetAllModel: function resetAllModel() {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
var form;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return getForm();
case 2:
form = _context4.sent;
form.resetAllModel();
case 4:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));
},
updateSchema: 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 form;
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return getForm();
case 2:
form = _context5.sent;
form.updateSchema(data, replace);
case 4:
case "end":
return _context5.stop();
}
}
}, _callee5);
}));
},
clearValidate: function clearValidate(name) {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee6() {
var form;
return _regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return getForm();
case 2:
form = _context6.sent;
form.clearValidate(name);
case 4:
case "end":
return _context6.stop();
}
}
}, _callee6);
}));
},
resetFields: function resetFields(emitReset) {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
var _this2 = this;
return _regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
getForm().then(function (form) {
return __awaiter(_this2, 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 form.resetFields(emitReset);
case 2:
case "end":
return _context7.stop();
}
}
}, _callee7);
}));
});
case 1:
case "end":
return _context8.stop();
}
}
}, _callee8);
}));
},
removeSchemaByFiled: function removeSchemaByFiled(field) {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee9() {
var form;
return _regeneratorRuntime.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
_context9.next = 2;
return getForm();
case 2:
form = _context9.sent;
form.removeSchemaByFiled(field);
case 4:
case "end":
return _context9.stop();
}
}
}, _callee9);
}));
},
// TODO promisify
getFieldsValue: function getFieldsValue(filterHidden) {
var _a;
return (_a = unref(formRef)) === null || _a === void 0 ? void 0 : _a.getFieldsValue(filterHidden);
},
// children 结构
getChildrenFieldsValue: function getChildrenFieldsValue(filterHidden) {
var _a;
return (_a = unref(formRef)) === null || _a === void 0 ? void 0 : _a.getChildrenFieldsValue(filterHidden);
},
setFieldsValue: function setFieldsValue(values) {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee10() {
var form;
return _regeneratorRuntime.wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
_context10.next = 2;
return getForm();
case 2:
form = _context10.sent;
form.setFieldsValue(values);
case 4:
case "end":
return _context10.stop();
}
}
}, _callee10);
}));
},
appendSchemaByField: function appendSchemaByField(schema, prefixField, first) {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee11() {
var form;
return _regeneratorRuntime.wrap(function _callee11$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
_context11.next = 2;
return getForm();
case 2:
form = _context11.sent;
form.appendSchemaByField(schema, prefixField, first);
case 4:
case "end":
return _context11.stop();
}
}
}, _callee11);
}));
},
submit: function submit() {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee12() {
var form;
return _regeneratorRuntime.wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
_context12.next = 2;
return getForm();
case 2:
form = _context12.sent;
return _context12.abrupt("return", form.submit());
case 4:
case "end":
return _context12.stop();
}
}
}, _callee12);
}));
},
validate: function validate(nameList) {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee13() {
var form;
return _regeneratorRuntime.wrap(function _callee13$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
_context13.next = 2;
return getForm();
case 2:
form = _context13.sent;
return _context13.abrupt("return", form.validate(nameList));
case 4:
case "end":
return _context13.stop();
}
}
}, _callee13);
}));
},
validateFields: function validateFields(nameList) {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee14() {
var form;
return _regeneratorRuntime.wrap(function _callee14$(_context14) {
while (1) {
switch (_context14.prev = _context14.next) {
case 0:
_context14.next = 2;
return getForm();
case 2:
form = _context14.sent;
return _context14.abrupt("return", form.validateFields(nameList));
case 4:
case "end":
return _context14.stop();
}
}
}, _callee14);
}));
}
};
return [register, methods];
}