cloudroam
2025-03-28 cef2bb0eeeb91a22860cf5d23c7348af1ba921dc
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<script>
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
dayjs.locale('zh-cn')
export default {
  inputFormat(row, item) {
    if (
      Array.isArray(item.commonFormatProps) &&
      item.commonFormatProps.length === 3
    ) {
      const dateType = item.commonFormatProps[0]
      const startDate = item.commonFormatProps[1]
      const endDate = item.commonFormatProps[2]
      if (dateType in row || startDate in row || endDate in row) {
        const dateValue =
          !row[startDate] && !row[endDate] ? [] : [row[startDate], row[endDate]]
        return !row[dateType] && dateValue.length === 0
          ? []
          : [row[dateType], dateValue]
      }
    }
  },
  outputFormat(val, item) {
    if (
      Array.isArray(item.commonFormatProps) &&
      item.commonFormatProps.length === 3
    ) {
      const dateValue = val?.[1] || []
      const radioValue = val?.[0] || ''
      return {
        [item.commonFormatProps[0]]: radioValue,
        [item.commonFormatProps[1]]: dateValue?.[0] || '',
        [item.commonFormatProps[2]]: dateValue?.[1] || '',
      }
    }
  },
  props: {
    value: {
      type: Array,
      default: () => [],
    },
    options: {
      type: Array,
      default: () => [],
    },
    radioAttrs: {
      type: Object,
      default: () => ({}),
    },
    dateRangeAttrs: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      radioValue: '',
      dateRangeValue: [],
    }
  },
  watch: {
    value: {
      immediate: true,
      handler(value) {
        this.radioValue = value?.[0] || ''
        this.dateRangeValue = value?.[1] || []
      },
    },
  },
  methods: {
    onRadioChange(e) {
      if (!e) {
        this.emitValue([])
      } else if (e === 'custom') {
        this.emitValue(['custom'])
      } else if (typeof e === 'string') {
        if (!e.includes(',')) {
          if (e === 'yesterday') {
            const yesterday = dayjs().subtract(1, 'day').format('YYYY-MM-DD')
            this.emitValue([e, [yesterday, yesterday]])
          } else {
            const startDate = dayjs().startOf(e).format('YYYY-MM-DD')
            const endDate = dayjs().format('YYYY-MM-DD')
            this.emitValue([e, [startDate, endDate]])
          }
        } else {
          const arr = e.split(',')
          const num = arr[0]
          let funcName = ''
          if (e > 0) {
            funcName = 'add'
          } else {
            funcName = 'subtract'
          }
          const startDate = dayjs()
            [funcName](Math.abs(num), arr[1])
            .format('YYYY-MM-DD')
          const endDate = dayjs().format('YYYY-MM-DD')
          this.emitValue([e, [startDate, endDate]])
        }
      }
    },
    onDateRangeChange(e) {
      this.emitValue(['custom', e])
    },
    emitValue(value) {
      this.$emit('input', value)
      this.$emit('change', value)
    },
  },
}
</script>
 
<template>
  <div class="custom-date-range">
    <el-bus-radio
      v-bind="{ childType: 'el-radio-button', hasAll: true, ...radioAttrs }"
      v-model="radioValue"
      :from-dict="false"
      :options="options"
      @change="onRadioChange"
    />
    <el-bus-date-range
      v-bind="{ canOverToday: false, ...dateRangeAttrs }"
      v-model="dateRangeValue"
      @change="onDateRangeChange"
    />
  </div>
</template>
 
<style lang="scss" scoped>
.custom-date-range {
  display: flex;
  align-items: center;
  .el-bus-date-range {
    margin-bottom: 10px;
    margin-left: 15px;
  }
}
</style>