UNPKG

@opensig/opendesign

Version:

113 lines (112 loc) 4.05 kB
import { defineComponent, useSlots, 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"; import { isEmptySlot } from "../_utils/vue-utils.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: 1, class: "o-switch-icon-wrap" }; const _hoisted_5 = { 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 slots = useSlots(); 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; }); const isCustomIcon = computed(() => { return !isEmptySlot(slots.active) || !isEmptySlot(slots.inactive); }); 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 }, { "o-switch-custom": isCustomIcon.value } ]]), 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.active || _ctx.$slots.inactive) && !props.loading ? (openBlock(), createElementBlock("div", _hoisted_4, [ isChecked.value ? renderSlot(_ctx.$slots, "active", { key: 0 }) : renderSlot(_ctx.$slots, "inactive", { key: 1 }) ])) : createCommentVNode("v-if", true) ]), _ctx.$slots.on || _ctx.$slots.off ? (openBlock(), createElementBlock("div", _hoisted_5, [ 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 };