index.vue
8.37 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<!--
* @Date: 2022-08-30 11:34:19
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-06-10 14:38:18
* @FilePath: /data-table/src/components/CheckboxField/index.vue
* @Description: 多项选择控件
-->
<template>
<div v-if="HideShow" class="checkbox-field-page">
<div class="label">
<span v-if="item.component_props.disabled_show"><van-icon name="https://cdn.ipadbiz.cn/custom_form/icon/closed-eye1.png" /></span>
<span v-if="item.component_props.required" style="color: red"> *</span>
<span :class="[ReadonlyShow ? 'readonly-show' : '']">{{ item.component_props.label }}</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 v-if="!item.component_props.readonly" :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%">
<div v-for="x in item.component_props.options" :key="x.title" class="checkbox-wrapper" :style="{ border: item.component_props.readonly ? 0 : ''}">
<!-- <div v-if="item.component_props.readonly">
<div v-if="checkbox_value.includes(x.value)" role="checkbox" class="van-checkbox van-checkbox--vertical" tabindex="0" aria-checked="true" data-v-3722f8ba="" style="margin-bottom: 0.25rem;"><div class="van-checkbox__icon van-checkbox__icon--square van-checkbox__icon--checked" style="font-size: 1rem;"><i class="van-badge__wrapper van-icon van-icon-success" style="border-color: rgb(194, 145, 95); background-color: rgb(194, 145, 95);"></i></div><span class="van-checkbox__label">{{ x.title }}</span></div>
<div v-else role="checkbox" class="van-checkbox van-checkbox--vertical" tabindex="0" aria-checked="false" data-v-3722f8ba="" style="margin-bottom: 0.25rem;"><div class="van-checkbox__icon van-checkbox__icon--square" style="font-size: 1rem;"><i class="van-badge__wrapper van-icon van-icon-success" style=""></i></div><span class="van-checkbox__label">{{ x.title }}</span></div>
</div>
<van-checkbox v-else @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-config-provider :theme-vars="{ checkboxLabelColor: x.color }">
<van-checkbox @click="onClick(x)" :name="x.title" icon-size="1rem" shape="square"
:checked-color="themeVars.radioColor">
<template #default>
<div :style="{ backgroundColor: x.background_color || '', padding: '0.15rem 0.5rem', borderRadius: '0.25rem' }">{{ x.title }}</div>
</template>
</van-checkbox>
</van-config-provider>
<van-field v-if="checkbox_value?.includes(x.value) && x.is_input" :name="'_' + item.key" @blur="onBlur(x)" v-model="x.affix"
label=" " label-width="5px" :placeholder="x.input_placeholder" :rules="x.input_required ? rules : ''"
:required="x.input_required" :readonly="item.component_props.readonly" class="affix-input" />
</div>
</van-checkbox-group>
</template>
</van-field>
<div v-else class="readonly-wrapper">
<span v-for="(item, index) in checkbox_value" :key="index" class="readonly-text">{{ item }} </span>
</div>
</div>
</template>
<script setup>
import { styleColor } from "@/constant.js";
import { useRoute } from "vue-router";
import Cookies from 'js-cookie';
import _ from 'lodash';
const $route = useRoute();
const props = defineProps({
item: Object,
});
// TAG: 自定义主题颜色
const themeVars = {
radioColor: styleColor.baseColor,
};
onMounted(() => {
// 默认值为数组
props.item.value = props.item.component_props?.default ? props.item.component_props.default : [];
});
// 隐藏显示
const HideShow = computed(() => {
return !props.item.component_props.disabled
})
// 只读显示-流程模式
const ReadonlyShow = computed(() => {
return ($route.query.page_type === 'flow' || $route.query.page_type === 'edit') && !props.item.component_props.readonly;
});
// 校验函数返回 true 表示校验通过,false 表示不通过
const required = props.item.component_props.required;
const validator = (val) => {
if (required && !val) {
return false;
} else {
return true;
}
};
// 错误提示文案
const validatorMessage = (val, rule) => {
if (required && !val) {
return "补充信息不能为空";
}
};
const rules = [{ validator, message: validatorMessage }];
const emit = defineEmits(["active"]);
const checkbox_value = ref(props.item.component_props?.default ? 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 ? `${item.title}: ${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);
// 适配cookie保存未完成表单
const checkbox_v = _.cloneDeep(checkbox_value.value)
checkbox_v?.forEach((element, index) => {
for (const key in affix_value.value) {
if (affix_value.value[key] && element === key) {
checkbox_value[index] = affix_value.value[key]
}
}
});
const currentValue = checkbox_v;
const existingCookie = Cookies.get($route.query.code);
if (existingCookie) {
// 如果Cookie存在,更新它
let obj = JSON.parse(existingCookie);
obj[props.item.key] = currentValue; // 替换掉旧值
Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
} else {
// 如果Cookie不存在,新增它
Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }), { expires: 1 });
}
}
onMounted(() => {
// TAG:默认值处理
let arr = props.item.component_props.default ? props.item.component_props.default : [];
let hasColonSome = arr?.some(item => item.includes(':')); // 检查是否有元素包含冒号
if (hasColonSome) { // 带补充信息输入框
// 提取冒号前的部分生成新的数组
let titles = arr.map(item => {
if (item.includes(':')) {
return item.split(': ')[0]
} else {
return item
}
});
// 生成对象
let obj = {};
arr.forEach(item => {
if (item.includes(':')) {
let parts = item.split(': ');
obj[parts[0]] = (parts[1]);
}
});
checkbox_value.value = titles;
//
props.item.component_props.options.forEach(item => {
for (const key in obj) {
if (item.value === key) {
item.affix = obj[key]
}
}
})
} else { // 纯多选情况
checkbox_value.value = arr;
}
// 同步数据结构中的checked属性
props.item.component_props.options.forEach(item => {
checkbox_value.value.forEach(x => {
if (item.value === x) {
item.checked = true;
affix_value.value[item.value] = item.affix ? `${item.title}: ${item.affix}` : '';
}
})
});
// 发送自定义数据结构
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-wrap;
}
.checkbox-wrapper {
border: var(--border-style);
border-radius: 0.25rem;
padding: 0.25rem 0.5rem;
margin-bottom: 0.25rem;
}
.affix-input {
border: var(--border-style);
border-radius: 0.25rem;
padding: 0.25rem 0.5rem;
margin-top: 0.5rem;
margin-bottom: 0.25rem;
}
.readonly-wrapper {
padding: 0.5rem 1.3rem;
}
.readonly-text {
font-size: 0.9rem;
}
}
</style>