index.vue 1.91 KB
<!--
 * @Date: 2022-09-08 15:02:45
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2022-11-21 14:47:52
 * @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"
      :rules="item.rules"
      @click="showPicker = true"
    />
    <van-popup v-model:show="showPicker" position="bottom">
      <van-picker-group
        :title="item.component_props.title"
        :tabs="['选择日期', '选择时间']"
        @confirm="onConfirm"
        @cancel="onCancel"
      >
        <van-date-picker
          v-model="currentDate"
          :min-date="item.component_props.min_date"
        />
        <van-time-picker v-model="currentTime" />
      </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;
};

onMounted(() => {
  // 获取值定位显示
  const datetime = props.item.value.split(" ");
  currentDate.value = datetime[0]?.split("-");
  currentTime.value = datetime[1]?.split(":");
});
</script>

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

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