UNPKG

@fe6/water-pro

Version:

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

371 lines (325 loc) 11.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useForm = useForm; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _tslib = require("tslib"); var _vue = require("vue"); var _env = require("../../../_util/env"); var _warning = _interopRequireDefault(require("../../../_util/warning")); var _use = require("../../../_util/use"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** @format */ function useForm(props) { var _this = this; var formRef = (0, _vue.ref)(null); var loadedRef = (0, _vue.ref)(false); function getForm() { return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee() { var form; return _regenerator.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: form = (0, _vue.unref)(formRef); if (!form) { (0, _warning.default)('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 (0, _vue.nextTick)(); case 4: return _context.abrupt("return", form); case 5: case "end": return _context.stop(); } } }, _callee); })); } function register(instance) { (0, _env.isProdMode)() && (0, _vue.onUnmounted)(function () { formRef.value = null; loadedRef.value = null; }); if ((0, _vue.unref)(loadedRef) && (0, _env.isProdMode)() && instance === (0, _vue.unref)(formRef)) { return; } formRef.value = instance; loadedRef.value = true; (0, _vue.watch)(function () { return props; }, function () { props && instance.setProps((0, _use.getDynamicProps)(props)); }, { immediate: true, deep: true }); } var methods = { scrollToField: function scrollToField(name, options) { return (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee2() { var form; return _regenerator.default.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 (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee3() { var form; return _regenerator.default.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 (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee4() { var form; return _regenerator.default.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 (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee5() { var form; return _regenerator.default.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 (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee6() { var form; return _regenerator.default.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 (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee8() { var _this2 = this; return _regenerator.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: getForm().then(function (form) { return (0, _tslib.__awaiter)(_this2, 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 form.resetFields(emitReset); case 2: case "end": return _context7.stop(); } } }, _callee7); })); }); case 1: case "end": return _context8.stop(); } } }, _callee8); })); }, removeSchemaByFiled: function removeSchemaByFiled(field) { return (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee9() { var form; return _regenerator.default.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 = (0, _vue.unref)(formRef)) === null || _a === void 0 ? void 0 : _a.getFieldsValue(filterHidden); }, // children 结构 getChildrenFieldsValue: function getChildrenFieldsValue(filterHidden) { var _a; return (_a = (0, _vue.unref)(formRef)) === null || _a === void 0 ? void 0 : _a.getChildrenFieldsValue(filterHidden); }, setFieldsValue: function setFieldsValue(values) { return (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee10() { var form; return _regenerator.default.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 (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee11() { var form; return _regenerator.default.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 (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee12() { var form; return _regenerator.default.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 (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee13() { var form; return _regenerator.default.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 (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee14() { var form; return _regenerator.default.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]; }