Files
medical-mall/components/StatusSwitch.uvue
2026-03-09 15:49:16 +08:00

103 lines
1.7 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view class="switch-mock" :class="modelValue ? 'switch-on' : ''" @click="toggle" :style="customStyle">
<view class="switch-dot"></view>
<text class="switch-text">{{ modelValue ? activeText : inactiveText }}</text>
</view>
</template>
<script setup lang="uts">
/**
* StatusSwitch 状态切换组件
* 用于替代原生 switch提供更符合后台管理系统的样式和交互
*/
const props = defineProps({
/**
* 绑定值
*/
modelValue: {
type: Boolean,
default: false
},
/**
* 开启时的文字
*/
activeText: {
type: String,
default: '开启'
},
/**
* 关闭时的文字
*/
inactiveText: {
type: String,
default: '关闭'
},
/**
* 自定义样式
*/
customStyle: {
type: String,
default: ''
}
})
const emit = defineEmits(['update:modelValue', 'change'])
const toggle = () => {
const newValue = !props.modelValue
emit('update:modelValue', newValue)
emit('change', newValue)
}
</script>
<style scoped>
.switch-mock {
width: 54px;
height: 24px;
background-color: #ccc;
border-radius: 12px;
position: relative;
transition: all 0.3s;
display: flex;
flex-direction: row;
align-items: center;
padding: 0 6px;
cursor: pointer;
}
.switch-on {
background-color: #1890ff;
}
.switch-dot {
width: 18px;
height: 18px;
background-color: #fff;
border-radius: 50%;
position: absolute;
left: 3px;
top: 3px;
transition: all 0.3s;
}
.switch-on .switch-dot {
left: 33px;
}
.switch-text {
font-size: 12px;
color: #fff;
margin-left: auto;
line-height: 24px;
display: flex;
flex-direction: row;
align-items: center;
}
.switch-on .switch-text {
margin-left: 0;
margin-right: auto;
}
</style>