-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathDatePicker.vue
175 lines (163 loc) · 5.18 KB
/
DatePicker.vue
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<script setup>
import { now, startOfMonth, startOfWeek } from '@internationalized/date'
import { useUrlSearchParams } from '@vueuse/core'
import { safeDestr } from 'destr'
const emit = defineEmits(['update:dateRange'])
const time = inject('time')
const dateRange = ref('last-7d')
const openCustomDateRange = ref(false)
const customDate = ref()
const customDateRange = ref()
const locale = getLocale()
function updateCustomDate(customDateValue) {
emit('update:dateRange', [date2unix(customDateValue, 'start'), date2unix(customDateValue, 'end')])
openCustomDateRange.value = false
customDate.value = undefined
}
function updateCustomDateRange(customDateRangeValue) {
if (customDateRangeValue.start && customDateRangeValue.end) {
emit('update:dateRange', [date2unix(customDateRangeValue.start, 'start'), date2unix(customDateRangeValue.end, 'end')])
openCustomDateRange.value = false
customDateRange.value = undefined
}
}
function isDateDisabled(dateValue) {
return dateValue.toDate() > new Date()
}
watch(dateRange, (newValue) => {
switch (newValue) {
case 'today':
emit('update:dateRange', [date2unix(now(), 'start'), date2unix(now())])
break
case 'last-24h':
emit('update:dateRange', [date2unix(now().subtract({ hours: 24 })), date2unix(now())])
break
case 'this-week':
emit('update:dateRange', [date2unix(startOfWeek(now(), locale), 'start'), date2unix(now())])
break
case 'last-7d':
emit('update:dateRange', [date2unix(now().subtract({ days: 7 })), date2unix(now())])
break
case 'this-month':
emit('update:dateRange', [date2unix(startOfMonth(now()), 'start'), date2unix(now())])
break
case 'last-30d':
emit('update:dateRange', [date2unix(now().subtract({ days: 30 })), date2unix(now())])
break
case 'last-90d':
emit('update:dateRange', [date2unix(now().subtract({ days: 90 })), date2unix(now())])
break
case 'custom':
openCustomDateRange.value = true
dateRange.value = null
break
default:
break
}
})
function restoreDateRange() {
try {
const searchParams = useUrlSearchParams('history')
if (searchParams.time) {
const time = safeDestr(searchParams.time)
emit('update:dateRange', [time.startAt, time.endAt])
dateRange.value = 'custom'
nextTick(() => {
openCustomDateRange.value = false
customDateRange.value = undefined
})
}
}
catch (error) {
console.error('restore searchParams error', error)
}
}
onBeforeMount(() => {
restoreDateRange()
})
</script>
<template>
<Select v-model="dateRange">
<SelectTrigger>
<SelectValue v-if="dateRange" />
<div v-else>
{{ shortDate(time.startAt) }} - {{ shortDate(time.endAt) }}
</div>
</SelectTrigger>
<SelectContent>
<SelectItem value="today">
{{ $t('dashboard.date_picker.today') }}
</SelectItem>
<SelectItem value="last-24h">
{{ $t('dashboard.date_picker.last_24h') }}
</SelectItem>
<SelectSeparator />
<SelectItem value="this-week">
{{ $t('dashboard.date_picker.this_week') }}
</SelectItem>
<SelectItem value="last-7d">
{{ $t('dashboard.date_picker.last_7d') }}
</SelectItem>
<SelectSeparator />
<SelectItem value="this-month">
{{ $t('dashboard.date_picker.this_month') }}
</SelectItem>
<SelectItem value="last-30d">
{{ $t('dashboard.date_picker.last_30d') }}
</SelectItem>
<SelectSeparator />
<SelectItem value="last-90d">
{{ $t('dashboard.date_picker.last_90d') }}
</SelectItem>
<SelectSeparator />
<SelectItem value="custom">
{{ $t('dashboard.date_picker.custom') }}
</SelectItem>
</SelectContent>
</Select>
<Dialog v-model:open="openCustomDateRange">
<DialogContent class="w-auto max-w-[95svw] max-h-[95svh] md:max-w-screen-md grid-rows-[auto_minmax(0,1fr)_auto]">
<DialogHeader>
<DialogTitle>{{ $t('dashboard.date_picker.custom_title') }}</DialogTitle>
</DialogHeader>
<Tabs
default-value="range"
>
<div class="flex justify-center">
<TabsList>
<TabsTrigger value="date">
{{ $t('dashboard.date_picker.single_date') }}
</TabsTrigger>
<TabsTrigger value="range">
{{ $t('dashboard.date_picker.date_range') }}
</TabsTrigger>
</TabsList>
</div>
<TabsContent
value="date"
class="overflow-y-auto h-80"
>
<Calendar
:model-value="customDate"
weekday-format="short"
:is-date-disabled="isDateDisabled"
@update:model-value="updateCustomDate"
/>
</TabsContent>
<TabsContent
value="range"
class="overflow-y-auto h-80"
>
<RangeCalendar
:model-value="customDateRange"
initial-focus
weekday-format="short"
:number-of-months="2"
:is-date-disabled="isDateDisabled"
@update:model-value="updateCustomDateRange"
/>
</TabsContent>
</Tabs>
</DialogContent>
</Dialog>
</template>