UNPKG

@ctsy/layui-vue

Version:

a component library for Vue 3 base on layui-vue

127 lines (126 loc) 4.33 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); import { defineComponent, onMounted, provide, reactive, toRefs, openBlock, createElementBlock, renderSlot } from "vue"; const __default__ = { name: "LayForm" }; const _sfc_main = /* @__PURE__ */ defineComponent(__spreadProps(__spreadValues({}, __default__), { props: { model: { default: function() { return {}; } }, required: { type: Boolean }, rules: null, initValidate: { type: Boolean, default: false }, requiredIcons: { default: "" }, requiredErrorMessage: null, validateMessage: null, useCN: { type: Boolean, default: true } }, emits: ["submit"], setup(__props, { expose, emit }) { const props = __props; const formItems = []; const formItemMap = {}; onMounted(() => { var _a; props.initValidate && ((_a = validate()) == null ? void 0 : _a.catch((err) => { })); }); const submit = function() { let _isValidate = false; validate((isValidate, model, errors) => { _isValidate = isValidate; emit("submit", isValidate, model, errors); }); return _isValidate; }; const validate = function(fields, callback) { let validateItems = formItems; if (typeof fields === "function") { callback = fields; } else if (typeof fields === "string" || Array.isArray(fields) && fields.length > 0) { validateItems = []; const validateFields = !fields ? [] : [].concat(fields); validateFields.forEach((field) => formItemMap[field] && validateItems.push(formItemMap[field])); } let errorsArrs = []; validateItems.forEach((filed) => { filed.validate((errors, _fields) => { errorsArrs = errorsArrs.concat(errors); }); }); const isValidate = errorsArrs.length === 0; if (typeof callback === "function") { isValidate ? callback(true, props.model, null) : callback(false, props.model, errorsArrs); return null; } return new Promise((resolve, reject) => { const callbackParams = { isValidate, model: props.model, errors: isValidate ? null : errorsArrs }; callbackParams.isValidate ? resolve(callbackParams) : reject(callbackParams); }); }; const clearValidate = function(fields) { const clearFields = !fields ? [] : [].concat(fields); if (clearFields.length === 0) { formItems.forEach((filed) => filed.clearValidate()); } else { clearFields.forEach((field) => formItemMap[field] && formItemMap[field].clearValidate()); } }; const reset = function() { for (const key in props.model) { props.model[key] = null; } setTimeout(() => { var _a; return (_a = validate()) == null ? void 0 : _a.catch((err) => { }); }, 0); }; const addField = function(item) { formItems.push(item); formItemMap[item.prop] = item; }; expose({ validate, clearValidate, reset }); provide("LayForm", reactive(__spreadValues({ formItems, addField, clearValidate, validate }, toRefs(props)))); return (_ctx, _cache) => { return openBlock(), createElementBlock("form", { class: "layui-form", onsubmit: submit }, [ renderSlot(_ctx.$slots, "default") ]); }; } })); _sfc_main.install = (app) => { app.component(_sfc_main.name, _sfc_main); }; export { _sfc_main as default };