2026-06-09 17:49:15 +08:00
|
|
|
<template>
|
2026-06-10 10:15:10 +08:00
|
|
|
<view class="form-checkbox">
|
|
|
|
|
<view v-for="(item, index) in options" :key="index" class="checkbox-item" @click="onToggle(item)">
|
|
|
|
|
<view class="checkbox-dot" :class="{ active: isChecked(item) }"></view>
|
|
|
|
|
<text class="checkbox-label">{{ item }}</text>
|
|
|
|
|
</view>
|
|
|
|
|
</view>
|
2026-06-09 17:49:15 +08:00
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
export default {
|
2026-06-10 10:15:10 +08:00
|
|
|
name: 'FormItemCheckbox',
|
|
|
|
|
props: {
|
|
|
|
|
value: { type: Array, default: function () { return []; } },
|
|
|
|
|
options: { type: Array, default: function () { return []; } },
|
|
|
|
|
maxCount: { type: Number, default: 0 }
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
isChecked: function (val) {
|
|
|
|
|
return this.value && this.value.indexOf(val) !== -1;
|
|
|
|
|
},
|
|
|
|
|
onToggle: function (val) {
|
|
|
|
|
var newValue = (this.value || []).slice();
|
|
|
|
|
var index = newValue.indexOf(val);
|
|
|
|
|
if (index === -1) {
|
|
|
|
|
if (this.maxCount > 0 && newValue.length >= this.maxCount) {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: '最多只能选择' + this.maxCount + '项',
|
|
|
|
|
icon: 'none'
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
newValue.push(val);
|
|
|
|
|
} else {
|
|
|
|
|
newValue.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
this.$emit('input', newValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-09 17:49:15 +08:00
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
.form-checkbox {
|
2026-06-10 10:15:10 +08:00
|
|
|
display: flex;
|
|
|
|
|
flex-wrap: wrap;
|
2026-06-09 17:49:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.checkbox-item {
|
2026-06-10 10:15:10 +08:00
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
margin-right: 40rpx;
|
|
|
|
|
margin-bottom: 16rpx;
|
2026-06-09 17:49:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.checkbox-dot {
|
2026-06-10 10:15:10 +08:00
|
|
|
width: 36rpx;
|
|
|
|
|
height: 36rpx;
|
|
|
|
|
border-radius: 8rpx;
|
|
|
|
|
border: 2rpx solid #cccccc;
|
|
|
|
|
margin-right: 12rpx;
|
|
|
|
|
position: relative;
|
2026-06-09 17:49:15 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-10 10:15:10 +08:00
|
|
|
.checkbox-dot.active {
|
|
|
|
|
border-color: #e8101e;
|
|
|
|
|
background-color: #e8101e;
|
2026-06-09 17:49:15 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-10 10:15:10 +08:00
|
|
|
.checkbox-dot.active::after {
|
|
|
|
|
content: '';
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 50%;
|
|
|
|
|
left: 50%;
|
|
|
|
|
transform: translate(-50%, -60%) rotate(-45deg);
|
|
|
|
|
width: 20rpx;
|
|
|
|
|
height: 12rpx;
|
|
|
|
|
border-left: 4rpx solid #ffffff;
|
|
|
|
|
border-bottom: 4rpx solid #ffffff;
|
2026-06-09 17:49:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.checkbox-label {
|
2026-06-10 10:15:10 +08:00
|
|
|
font-size: 28rpx;
|
|
|
|
|
color: #333333;
|
2026-06-09 17:49:15 +08:00
|
|
|
}
|
|
|
|
|
</style>
|