mrr.sj.front/components/dynamic-form/FormItemCheckbox.vue

112 lines
2.8 KiB
Vue
Raw Normal View History

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>
2026-06-23 09:55:51 +08:00
<text class="checkbox-label">{{ getOptionLabel(item) }}</text>
2026-06-10 10:15:10 +08:00
</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: {
2026-06-23 09:55:51 +08:00
getOptionValue: function (item) {
if (item && typeof item === 'object') {
if (item.value !== undefined) return item.value;
if (item.id !== undefined) return item.id;
if (item.key !== undefined) return item.key;
if (item.code !== undefined) return item.code;
return item.label;
}
return item;
2026-06-10 10:15:10 +08:00
},
2026-06-23 09:55:51 +08:00
getOptionLabel: function (item) {
if (item && typeof item === 'object') {
var label = item.label || item.title || item.name || item.text;
if (label !== undefined && label !== null) return label;
return String(this.getOptionValue(item));
}
return item;
},
findValueIndex: function (list, value) {
for (var i = 0; i < list.length; i++) {
if (list[i] == value) return i;
}
return -1;
},
isChecked: function (item) {
return this.findValueIndex(this.value || [], this.getOptionValue(item)) !== -1;
},
onToggle: function (item) {
var val = this.getOptionValue(item);
2026-06-10 10:15:10 +08:00
var newValue = (this.value || []).slice();
2026-06-23 09:55:51 +08:00
var index = this.findValueIndex(newValue, val);
2026-06-10 10:15:10 +08:00
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>