index.vue 1.6 KB
<!--
 * @Date: 2022-08-30 11:34:19
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2022-11-21 14:42:00
 * @FilePath: /data-table/src/components/CheckboxField/index.vue
 * @Description: 多项选择控件
-->
<template>
  <div 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>
    <van-field :name="item.key" :rules="item.rules" :border="false">
      <template #input>
        <van-checkbox-group
          v-model="item.value"
          :direction="item.component_props.direction"
          :max="item.component_props.max"
          style="width: 100%"
        >
          <van-checkbox
            v-for="x in item.component_props.options"
            :key="index"
            :name="x"
            icon-size="1rem"
            shape="square"
            style="margin-bottom: 0.25rem"
            >{{ x }}</van-checkbox
          >
        </van-checkbox-group>
      </template>
    </van-field>
  </div>
</template>

<script setup>
const props = defineProps({
  item: Object,
});

onMounted(() => {
  // 默认值为数组
  props.item.value = [];
});
</script>

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

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