UNPKG

@wetspace/pro-components

Version:
224 lines (223 loc) 6.57 kB
import { defineComponent as W, ref as w, shallowRef as E, useSlots as M, computed as z, createVNode as u, Fragment as V, mergeProps as F, createTextVNode as m, isVNode as q } from "vue"; import { useNamespace as G, ElSpace as H, ElButton as h, ElPopconfirm as J } from "element-plus"; import { wetEditTableProps as K } from "./types.mjs"; import { WetSchemaTable as L } from "../../../pro-table/index.mjs"; import Q from "./columns-item.vue.mjs"; import I from "../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/cloneDeep.mjs"; function U(n) { return typeof n == "function" || Object.prototype.toString.call(n) === "[object Object]" && !q(n); } const ae = /* @__PURE__ */ W({ name: "WetEditableTabl", props: { ...K }, emits: ["update:modelValue", "change"], setup(n, { emit: o, expose: P }) { const b = G("editable-table"), d = w([]), k = E(), r = M(), S = (l, e) => { const a = n.modelValue || []; a[e] = l, o("update:modelValue", a), n.controlled || o("change", { row: l, $index: e }); }, B = (l, e) => { o("change", { row: l, $index: e }); }, c = (l = !1) => { const e = n.modelValue || [], a = I(n.addDefaultRow); let t = e.length; n.controlled && d.value.push(a), l ? (e.unshift(a), t = 0) : e.push(a), o("update:modelValue", e), o("change", { row: a, $index: t }); }, R = (l) => { d.value = d.value.filter((e) => e !== l); }, i = (l) => { if (n.modelValue && n.modelValue.length > l) { const e = [...n.modelValue], a = e.splice(l, 1); R(a), o("update:modelValue", e), o("change", { row: null, $index: l }); } }, f = (l, e) => { const a = [...n.modelValue || []], t = I(l); e !== void 0 ? a.splice(e + 1, 0, t) : a.push(t), o("update:modelValue", a), o("change", { row: t, $index: e || a.length }); }, y = (l) => { d.value.includes(l) || d.value.push(l); }, p = E(!1), g = (l) => { p.value || (R(l), p.value = !0, setTimeout(() => { p.value = !1; })); }, v = (l) => R(l), _ = (l, e) => { var t; const a = (t = n.modelValue) == null ? void 0 : t[l]; a && (Object.assign(a, e), o("update:modelValue", n.modelValue), o("change", { row: a, $index: l })); }, j = { placement: "top" }, x = (l, e, a) => { let t; return u(H, { size: l.length === 0 ? 0 : void 0 }, U(t = l.map((s) => s === "add" ? u(h, { link: !0, type: "primary", onClick: () => c() }, { default: () => [m("新增")] }) : s === "delete" ? u(J, F(j, { title: "删除此项?", onConfirm: () => i(a) }), { reference() { return u(h, { link: !0, type: "danger" }, { default: () => [m("删除")] }); } }) : s === "copy" ? u(h, { link: !0, type: "primary", onClick: () => f(e, a) }, { default: () => [m("复制")] }) : s === "edit" ? u(V, null, [d.value.includes(e) ? u(V, null, [u(h, { link: !0, type: "primary", onClick: () => g(e) }, { default: () => [m("保存")] }), u(h, { link: !0, type: "primary", onClick: () => v(e) }, { default: () => [m("取消")] })]) : u(h, { link: !0, type: "primary", onClick: () => y(e) }, { default: () => [m("编辑")] })]) : u(V, null, null))) ? t : { default: () => [t] }); }, N = (l) => n.readonly ? !0 : d.value.includes(l) ? !1 : n.controlled, O = z(() => (n.columns || []).filter((e) => n.readonly ? e.valueType !== "actions" : e).map((e) => ({ ...e, isRender: !1, render({ row: a, $index: t }) { var s, T; if (e.valueType === "actions") return e.isRender ? r.actions && r.actions({ row: a, $index: t, actions: { add: c, deleteRow: i, saveRow: g, copy: f, cancel: v, edit: y, readonly: !d.value.includes(a) } }) : e.render ? e.render({ row: a, $index: t }, { add: c, deleteRow: i, saveRow: g, copy: f, cancel: v, edit: y, readonly: d.value.includes(a) }) : x(e.actions || [], a, t); { const D = Array.isArray(e.dataIndex) ? e.dataIndex.join("") : e.dataIndex; let C = a && e.render; return e.isRender && a && (C = r[D] && r[D]), t >= 0 ? u(Q, { controlCellMethod: n.controlCellMethod, columns: (n.columns || []).map((A) => A.dataIndex || ""), row: a, index: t, prop: e.dataIndex, itemConfig: { ...e, fieldProps: { ...e.fieldProps || {}, disabled: n.disabled || ((s = e.fieldProps) == null ? void 0 : s.disabled), clearable: n.clearable || ((T = e.fieldProps) == null ? void 0 : T.clearable) } }, changeValue: S, emitControlChangeValue: B, readonly: N(a), controlled: n.controlled, saveFlag: p.value }, { default: C ? () => C({ $index: t, row: a }) : null }) : u(V, null, null); } } }))); return P({ scrollToIndex: (l) => { var e; return (e = k.value) == null ? void 0 : e.scrollToIndex(l); }, add: c, deleteRow: i, saveRow: g, copy: f, cancel: v, edit: y, setRowData: _ }), () => u("div", { class: b.b() }, [r.header && u("div", { class: b.e("header") }, [r.header({ addBefore: () => c(!0), add: () => c(), deleteRow: i, copy: f })]), u(L, F({ ...n, modelValue: "", adddefaultrow: "" }, { ref: k, data: n.modelValue || [], columns: O.value }), null), r.footer && u("div", { class: b.e("footer") }, [r.footer({ addBefore: () => c(!0), add: () => c(), deleteRow: i, copy: f })])]); } }); export { ae as default };