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)

272 lines (271 loc) 7.96 kB
import { ElMessage as g } from "element-plus"; import { useLocale as E } from "../../locale/lang.mjs"; import { deepClone as m } 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 j } from "../../locale/packages.mjs"; import { FormTypeEnum as v } from "../index.mjs"; const c = {}, 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 l; const i = (l = F()) == null ? void 0 : l.proxy, s = O({ formItems: [], rules: {}, formData: {}, watchCache: [] }), h = $(null), { t: y } = E(); N(() => { setTimeout(() => { var r; const e = (r = h.value) == null ? void 0 : r.$el, t = e == null ? void 0 : e.parentNode; t && e && (t.style.height = `${e.offsetHeight}px`); }, 100); }); const o = (e, t) => { var u, f, T; const r = Object.values(v).includes( t.type // 这里由于是继承了列表的type,但是表单也有type不好去断言,故此直接使用了 any ), n = m({ text: t.text || e.label, prop: ((u = t.formItem) == null ? void 0 : u.prop) || t.prop, render: r ? t.render : void 0, type: r ? t.type : void 0, slotName: t.slotName, ...t.formItem || {}, data: r && !((f = t.formItem) != null && f.data) ? t.data || {} : { ...r ? t.data : {}, ...(T = t.formItem) == null ? void 0 : T.data } }); s.formItems.push(n), p( 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)} ${y(j.VerifyEmpty)}`, trigger: "blur" }), Array.isArray(e.rule) ? t.push({ min: e.rule[0], max: e.rule[1], message: `${y(j.VerifyLength)}${e.rule[0]} ~ ${e.rule[1]}`, trigger: "blur" }) : typeof e.rule == "string" && (b[e.rule] ? t.push({ ...b[e.rule], message: y(b[e.rule].message) }) : console.warn( `${b[e.rule] ? "在 verifyFormItemRule 中存在" : "在 verifyFormItemRule 中不存在"} 支持 ${Object.keys(b).join("、")}` )), s.rules[e.prop] = t); }, p = (e, t, r) => { var n, u; switch (Object.assign(s.formData, a.formDataDefault || {}), s.formData[e.prop] = ((n = a.formDataDefault) == null ? void 0 : n[e.prop]) ?? e.defaultData ?? "", c[e.prop] = e.defaultData ?? "", e.type) { case v.Select: r && !((u = e.data) != null && u.options) && Array.isArray(r.filters) && (e.data.options = r.filters.map((f) => ({ ...f, label: f.value, value: f.key }))), t[e.prop] = t[e.prop] || void 0, c[e.prop] = e.defaultData ?? ""; break; case v.Rate: t[e.prop] = Number.isNaN(Number(t[e.prop])) ? 0 : Number(t[e.prop]); break; case v.Checkbox: t[e.prop] = typeof t[e.prop] == "string" ? t[e.prop].split(",").filter((f) => f.length) : Array.isArray(t[e.prop]) ? t[e.prop] : [], c[e.prop] = e.defaultData || []; break; } }, x = (e) => { s.formItems = [], Object.keys(c).forEach((t) => delete c[t]), e.forEach((t) => { Array.isArray(t.props) ? t.props.filter((r) => r.formItem).forEach((r) => { o(t, r); }) : t.props.formItem && o(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) => { s.formData = m(c), Object.assign(s.formData, e || {}); }, { immediate: !0, deep: !0 } ), A( () => a.formItems, (e) => { e && a.extendTable === !1 && (s.formItems = e, Object.keys(c).forEach((t) => delete c[t]), s.formItems.forEach((t) => { d(t), p(t, s.formData); })); }, { immediate: !0, deep: !0 } ) ); const D = k( () => s.formItems.filter( (e) => (Array.isArray(e.showType) ? e.showType.includes(a.showType || "") : e.showType === a.showType) || !a.showType || !e.showType ) ); return R(() => { s.watchCache.forEach((e) => e()); }), { formViewData: s, formRenderItems: D, elFormRef: h, transformHeader: x }; }, X = (a, i, s, h) => { const { proxy: y } = F(); let o = !0; return { submitForm: (l = {}, e) => (s.value.validate().then(() => { var t, r, n; a.apis && (w(a.apis, (u) => !!Object.keys(u).length) ? a.showType ? (t = a.apis) != null && t[a.showType] ? (r = a.apis) == null || r[a.showType]({ ...l, ...i.value }).then(() => { e && g.success(e.successMessage || "提交成功!"); }).catch(() => { e && g.error(e.errorMessage || "提交失败!"); }) : console.error(`apis.${a.showType} 不存在`) : console.error( "showType 为空,但是 apis 为对象时需要 showType 作为键值。或者 apis 传递一个 Promise" ) : (n = a.apis) == null || n.call(a, { ...l, ...i.value }).then(() => { e && g.success(e.successMessage || "提交成功!"); }).catch(() => { e && g.error(e.errorMessage || "提交失败!"); })); }).catch(() => !1), s.value.validate()), resetForm: () => { i.value = m(c); }, refreshRender: (l = typeof a.extendTable == "boolean" ? "PTable" : a.extendTable, e) => { var n, u; const r = L(l, e || y); (n = r.value) != null && n.props.header && h((u = r.value) == null ? void 0 : u.props.header); }, visibleFormTrigger: () => { var t; const l = (t = s.value) == null ? void 0 : t.$el, e = l == null ? void 0 : l.parentNode; if (e && l) { o = !o; const r = l.offsetHeight; e.style.height = o ? `${r}px` : "0px"; } return o; }, bindAttr(l, e, t) { var r; return { formItem: l, formData: e, index: t, inline: ((r = a.property) == null ? void 0 : r.inline) || !1 }; } }; }, Y = (a) => ({ event: (s, ...h) => { var y, o, d; w( a.formItem.data, (p) => typeof (p == null ? void 0 : p.on) != null ) && ((d = (o = (y = a.formItem.data) == null ? void 0 : y.on) == null ? void 0 : o[s]) == null || d.call(o, { ...a }, ...h)); } }); 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