@opensig/opendesign
Version:
<p align="center"><img src="../docs/public/opendesign-logo-light.png"/></p> <h1 align="center">opendesign</h1> <p align="center">一个 Vue 3 组件库</p> <p align="center"><b>皮肤可定制,使用 TypeScript</b></p>
100 lines (99 loc) • 3.4 kB
JavaScript
import { defineComponent, ref, computed, watch, createElementBlock, openBlock, normalizeStyle, normalizeClass, unref, createElementVNode, createCommentVNode, createVNode, renderSlot } from "vue";
import { switchProps } from "./types.mjs";
import { getRoundClass } from "../_utils/style-class.mjs";
import { IconLoading } from "../_utils/icons.mjs";
import { isUndefined, isPromise, isBoolean } from "../_utils/is.mjs";
import { log } from "../_utils/log.mjs";
const _hoisted_1 = { class: "o-switch-wrap" };
const _hoisted_2 = { class: "o-switch-handler" };
const _hoisted_3 = {
key: 0,
class: "o-switch-icon-loading o-rotating"
};
const _hoisted_4 = {
key: 0,
class: "o-switch-label"
};
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "OSwitch",
props: switchProps,
emits: ["update:modelValue", "change"],
setup(__props, { emit: __emit }) {
const props = __props;
const emits = __emit;
const round = getRoundClass(props, "switch");
const _checked = ref(props.defaultChecked);
const isChecked = computed(() => {
if (!isUndefined(props.modelValue)) {
return props.checkedValue === props.modelValue;
}
return _checked.value;
});
watch(
isChecked,
(val) => {
_checked.value = val;
},
{ immediate: true }
);
const isChangeable = () => {
if (props.loading || props.disabled) {
return Promise.resolve(false);
}
if (!props.beforeChange) {
return Promise.resolve(true);
}
const res = props.beforeChange(!isChecked.value);
if (!(isPromise(res) || isBoolean(res))) {
return Promise.reject("beforeChange should return type `Promise<boolean>` or `boolean`");
}
return isBoolean(res) ? Promise.resolve(res) : res;
};
const onClick = (ev) => {
isChangeable().then((flag) => {
if (flag) {
const checked = !isChecked.value;
_checked.value = checked;
const val = checked ? props.checkedValue : props.uncheckedValue;
emits("update:modelValue", val);
emits("change", val, ev);
}
}).catch((err) => {
log.warn(`${err}`);
});
};
return (_ctx, _cache) => {
return openBlock(), createElementBlock(
"div",
{
class: normalizeClass(["o-switch", [
`o-switch-${props.size}`,
unref(round).class.value,
{ "o-switch-checked": isChecked.value },
{ "o-switch-disabled": props.disabled },
{ "o-switch-loading": props.loading }
]]),
style: normalizeStyle(unref(round).style.value),
onClick
},
[
createElementVNode("div", _hoisted_1, [
createElementVNode("div", _hoisted_2, [
props.loading ? (openBlock(), createElementBlock("span", _hoisted_3, [
createVNode(unref(IconLoading))
])) : createCommentVNode("v-if", true)
]),
_ctx.$slots.on || _ctx.$slots.off ? (openBlock(), createElementBlock("div", _hoisted_4, [
isChecked.value ? renderSlot(_ctx.$slots, "on", { key: 0 }) : renderSlot(_ctx.$slots, "off", { key: 1 })
])) : createCommentVNode("v-if", true)
])
],
6
/* CLASS, STYLE */
);
};
}
});
export {
_sfc_main as default
};