index.vue 1.39 KB
<!--
 * @Date: 2022-09-08 15:47:54
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2022-09-08 16:20:11
 * @FilePath: /data-table/src/components/RatePickerField/index.vue
 * @Description: 文件描述
-->
<template>
  <div class="rate-field">
    <div class="label">{{ item.label }}<span v-if="item.required">&nbsp;*</span></div>
    <van-rate v-model="rate_value" :count="item.component_props.count" @change="onChange" style="padding: 1rem;" />
    <div v-if="show_empty" class="van-field__error-message" style="padding: 0 1rem 1rem 1rem;">评分不能为空</div>
  </div>
</template>

<script setup>
const props = defineProps({
  item: Object
});
const emit = defineEmits(['active']);
const show_empty = ref(false);
const rate_value = ref(0);

const onChange = () => {
  props.item.value = { key: 'rate', value: rate_value.value };
  emit('active', props.item.value);
}

onMounted(() => {
  props.item.value = { key: 'rate', value: rate_value.value };
  emit('active', props.item.value);
});

const validRate = () => {
  // 必填项
  if (props.item.required && !rate_value.value) {
    show_empty.value = true;
  } else {
    show_empty.value = false;
  }
  return !show_empty.value
}

defineExpose({ validRate });
</script>

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

    span {
      color: red;
    }
  }
}
</style>