You need to sign in or sign up before continuing.
index.vue 3.65 KB
<!--
 * @Date: 2022-08-30 11:34:19
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2023-02-01 18:58:00
 * @FilePath: /data-table/src/components/RadioField/index.vue
 * @Description: 单项选择控件
-->
<template>
  <div v-if="HideShow" class="radio-field-page">
    <div class="label">
      {{
        item.component_props.label
      }}<span v-if="item.component_props.required">&nbsp;*</span>
    </div>
    <div v-if="item.component_props.note" class="note" v-html="item.component_props.note" />
    <van-field :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">
            <van-radio :name="x.value" icon-size="1rem" :checked-color="themeVars.radioColor"
              style="margin-bottom: 0.25rem">{{ x.title }}</van-radio>
            <van-field v-if="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>
</template>

<script setup>
import { styleColor } from "@/constant.js";

const props = defineProps({
  item: Object,
});

// TAG: 自定义主题颜色
const themeVars = {
  radioColor: styleColor.baseColor,
};
// 隐藏显示
const HideShow = computed(() => {
  return !props.item.component_props.disabled
})

// TODO: 等待数据结构更新,看看怎么判断必填
// 校验函数返回 true 表示校验通过,false 表示不通过
const validator = (val) => {
  if (!val) {
    return false;
  } else {
    return true;
  }
};
// 错误提示文案
const validatorMessage = (val, rule) => {
  if (!val) {
    return "补充信息不能为空";
  }
};
const rules = [{ validator, message: validatorMessage }];

const emit = defineEmits(["active"]);
const radio_value = ref(props.item.component_props.default);
const affix_value = ref('');

const onChange = (item) => {
  clearAffix()
}
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(() => {
  // 发送自定义数据结构
  props.item.value = { key: props.item.key, value: radio_value.value, affix: affix_value.value, type: "radio" };
  emit("active", props.item.value);
})
</script>

<style lang="less" scoped>
.radio-field-page {
  .label {
    padding: 1rem 1rem 0 1rem;
    font-size: 0.9rem;
    font-weight: bold;

    span {
      color: red;
    }
  }

  .note {
    font-size: 0.9rem;
    margin-left: 1rem;
    color: gray;
    padding-bottom: 0.5rem;
    white-space: pre-wrap;
  }

  .radio-wrapper {
    border: 1px solid #eaeaea;
    border-radius: 0.25rem;
    padding: 0.25rem 0.5rem;
    margin-bottom: 0.25rem;
  }
  .affix-input {
    border: 1px solid #eaeaea;
    border-radius: 0.25rem;
    padding: 0.25rem 0.5rem;
    margin-top: 0.5rem;
    margin-bottom: 0.25rem;
  }
}

:deep(.van-radio) {
  // border: 1px solid #eaeaea;
  // border-radius: 0.25rem;
  // padding: 0.25rem 0.5rem;
}
</style>