index.vue 5.61 KB
<!--
 * @Date: 2022-08-30 11:34:19
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2026-06-03 16:09:40
 * @FilePath: /data-table/src/components/GenderField/index.vue
 * @Description: 性别选择控件
-->
<template>
  <div v-if="HideShow" class="gender-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">*&nbsp;</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"
      :name="item.name"
      :rules="item.rules"
      :required="item.component_props.required"
      :disabled="item.component_props.disabled"
    >
      <template #input>
        <van-radio-group @change="onChange(item)" v-model="gender_value" :direction="item.component_props.direction" style="width: 100%">
          <div v-for="x in item.component_props.options" :key="x.value" class="radio-wrapper">
            <!-- <div v-if="item.component_props.readonly">
              <div v-if="item.component_props.default === 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>
            </div>
            <van-radio v-else :name="x.value" icon-size="1rem" :checked-color="themeVars.radioColor"
            style="margin-bottom: 0.25rem">{{ x.title }}</van-radio> -->
            <van-config-provider :theme-vars="{ radioLabelColor: x.color }">
              <van-radio @click="handleClick(item)" :name="x.value" icon-size="1rem" :checked-color="themeVars.radioColor" style="margin-bottom: 0rem">
                <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>
        </van-radio-group>
      </template>
    </van-field>
    <div v-else style="padding: 0.5rem 1.3rem; font-size: 0.9rem;">{{ item.component_props.default }}</div>
  </div>
</template>

<script setup>
import { useRoute } from "vue-router";
import { styleColor } from "@/constant.js";
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;
});

const gender_value = ref(props.item.component_props.default);

onMounted(() => {
  gender_value.value = props.item.component_props.default ? props.item.component_props.default : '';
  // 发送自定义数据结构
  props.item.value = { key: props.item.key, value: gender_value.value, type: "gender" };
  emit("active", props.item.value);
});

// 监听默认值变化
watch(
  () => props.item.component_props.default,
  (v) => {
    if (v) {
      gender_value.value = v;
      // 发送自定义数据结构
      props.item.value = { key: props.item.key, value: gender_value.value, type: "gender" };
      emit("active", props.item.value);
    }
  }
);

const emit = defineEmits(["active"]);

const onChange = (item) => {
  // 发送自定义数据结构
  props.item.value = { key: props.item.key, value: gender_value.value, type: "gender" };
  emit("active", props.item.value);
  // 适配cookie保存未完成表单
  const currentValue = gender_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 handleClick = (item) => {
  // 如果当前选中项和点击的项相同,清空选中项
  if (item.value.value === gender_value.value) {
    gender_value.value = '';
    // 发送自定义数据结构
    onChange(item);
  }
}
</script>

<style lang="less" scoped>
.gender-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;
  }
}
</style>