UNPKG

@opensig/opendesign

Version:

71 lines (70 loc) 2.33 kB
import { defineComponent, inject, ref, watch, createElementBlock, openBlock, normalizeStyle, normalizeClass, unref, createCommentVNode, renderSlot, createBlock, resolveDynamicComponent, nextTick } from "vue"; import { buttonToggleProps } from "./types.mjs"; import { getRoundClass } from "../_utils/style-class.mjs"; import { isUndefined } from "../_utils/is.mjs"; import { checkboxInjectKey } from "../checkbox/provide.mjs"; import { radioInjectKey } from "../radio/provide.mjs"; const _hoisted_1 = { key: 0, class: "o-toggle-prefix" }; const _sfc_main = /* @__PURE__ */ defineComponent({ __name: "OToggle", props: buttonToggleProps, emits: ["update:checked", "change"], setup(__props, { emit: __emit }) { const checkboxInjection = inject(checkboxInjectKey, null); const radioInjection = inject(radioInjectKey, null); const props = __props; const round = getRoundClass(props, "toggle"); const isChecked = ref(props.checked ?? props.defaultChecked); const emits = __emit; watch( () => props.checked, (val) => { if (!isUndefined(val)) { isChecked.value = val; } } ); const onClick = (ev) => { if (props.disabled || checkboxInjection || radioInjection) { return; } isChecked.value = !isChecked.value; emits("update:checked", isChecked.value); nextTick(() => { emits("change", isChecked.value, ev); }); }; return (_ctx, _cache) => { return openBlock(), createElementBlock( "div", { class: normalizeClass(["o-toggle", [ unref(round).class.value, { "o-toggle-disabled": props.disabled, "o-toggle-checked": isChecked.value } ]]), style: normalizeStyle(unref(round).style.value), onClick }, [ props.icon || _ctx.$slots.icon ? (openBlock(), createElementBlock("span", _hoisted_1, [ renderSlot(_ctx.$slots, "icon", {}, () => [ (openBlock(), createBlock(resolveDynamicComponent(props.icon))) ]) ])) : createCommentVNode("v-if", true), renderSlot(_ctx.$slots, "default") ], 6 /* CLASS, STYLE */ ); }; } }); export { _sfc_main as default };