index.vue 3.96 KB
<!--
 * @Date: 2022-09-08 15:02:45
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2022-12-22 18:52:28
 * @FilePath: /data-table/src/components/DateTimePickerField/index.vue
 * @Description: 日期时间选择器
-->
<template>
  <div class="datetime-picker">
    <div class="label">
      {{ item.component_props.label }}
      <span v-if="item.component_props.required">&nbsp;*</span>
    </div>
    <van-field
      v-model="item.value"
      is-link
      readonly
      :name="item.key"
      :required="item.component_props.required"
      :placeholder="item.component_props.placeholder ? item.component_props.placeholder : '请选择日期时间'"
      :rules="rules"
      @click="showPicker = true"
      :border="false"
    />
    <van-popup v-model:show="showPicker" position="bottom">
      <van-picker-group
        title="请选择日期时间"
        :tabs="['选择日期', '选择时间']"
        @confirm="onConfirm"
        @cancel="onCancel"
      >
        <van-date-picker v-model="currentDate" :columns-type="columns_date_type" />
        <van-time-picker v-model="currentTime" :columns-type="columns_time_type" />
      </van-picker-group>
    </van-popup>
  </div>
</template>

<script setup>
import { showToast } from "vant";
import dayjs from "dayjs";

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

const currentDate = ref([]);
const currentTime = ref([]);

const onConfirm = () => {
  props.item.value = `${currentDate.value.join("-")} ${currentTime.value.join(":")}`;
  showPicker.value = false;
};
const onCancel = () => {
  showPicker.value = false;
};

const columns_date_type = ref([]);
const columns_time_type = ref([]);
const date_format = props.item.component_props.data_dateformat;
onMounted(() => {
  // 获取值定位显示
  const datetime = props.item.value.split(" ");
  currentDate.value = datetime[0]?.split("-");
  currentTime.value = datetime[1]?.split(":");
  // YYYY=年,YYYY-MM=年月,YYYY-MM-DD=年月日,YYYY-MM-DD HH=年月日时,YYYY-MM-DD HH:mm=年月日时分,YYYY-MM-DD HH:mm:ss=年月日时分秒
  switch (date_format) {
    case "YYYY":
      columns_date_type.value = ['year']
      break;
    case "YYYY-MM":
      columns_date_type.value = ['year', 'month']
      break;
    case "YYYY-MM-DD":
      columns_date_type.value = ['year', 'month', 'day']
      break;
    case "YYYY-MM-DD HH":
      columns_date_type.value = ['year', 'month', 'day']
      columns_time_type.value = ['hour']
      break;
    case "YYYY-MM-DD HH:mm":
      columns_date_type.value = ['year', 'month', 'day']
      columns_time_type.value = ['hour', 'minute']
      break;
    case "YYYY-MM-DD HH:mm:ss":
      columns_date_type.value = ['year', 'month', 'day']
      columns_time_type.value = ['hour', 'minute', 'second']
      break;
  }
});

const required = props.item.component_props.required;
const data_minvalue = props.item.component_props.data_minvalue;
const data_maxvalue = props.item.component_props.data_maxvalue;
const validator = (val) => {
  if (required && !val) {
    return false;
  } else if (val && data_minvalue && val < data_minvalue) {
    return false;
  } else if (val && data_maxvalue && val > data_maxvalue) {
    return false;
  } else {
    return true;
  }
};
// 错误提示文案
const validatorMessage = (val, rule) => {
  if (required && !val) {
    return "必填项不能为空";
  } else if (val && data_minvalue && val < data_minvalue) {
    return "最小可选:" + data_minvalue;
  } else if (val && data_maxvalue && val > data_maxvalue) {
    return "最大可选:" + data_maxvalue;
  }
};
const rules = [{ validator, message: validatorMessage }];
</script>

<style lang="less" scoped>
.datetime-picker {
  margin: 1rem;
  .label {
    // padding: 1rem 1rem 0 1rem;
    font-size: 0.9rem;
    font-weight: bold;

    span {
      color: red;
    }
  }
}

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