UNPKG

birdpaper-ui

Version:

一个通用的 vue3 UI组件库。A common vue3 UI component library.

58 lines (57 loc) 1.75 kB
"use strict"; const vue = require("vue"); const _sfc_main = vue.defineComponent({ name: "Switch", props: { /** 绑定值 Binding value */ modelValue: { type: [Boolean, Number, String], default: false }, /** 是否禁用 Disabled or not */ disabled: { type: Boolean, default: false }, /** 开启时的值 */ checkValue: { type: [Boolean, Number, String], default: true }, /** 关闭时的值 */ uncheckValue: { type: [Boolean, Number, String], default: false }, /** 开启时的文本内容 */ checkText: { type: String, default: "" }, /** 关闭时的文本内容 */ uncheckText: { type: String, default: "" }, /** 触发改变前的回调,返回 false 则中断 */ onBeforeOk: { type: Function, default: () => true } }, emits: ["update:modelValue"], setup(props, { emit }) { const name = "bp-switch"; const cls = vue.computed(() => { let clsName = [name]; if (props.disabled) { clsName.push(`${name}-disabled`); } return clsName; }); const isCheck = vue.computed(() => props.modelValue === props.checkValue); const loading = vue.ref(false); const handleClick = async () => { if (props.disabled || loading.value) return; try { loading.value = true; const res = await props.onBeforeOk(); if (!res) return; emit("update:modelValue", isCheck.value ? props.uncheckValue : props.checkValue); } catch (error) { console.log("[ Switch -onBeforeOk error]", error); } finally { loading.value = false; } }; return { name, cls, isCheck, loading, handleClick }; } }); module.exports = _sfc_main;