index.vue 2.94 KB
<!--
 * @Date: 2022-08-30 11:34:19
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2023-01-30 12:40:36
 * @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">&nbsp;*</span>
      <span v-if="item.component_props.max" style="color: gray">
        (最多可选数:&nbsp;{{ item.component_props.max }})
      </span>
    </div>
    <div v-if="item.component_props.note" class="note" v-html="item.component_props.note" />
    <van-field :name="item.key" :rules="item.rules" :border="false">
      <template #input>
        <van-checkbox-group
          v-model="item.value"
          @change="onChange(item)"
          :direction="item.component_props.direction"
          :max="item.component_props.max"
          style="width: 100%"
        >
          <van-checkbox
            v-for="x in item.component_props.options"
            :key="x.value"
            :name="x.value"
            icon-size="1rem"
            shape="square"
            :checked-color="themeVars.radioColor"
            style="margin-bottom: 0.25rem"
            >{{ x.title }}</van-checkbox
          >
          <van-field v-if="has_add_info" :name="add_info_name" v-model="add_info" label="" placeholder="请输入补充信息" />
        </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
})
// TEST: 测试新功能:选择其他选项时,下方出现输入框,如果其他项被选中,输入框值为最终录入值。
// 绑定值发生变化时回调,处理选项为其他时的输入项录入
const has_add_info = ref(true); // 文字不一定是其他,后续可能需要字段绑定一个值,标识是否有其他输入框进行判断
const add_info = ref('');
const add_info_name = ref(props.item.key + '#');
const add_info_key = ref('其他'); // TODO: 以后动态获取
const onChange = (item) => {
  console.warn(item);
}
onMounted(() => {
  add_info_name.value = `${props.item.key}#${add_info_key.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>