Files
zgty-mas-m/components/checkbox-switch/checkbox-switch.vue
2026-03-05 16:54:47 +08:00

74 lines
1.5 KiB
Vue
Raw 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.
<!-- components/checkbox-switch.vue -->
<template>
<view class="custom-checkbox" @click="toggle" :class="{ 'custom-checkbox--checked': modelValue }">
<!-- 这里可以用一个图标或者用CSS绘制一个方框 -->
<view class="custom-checkbox__icon">
<!-- 示例使用一个简单的对勾符号 -->
<up-icon v-if="modelValue" name="checkbox-mark" color="#FFF" size="15"></up-icon>
</view>
<text class="custom-checkbox__label" v-if="$slots.default">
<slot></slot>
</text>
</view>
</template>
<script setup>
// 1. 定义并接收 props将其解构赋值
const props = defineProps({
modelValue: {
type: Boolean,
required: true
}
})
// 定义 emits
const emit = defineEmits(['update:modelValue'])
// 2. 在函数中使用 props.modelValue
const toggle = () => {
emit('update:modelValue', !props.modelValue)
}
</script>
<style lang="scss" scoped>
.custom-checkbox {
display: inline-flex;
align-items: center;
user-select: none;
cursor: pointer;
padding: 8rpx;
&__icon {
width: 16px;
height: 16px;
border: 2rpx solid #c8c9cc;
border-radius: 6rpx;
display: flex;
align-items: center;
justify-content: center;
margin-right: 10rpx;
transition: all 0.2s;
}
&__icon-text {
font-size: 24rpx;
color: #ffffff;
}
&__label {
font-size: 28rpx;
color: #606266;
}
// 选中状态的样式
&--checked &__icon {
background-color: #2979ff;
border-color: #2979ff;
}
&--checked &__label {
color: #2979ff;
}
}
</style>