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