抽离公共状态组件

This commit is contained in:
2026-03-09 15:49:16 +08:00
parent 7e71126774
commit a1ab7f4faa
5 changed files with 148 additions and 130 deletions

View File

@@ -0,0 +1,102 @@
<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>