comic-plus
Version:
<p align="center"> <img width="200px" src="./logo.png"/> </p>
133 lines (132 loc) • 5.64 kB
JavaScript
import { defineComponent, inject, computed, openBlock, createElementBlock, normalizeClass, normalizeStyle, unref, createBlock, resolveDynamicComponent, Fragment, createTextVNode, toDisplayString, createCommentVNode, createElementVNode, withCtx, warn } from "vue";
import "../style/switch.css";
import { useGlobal } from "../../../utils/config.mjs";
import { isBoolean, isVueComponent, isPromise } from "../../../utils/typescript.mjs";
import "@vueuse/core";
import { useItemValidate } from "../../../hooks/validate.mjs";
import { switchProps, switchEmits } from "./main.props.mjs";
import { FORM_PROVIDE } from "../../form/src/type.mjs";
import "../../../icons/index.mjs";
import { Loading } from "../../../icons/components/components.mjs";
const _hoisted_1 = {
key: 0,
class: "cu-switch__label cu-switch__label--off"
};
const _hoisted_2 = { class: "cu-switch__container" };
const _hoisted_3 = {
key: 0,
class: "cu-switch__text"
};
const _hoisted_4 = { class: "cu-switch__inner" };
const _hoisted_5 = {
key: 1,
class: "cu-switch__label cu-switch__label--on"
};
const _hoisted_6 = ["checked", "disabled"];
const _sfc_main = /* @__PURE__ */ defineComponent({
...{
name: "CuSwitch"
},
__name: "main",
props: switchProps,
emits: switchEmits,
setup(__props, { emit: __emit }) {
const props = __props;
const emit = __emit;
const form = inject(FORM_PROVIDE, void 0);
const { itemValidate } = useItemValidate();
const { globalSize } = useGlobal();
const currentSize = computed(() => {
return props.size ?? (form == null ? void 0 : form.props.size) ?? (globalSize == null ? void 0 : globalSize.value);
});
const isOn = computed(() => {
return isBoolean(props.modelValue) ? props.modelValue : props.modelValue === props.onValue;
});
const status = computed(() => {
return isOn.value ? "is-on" : "is-off";
});
const style = computed(() => {
return {
"--cu-switch-on-color": props.onColor,
"--cu-switch-off-color": props.offColor,
"--cu-switch-on-disabled": props.onColor ? `color-mix(in srgb, ${props.onColor}, white 50%)` : void 0,
"--cu-switch-off-disabled": props.onColor ? `color-mix(in srgb, ${props.offColor}, white 50%)` : void 0
};
});
function getChecked() {
return isBoolean(props.modelValue) ? props.modelValue : props.modelValue === props.onValue;
}
function changeValue() {
var _a;
let checked = !getChecked();
if (props.beforeChange) {
let before = (_a = props.beforeChange) == null ? void 0 : _a.call(props);
const included = [isBoolean(before), isPromise(before)].includes(true);
if (!included) {
throw "beforeChange must be a function that provides a return Boolean value or Promise";
}
if (isPromise(before)) {
before.then((result) => {
if (result) {
updateValue(checked);
}
}).catch((err) => {
err && warn(err);
});
} else if (isBoolean(before)) {
before && updateValue(checked);
}
} else {
updateValue(checked);
}
}
function updateValue(checked) {
let val = isBoolean(props.modelValue) ? checked : checked ? props.onValue : props.offValue;
emit("update:modelValue", val);
emit("change", val);
itemValidate("change");
}
return (_ctx, _cache) => {
return openBlock(), createElementBlock("label", {
class: normalizeClass(["cu-switch", [{ "is-disabled": _ctx.disabled || _ctx.loading, "is-square": _ctx.square }, status.value, currentSize.value]]),
style: normalizeStyle(style.value)
}, [
!_ctx.inlineText && (_ctx.offIcon || _ctx.offText) ? (openBlock(), createElementBlock("span", _hoisted_1, [
unref(isVueComponent)(_ctx.offIcon) ? (openBlock(), createBlock(resolveDynamicComponent(_ctx.offIcon), { key: 0 })) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
createTextVNode(toDisplayString(_ctx.offText), 1)
], 64))
])) : createCommentVNode("", true),
createElementVNode("div", _hoisted_2, [
_ctx.inlineText ? (openBlock(), createElementBlock("span", _hoisted_3, [
(openBlock(), createBlock(resolveDynamicComponent(isOn.value ? unref(isVueComponent)(_ctx.onIcon) ? _ctx.onIcon : "span" : unref(isVueComponent)(_ctx.offIcon) ? _ctx.offIcon : "span"), null, {
default: withCtx(() => [
createTextVNode(toDisplayString(isOn.value ? _ctx.onText : _ctx.offText), 1)
]),
_: 1
}))
])) : createCommentVNode("", true),
createElementVNode("span", _hoisted_4, [
_ctx.loading ? (openBlock(), createBlock(unref(Loading), {
key: 0,
class: "is-loading"
})) : createCommentVNode("", true)
])
]),
!_ctx.inlineText && (_ctx.onIcon || _ctx.onText) ? (openBlock(), createElementBlock("span", _hoisted_5, [
unref(isVueComponent)(_ctx.onIcon) ? (openBlock(), createBlock(resolveDynamicComponent(_ctx.onIcon), { key: 0 })) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
createTextVNode(toDisplayString(_ctx.onText), 1)
], 64))
])) : createCommentVNode("", true),
createElementVNode("input", {
type: "checkbox",
checked: isOn.value,
disabled: _ctx.disabled,
onChange: changeValue
}, null, 40, _hoisted_6)
], 6);
};
}
});
export {
_sfc_main as default
};