@aplus-frontend/ui
Version:
126 lines (125 loc) • 3.43 kB
JavaScript
import { defineComponent as z, useSlots as D, ref as T, unref as o, computed as g, createVNode as u, Fragment as s, createBlock as m, createElementBlock as E, openBlock as f, resolveDynamicComponent as K, mergeProps as k, createSlots as v, renderList as V, withCtx as p, renderSlot as B, normalizeProps as b, guardReactiveProps as w } from "vue";
import { Switch as A, Popconfirm as L } from "@aplus-frontend/antdv";
import "../../hooks/index.mjs";
import { isNil as M, isFunction as R, omit as P } from "lodash-unified";
import { isDef as d } from "@fruits-chain/utils";
import { useControllableValue as j } from "../../hooks/useControllableValue.mjs";
const O = /* @__PURE__ */ z({
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 = D(), e = S, F = _, {
value: t,
updateValue: r
} = j(e, F, {
valuePropName: "checked"
}), c = T(!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 $ = g(() => {
const n = o(t);
return M(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}`]);
}), U = g(() => d(e.confirm) ? R(e.confirm) ? e.confirm(o(t)) : e.confirm : {});
return (n, a) => n.mode === "read" ? (f(), m(K($.value), {
key: 0
})) : (f(), E(s, {
key: 1
}, [!o(d)(n.confirm) || n.disabled ? (f(), m(o(A), k({
key: 0
}, o(P)(e, "onUpdate:checked"), {
checked: o(t),
loading: n.loading ?? c.value,
onClick: C
}), v({
_: 2
}, [V(i, (y, l) => ({
name: l,
fn: p((h) => [B(n.$slots, l, b(w(h || {})))])
}))]), 1040, ["checked", "loading"])) : (f(), m(o(L), k({
key: 1,
disabled: c.value
}, U.value, {
onConfirm: N
}), {
default: p(() => [u(o(A), k(o(P)(e, "onUpdate:checked"), {
checked: o(t),
loading: n.loading ?? c.value,
onClick: C
}), v({
_: 2
}, [V(i, (y, l) => ({
name: l,
fn: p((h) => [B(n.$slots, l, b(w(h || {})))])
}))]), 1040, ["checked", "loading"])]),
_: 3
}, 16, ["disabled"]))], 64));
}
});
export {
O as default
};