1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<template>
  <el-form-item :label="label" :prop="prop">
    <el-date-picker
      v-model="date"
      @change="handleChange"
      :type="type"
      placeholder="选择时间"
      start-placeholder="选择开始时间"
      end-placeholder="选择结束时间"
      style="width: 150px"
      v-bind="$attrs"
    />
  </el-form-item>
</template>
 
<script>
import dayjs from 'dayjs';
 
const MONTH = 'month';
const DATE = 'date';
const RANGE = 'datetimerange';
const RANGE2 = 'daterange';
 
export default {
  props: {
    type: {
      type: String,
      default: MONTH
    },
    // 返回结果
    value: Date || Array,
    // 是否默认返回初始选项
    initValue: {
      type: Boolean,
      default: true
    },
    label: {
      type: String,
      default: '时间'
    },
    prop: {
      type: String,
      default: 'time'
    }
  },
  emits: ['update:value', 'change'],
  data() {
    return {
      date: this.value
    };
  },
  computed: {},
  methods: {
    handleChange(value) {
      this.$emit('update:value', value);
      this.$emit('change', value);
    }
  },
  mounted() {
    if (this.initValue) {
      switch (this.type) {
        case RANGE:
        case RANGE2:
          this.date = [dayjs().startOf('month').toDate(), dayjs().toDate()];
          break;
        case MONTH:
          this.date = dayjs().startOf('month').toDate();
          break;
        case DATE:
          this.date = dayjs().toDate();
          break;
        default:
          break;
      }
      this.handleChange(this.date);
    }
  }
};
</script>