index.vue
3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!--
* @Date: 2022-08-30 11:34:19
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2023-02-01 00:19:34
* @FilePath: /data-table/src/components/CheckboxField/index.vue
* @Description: 多项选择控件
-->
<template>
<div v-if="HideShow" class="checkbox-field-page">
<div class="label">
{{ item.component_props.label }}
<span v-if="item.component_props.required" style="color: red"> *</span>
<span v-if="item.component_props.max" style="color: gray">
(最多可选数: {{ item.component_props.max }})
</span>
</div>
<div v-if="item.component_props.note" class="note" v-html="item.component_props.note" />
<van-field :rules="item.rules" :border="false">
<template #input>
<van-checkbox-group
v-model="checkbox_value"
:direction="item.component_props.direction"
:max="item.component_props.max"
style="width: 100%"
>
<template v-for="x in item.component_props.options" :key="x.title">
<van-checkbox
@click="onClick(x)"
:name="x.title"
icon-size="1rem"
shape="square"
:checked-color="themeVars.radioColor"
style="margin-bottom: 0.25rem"
>{{ x.title }}</van-checkbox
>
<van-field
v-if="checkbox_value.includes(x.value)"
@blur="onBlur(x)"
v-model="x.affix"
label=""
placeholder="请输入补充信息"
style="border: 1px solid #eaeaea;border-radius: 0.25rem; padding: 0.25rem 0.5rem;"
/>
</template>
</van-checkbox-group>
</template>
</van-field>
</div>
</template>
<script setup>
import { styleColor } from "@/constant.js";
const props = defineProps({
item: Object,
});
// TAG: 自定义主题颜色
const themeVars = {
radioColor: styleColor.baseColor,
};
onMounted(() => {
// 默认值为数组
props.item.value = props.item.component_props.default;
});
// 隐藏显示
const HideShow = computed(() => {
return !props.item.component_props.disabled
})
const emit = defineEmits(["active"]);
const checkbox_value = ref(props.item.component_props.default);
const affix_value = ref({});
const onClick = (item) => {
item.checked = !item.checked;
handleEmit(item)
}
const onBlur = (item) => {
handleEmit(item)
}
const handleEmit = (item) => {
// 选中状态添加属性
if (item.checked) {
affix_value.value[item.value] = item.affix
} else {
// 为选中删除属性
delete affix_value.value[item.value]
}
// 发送自定义数据结构
props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" };
emit("active", props.item.value);
}
onMounted(() => {
// 发送自定义数据结构
props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" };
emit("active", props.item.value);
})
</script>
<style lang="less" scoped>
.checkbox-field-page {
.label {
padding: 1rem 1rem 0 1rem;
font-size: 0.9rem;
font-weight: bold;
}
.note {
font-size: 0.9rem;
margin-left: 1rem;
color: gray;
padding-bottom: 0.5rem;
white-space: pre;
}
}
:deep(.van-checkbox) {
border: 1px solid #eaeaea;
border-radius: 0.25rem;
padding: 0.25rem 0.5rem;
// width: 100vw;
}
</style>