@aplus-frontend/ui
Version:
127 lines (126 loc) • 3.49 kB
JavaScript
import { defineComponent as U, useSlots as x, ref as z, unref as o, computed as g, createVNode as u, Fragment as s, createBlock as h, createElementBlock as D, openBlock as f, resolveDynamicComponent as E, mergeProps as p, createSlots as v, renderList as V, withCtx as k, renderSlot as B, normalizeProps as b, guardReactiveProps as w } from "vue";
import { Switch as A, Popconfirm as K } from "@aplus-frontend/antdv";
import { isNil as L, isFunction as M, omit as P } from "lodash-unified";
import "../../utils/index.mjs";
import "../../hooks/index.mjs";
import { useControllableValue as R } from "../../hooks/useControllableValue.mjs";
import { isDef as d } from "../../utils/is.mjs";
const Q = /* @__PURE__ */ U({
name: "ApFieldSwitch",
__name: "index",
props: {
mode: {},
class: {},
style: {},
id: {},
prefixCls: {},
size: {
default: "default"
},
disabled: {
type: Boolean,
default: void 0
},
checkedChildren: {},
unCheckedChildren: {},
tabindex: {},
autofocus: {
type: Boolean
},
loading: {
type: Boolean,
default: void 0
},
checked: {},
checkedValue: {
default: !0
},
unCheckedValue: {
default: !1
},
onChange: {},
onKeydown: {},
onMouseup: {},
"onUpdate:checked": {},
onBlur: {},
onFocus: {},
emptyText: {
default: "--"
},
onAction: {},
confirm: {}
},
emits: ["update:checked"],
setup(S, {
emit: _
}) {
const i = x(), e = S, F = _, {
value: t,
updateValue: r
} = R(e, F, {
valuePropName: "checked"
}), c = z(!1);
async function C(n, a) {
if (!d(e.confirm)) {
if (!d(e.onAction)) {
r(n);
return;
}
c.value = !0;
try {
await e.onAction(n, a) && r(n);
} finally {
c.value = !1;
}
}
}
async function N() {
const n = o(t) === e.checkedValue ? e.unCheckedValue : e.checkedValue;
if (!d(e.onAction)) {
r(n);
return;
}
await e.onAction(n) && r(n);
}
const T = g(() => {
const n = o(t);
return L(n) ? u(s, null, [e.emptyText]) : n === e.checkedValue ? u(s, null, [e.checkedChildren || i.checkedChildren?.() || `${e.checkedValue}`]) : u(s, null, [e.unCheckedChildren || i.unCheckedChildren?.() || `${e.unCheckedValue}`]);
}), $ = g(() => d(e.confirm) ? M(e.confirm) ? e.confirm(o(t)) : e.confirm : {});
return (n, a) => n.mode === "read" ? (f(), h(E(T.value), {
key: 0
})) : (f(), D(s, {
key: 1
}, [!o(d)(n.confirm) || n.disabled ? (f(), h(o(A), p({
key: 0
}, o(P)(e, ["onUpdate:checked", "emptyText"]), {
checked: o(t),
loading: n.loading ?? c.value,
onClick: C
}), v({
_: 2
}, [V(i, (y, l) => ({
name: l,
fn: k((m) => [B(n.$slots, l, b(w(m || {})))])
}))]), 1040, ["checked", "loading"])) : (f(), h(o(K), p({
key: 1,
disabled: c.value
}, $.value, {
onConfirm: N
}), {
default: k(() => [u(o(A), p(o(P)(e, ["onUpdate:checked", "emptyText"]), {
checked: o(t),
loading: n.loading ?? c.value,
onClick: C
}), v({
_: 2
}, [V(i, (y, l) => ({
name: l,
fn: k((m) => [B(n.$slots, l, b(w(m || {})))])
}))]), 1040, ["checked", "loading"])]),
_: 3
}, 16, ["disabled"]))], 64));
}
});
export {
Q as default
};