UNPKG

el-plus-powerful-form

Version:

## [DOCS](https://peng-xiao-shuai.github.io/vite-vue-admin-docs/zh-CN/component_directive/component/powerful-form-doc.html)

273 lines (272 loc) 8.19 kB
import { ElMessage as v } from "element-plus"; import { useLocale as E } from "../../locale/lang.mjs"; import { deepClone as j } from "../../index.mjs"; import { getCurrentInstance as F, reactive as O, ref as $, onMounted as N, watch as A, computed as k, onBeforeUnmount as R } from "vue"; import { useAsyncGetRefs as C, useGetRefs as L } from "../../hooks/useGetRefs.mjs"; import { isTypeProtect as w, clearSymbol as M, verifyFormItemRule as b } from "../../utils/index.mjs"; import { LangKey as D } from "../../locale/packages.mjs"; import { FormTypeEnum as g } from "../index.mjs"; const f = {}, z = { // 表单配置数据 formItems: { type: Array, default: () => [] }, extendTable: { type: [Boolean, String], default: !1 }, formDataDefault: { type: Object, default: () => ({}) }, disabled: { type: Boolean, default: !1 }, showType: { type: String, default: void 0 }, property: { type: Object, default: () => ({}) }, apis: { type: [Object, Function], default: void 0 }, showLabel: { type: Boolean, default: !0 } }, J = () => ({ // 按钮组件配置数据 formItem: { type: Object, default: () => ({}) }, formData: { type: Object, default: () => ({}) }, index: { type: Number }, inline: { type: Boolean, default: !1 } }), Q = (a, i) => w( i, (s) => typeof s != "function" ) ? i : i(a), W = (a) => { var u; const i = (u = F()) == null ? void 0 : u.proxy, s = O({ formItems: [], rules: {}, formData: {}, watchCache: [] }), p = $(null), { t: o } = E(); N(() => { a.showType === "query" && setTimeout(() => { var r; const e = (r = p.value) == null ? void 0 : r.$el, t = e == null ? void 0 : e.parentNode; t && e && (t.style.height = `${e.offsetHeight}px`); }, 100); }); const c = (e, t) => { var l, y, m; const r = Object.values(g).includes( t.type // 这里由于是继承了列表的type,但是表单也有type不好去断言,故此直接使用了 any ), n = j({ text: t.text || e.label, prop: ((l = t.formItem) == null ? void 0 : l.prop) || t.prop, render: r ? t.render : void 0, type: r ? t.type : void 0, slotName: t.slotName, ...t.formItem || {}, data: r && !((y = t.formItem) != null && y.data) ? t.data || {} : { ...r ? t.data : {}, ...(m = t.formItem) == null ? void 0 : m.data } }); s.formItems.push(n), h( n, s.formData, t ), d(n); }, d = (e) => { if (e.rule && w( e.rule, (r) => Array.isArray(r) && typeof r[0] == "object" && !Array.isArray(r[0]) )) { s.rules[e.prop] = e.rule; return; } const t = []; e.rule && (t.push({ required: !0, message: `${M(e.text)} ${o(D.VerifyEmpty)}`, trigger: "blur" }), Array.isArray(e.rule) ? t.push({ min: e.rule[0], max: e.rule[1], message: `${o(D.VerifyLength)}${e.rule[0]} ~ ${e.rule[1]}`, trigger: "blur" }) : typeof e.rule == "string" && (b[e.rule] ? t.push({ ...b[e.rule], message: o(b[e.rule].message) }) : console.warn( `${b[e.rule] ? "在 verifyFormItemRule 中存在" : "在 verifyFormItemRule 中不存在"} 支持 ${Object.keys(b).join("、")}` )), s.rules[e.prop] = t); }, h = (e, t, r) => { var n, l; switch (Object.assign(s.formData, a.formDataDefault || {}), s.formData[e.prop] = ((n = a.formDataDefault) == null ? void 0 : n[e.prop]) ?? e.defaultData ?? "", f[e.prop] = e.defaultData ?? "", e.type) { case g.Select: case g.SelectV2: r && !((l = e.data) != null && l.options) && Array.isArray(r.filters) && (e.data.options = r.filters.map((y) => ({ ...y, label: y.value, value: y.key }))), t[e.prop] = t[e.prop] || void 0, f[e.prop] = e.defaultData ?? ""; break; case g.Rate: t[e.prop] = Number.isNaN(Number(t[e.prop])) ? 0 : Number(t[e.prop]); break; case g.Checkbox: t[e.prop] = typeof t[e.prop] == "string" ? t[e.prop].split(",").filter((y) => y.length) : Array.isArray(t[e.prop]) ? t[e.prop] : [], f[e.prop] = e.defaultData || []; break; } }, x = (e) => { s.formItems = [], Object.keys(f).forEach((t) => delete f[t]), e.forEach((t) => { Array.isArray(t.props) ? t.props.filter((r) => r.formItem).forEach((r) => { c(t, r); }) : t.props.formItem && c(t, t.props); }); }; s.watchCache.push( /** * 监听 extendTable 数据变化 */ A( () => a.extendTable, (e) => { if (e) { let t = []; C( typeof e == "boolean" ? "PTable" : e, i ).then((r) => { t = r == null ? void 0 : r.props.header, x(t); }); } }, { immediate: !0 } ), A( () => a.formDataDefault, (e) => { Object.assign(s.formData, e || {}); }, { immediate: !0, deep: !0 } ), A( () => a.formItems, (e) => { e && a.extendTable === !1 && (s.formItems = e, Object.keys(f).forEach((t) => delete f[t]), s.formItems.forEach((t) => { d(t), h(t, s.formData); })); }, { immediate: !0, deep: !0 } ) ); const T = k( () => s.formItems.filter( (e) => typeof e.visible > "u" ? (Array.isArray(e.showType) ? e.showType.includes(a.showType || "") : e.showType === a.showType) || !a.showType || !e.showType : e.visible(s.formData, a.showType) ) ); return R(() => { s.watchCache.forEach((e) => e()); }), { formViewData: s, formRenderItems: T, elFormRef: p, transformHeader: x }; }, X = (a, i, s, p) => { const { proxy: o } = F(); let c = !0; return { submitForm: (u = {}, e) => (s.value.validate().then(() => { var t, r, n; a.apis && (w(a.apis, (l) => !!Object.keys(l).length) ? a.showType ? (t = a.apis) != null && t[a.showType] ? (r = a.apis) == null || r[a.showType]({ ...u, ...i.value }).then((l) => { e && v.success(e.successMessage || "提交成功!"), e == null || e.cb("success", l); }).catch((l) => { e && v.error(e.errorMessage || "提交失败!"), e == null || e.cb("error", l); }) : console.error(`apis.${a.showType} 不存在`) : console.error( "showType 为空,但是 apis 为对象时需要 showType 作为键值。或者 apis 传递一个 Promise" ) : (n = a.apis) == null || n.call(a, { ...u, ...i.value }).then((l) => { e && v.success(e.successMessage || "提交成功!"), e == null || e.cb("success", l); }).catch((l) => { e && v.error(e.errorMessage || "提交失败!"), e == null || e.cb("error", l); })); }).catch(() => !1), s.value.validate()), resetForm: () => { i.value = j(f); }, refreshRender: (u = typeof a.extendTable == "boolean" ? "PTable" : a.extendTable, e) => { var n, l; const r = L(u, e || o); (n = r.value) != null && n.props.header && p((l = r.value) == null ? void 0 : l.props.header); }, visibleFormTrigger: () => { var t; const u = (t = s.value) == null ? void 0 : t.$el, e = u == null ? void 0 : u.parentNode; if (e && u) { c = !c; const r = u.offsetHeight; e.style.height = c ? `${r}px` : "0px"; } return c; }, bindAttr(u, e, t) { var r; return { formItem: u, formData: e, index: t, inline: ((r = a.property) == null ? void 0 : r.inline) || !1 }; } }; }, Y = (a) => ({ event: (s, ...p) => { var o, c, d; w( a.formItem.data, (h) => typeof (h == null ? void 0 : h.on) != null ) && ((d = (c = (o = a.formItem.data) == null ? void 0 : o.on) == null ? void 0 : c[s]) == null || d.call(c, { ...a }, ...p)); } }); export { J as componentProps, z as formViewProps, Q as isProperty, Y as useEvent, W as useFormViewState, X as useFunction }; //# sourceMappingURL=form-view-data.mjs.map