index.vue
9.84 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<!--
* @Date: 2022-08-30 11:34:19
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-03-27 14:17:04
* @FilePath: /data-table/src/components/RadioField/index.vue
* @Description: 单项选择控件
-->
<template>
<div v-if="HideShow" class="radio-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>
</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">
<template #input>
<van-radio-group @change="onChange(item)" v-model="radio_value" :direction="item.component_props.direction"
style="width: 100%">
<div v-for="x in item.component_props.options" :key="x.value" class="radio-wrapper" :style="{ border: item.component_props.readonly ? 0 : ''}">
<div v-if="item.component_props.readonly">
<div v-if="default_value === x.value" role="radio" class="van-radio van-radio--vertical" tabindex="0" aria-checked="true" data-v-04873bb2="" style="margin-bottom: 0.25rem;"><div class="van-radio__icon van-radio__icon--round van-radio__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-radio__label">{{ x.title }}</span></div>
<div v-else role="radio" class="van-radio van-radio--vertical" tabindex="0" aria-checked="false" data-v-04873bb2="" style="margin-bottom: 0.25rem;"><div class="van-radio__icon van-radio__icon--round" style="font-size: 1rem;"><i class="van-badge__wrapper van-icon van-icon-success"></i></div><span class="van-radio__label">{{ x.title }}</span></div>
<span v-if="default_value === x.value && default_affix_value">{{ default_affix_value }}</span>
</div>
<van-config-provider v-else :theme-vars="{ radioLabelColor: x.color }">
<van-radio
:name="x.value"
icon-size="1rem"
:checked-color="themeVars.radioColor"
@click="handleClick(item)"
>
<template #default>
<div :style="{ backgroundColor: x.background_color || '', padding: '0.15rem 0.5rem', borderRadius: '0.25rem' }">{{ x.title }}</div>
</template>
</van-radio>
</van-config-provider>
<div v-if="x.desc_text" class="van-multi-ellipsis--l3 rule-desc-text">{{ x.desc_text }}</div>
<div v-if="x.desc_type === 'text'" class="rule-box" @click="showRule(x)">
{{ x.desc_btn_name }} >>
</div>
<div v-if="x.desc_type === 'url'" class="rule-box" @click="showUrl(x)">
{{ x.desc_btn_name }} <van-icon name="link-o" />
</div>
<van-field
:name="'_' + item.key"
v-if="!item.component_props.readonly && radio_value === x.value && x.is_input"
@blur="onBlur(x)" v-model="x.affix"
label=" " label-width="5px"
:placeholder="x.input_placeholder" :rules="x.input_required ? rules : ''" :required="x.input_required"
class="affix-input" />
</div>
</van-radio-group>
</template>
</van-field>
<div v-else style="padding: 0.5rem 1.3rem; font-size: 0.9rem;">
<span>{{ default_value }}</span>
<span>{{ default_affix_value }}</span>
</div>
</div>
<van-overlay :show="show" :lock-scroll="false">
<div class="rule-wrapper" @click.stop>
<div class="rule-block">
<div style="height: 70vh; min-height: 70vh; overflow: scroll; white-space: pre-wrap; line-height: 1.5;" v-html="rule_content"></div>
<div class="close-btn">
<van-button type="primary" block @click="closeRule"
>关 闭</van-button
>
</div>
<div></div>
</div>
</div>
</van-overlay>
</template>
<script setup>
import { styleColor } from "@/constant.js";
import $ from "jquery";
import { useRoute } from "vue-router";
import Cookies from 'js-cookie';
const $route = useRoute();
const props = defineProps({
item: Object,
});
// TAG: 自定义主题颜色
const themeVars = {
radioColor: styleColor.baseColor,
};
// 隐藏显示
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 radio_value = ref('');
const affix_value = ref('');
const default_value = ref('');
const default_affix_value = ref('');
const onChange = (item) => {
clearAffix()
// 发送自定义数据结构
props.item.value = { key: props.item.key, value: radio_value.value, affix: affix_value.value, type: "radio" };
emit("active", props.item.value);
// 适配cookie保存未完成表单
const currentValue = affix_value.value ? affix_value.value : radio_value.value;
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 });
}
}
const onBlur = (item) => {
clearAffix()
affix_value.value = item.affix ? `${item.title}: ${item.affix}` : '';
// 发送自定义数据结构
props.item.value = { key: props.item.key, value: radio_value.value, affix: affix_value.value, type: "radio" };
emit("active", props.item.value);
}
const clearAffix = () => {
const options = props.item.component_props.options;
// 为选中项目的补充清空
options.forEach(element => {
if (element.value !== radio_value.value) {
element.affix = ''
}
});
}
onMounted(() => {
radio_value.value = props.item.component_props.default;
// 单选存在补充信息情况下处理
if (props.item.component_props.default !== null && props.item.component_props.default.includes(':')) {
let parts = props.item.component_props.default.split(':');
let part1 = parts[0].trim(); // 去除前后空格
let part2 = parts[1].trim(); // 去除前后空格
default_value.value = part1;
default_affix_value.value = part2;
// 编辑模式下处理
radio_value.value = part1; // 单选框勾选
// 补充输入框显示
const options = props.item.component_props.options;
// 为选中项目的补充清空
options.forEach(element => {
if (element.value === radio_value.value) {
element.affix = part2
}
});
} else {
default_value.value = props.item.component_props.default;
}
// 发送自定义数据结构
props.item.value = { key: props.item.key, value: radio_value.value, affix: affix_value.value, type: "radio" };
emit("active", props.item.value);
//
$(".rule-box").css("color", themeVars.radioColor);
})
// 补充信息弹框
const show = ref(false);
const showRule = (rule) => {
show.value = true;
rule.desc_text = rule.desc_text.replace(/\\n/g, "<br>")
rule_content.value = rule.desc_text;
};
const closeRule = () => {
show.value = false;
rule_content.value = "";
};
const showUrl = (rule) => {
location.href = rule.desc_url
}
const rule_content = ref("");
// 单选框点击事件, 清空当前选中项
const handleClick = (item) => {
// 如果当前选中项和点击的项相同,清空选中项
if (item.value.value === radio_value.value) {
radio_value.value = ''; // 清空选中项
affix_value.value = ''; // 清空补充信息
// 发送自定义数据结构
onChange(item);
}
}
</script>
<style lang="less" scoped>
.radio-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;
}
.radio-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;
}
.rule-desc-text {
margin: 0.35rem 0.5rem 0.5rem 1.75rem;
font-size: 0.8rem;
color: #808080;
}
.rule-box {
font-size: 0.85rem;
margin-left: 1.8rem;
padding-bottom: 0.5rem;
width: fit-content;
}
}
.rule-wrapper {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
.rule-block {
position: relative;
width: 80vw;
height: 80vh;
background-color: #fff;
overflow: scroll;
padding: 1rem;
.close-btn {
position: absolute;
bottom: 1rem;
// transform: translateX(100%);
width: calc(100% - 2rem);
}
}
.multi-rule-field-box {
border: var(--border-style);
border-radius: 0.25rem;
padding: 1rem 0.5rem 0 0.5rem;
// width: 100vw;
margin-bottom: 0.5rem;
}
</style>