-
Notifications
You must be signed in to change notification settings - Fork 725
/
FieldDateTimeInput.vue
412 lines (355 loc) · 10.7 KB
/
FieldDateTimeInput.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<!-- Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/ -->
<script setup lang="ts">
import { i18n } from '@shared/i18n'
import { useApplicationStore } from '@shared/stores/application'
import flatpickr from 'flatpickr'
import 'flatpickr/dist/themes/dark.css'
import {
onBeforeUnmount,
onMounted,
ref,
toRef,
shallowRef,
watch,
watchEffect,
computed,
nextTick,
} from 'vue'
import { useEventListener } from '@vueuse/core'
import type { RouteLocationRaw } from 'vue-router'
import { useRawHTMLIcon } from '@shared/components/CommonIcon'
import testFlags from '@shared/utils/testFlags'
import type { FormFieldContext } from '../../types/field'
import useValue from '../../composables/useValue'
export interface Props {
context: FormFieldContext<{
futureOnly?: boolean
maxDate?: flatpickr.Options.DateOption
minDate?: flatpickr.Options.DateOption
link?: RouteLocationRaw
}>
}
const props = defineProps<Props>()
const { currentValue } = useValue(toRef(props, 'context'))
const application = useApplicationStore()
const locale: flatpickr.CustomLocale = {
// calendar on desktop always starts at 1
firstDayOfWeek: 1,
weekdays: {
shorthand: [
i18n.t('Sun'),
i18n.t('Mon'),
i18n.t('Tue'),
i18n.t('Wed'),
i18n.t('Thu'),
i18n.t('Fri'),
i18n.t('Sat'),
],
longhand: [
i18n.t('Sunday'),
i18n.t('Monday'),
i18n.t('Tuesday'),
i18n.t('Wednesday'),
i18n.t('Thursday'),
i18n.t('Friday'),
i18n.t('Saturday'),
],
},
months: {
shorthand: [
i18n.t('Jan'),
i18n.t('Feb'),
i18n.t('Mar'),
i18n.t('Apr'),
i18n.t('May'),
i18n.t('Jun'),
i18n.t('Jul'),
i18n.t('Aug'),
i18n.t('Sep'),
i18n.t('Oct'),
i18n.t('Nov'),
i18n.t('Dec'),
],
longhand: [
i18n.t('January'),
i18n.t('February'),
i18n.t('March'),
i18n.t('April'),
i18n.t('May'),
i18n.t('June'),
i18n.t('July'),
i18n.t('August'),
i18n.t('September'),
i18n.t('October'),
i18n.t('November'),
i18n.t('December'),
],
},
rangeSeparator: i18n.t(' to '),
weekAbbreviation: i18n.t('CW'),
}
const pickerNode = shallowRef<HTMLElement>()
const datepicker = shallowRef<flatpickr.Instance>()
const time = computed(() => {
return props.context.type === 'datetime'
})
const getMinDate = () => {
if (props.context.minDate) {
return props.context.minDate
}
if (props.context.futureOnly) {
const now = new Date()
const tomorrow = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 1,
)
return tomorrow
}
return undefined
}
const canSelectToday = (picker: flatpickr.Instance) => {
const { minDate } = picker.config
if (!minDate) return true
const now = new Date()
return now.getTime() >= minDate.getTime()
}
const createTodayButton = (picker: flatpickr.Instance) => {
const todayAvailable = canSelectToday(picker)
if (!todayAvailable) return null
const todayButton = document.createElement('button')
todayButton.addEventListener('click', () => {
picker.setDate('today', true)
})
todayButton.type = 'button'
todayButton.name = 'today'
todayButton.textContent = i18n.t('Today')
todayButton.className = 'flex-1 hover:bg-gray-300 border border-gray rounded'
return todayButton
}
const createClearButton = (picker: flatpickr.Instance) => {
const clearButton = document.createElement('button')
clearButton.addEventListener('click', () => {
picker.clear(true)
})
clearButton.type = 'button'
clearButton.name = 'clear'
clearButton.className = 'flex-1 hover:bg-gray-300 border border-gray rounded'
clearButton.textContent = i18n.t('Clear')
return clearButton
}
const createCalendarFooter = (picker: flatpickr.Instance) => {
const footer = document.createElement('div')
footer.className = 'flex p-2 gap-2'
const clearButton = createClearButton(picker)
const todayButton = createTodayButton(picker)
footer.appendChild(clearButton)
if (todayButton) footer.appendChild(todayButton)
return footer
}
const iconPrevArrow = useRawHTMLIcon({ name: 'mobile-chevron-left' })
const iconNextArrow = useRawHTMLIcon({ name: 'mobile-chevron-right' })
const createFlatpickr = () => {
if (!pickerNode.value || props.context.disabled) return undefined
if (datepicker.value) {
datepicker.value.destroy()
}
// TODO redesign month selection
return flatpickr(pickerNode.value, {
locale,
disableMobile: true,
inline: true,
time_24hr: i18n.timeFormat() === '24hour',
enableTime: time.value,
allowInput: true,
// append calendar to parent, so we can add our own elements after input
// otherwise everything after input will actually appear after calendar
appendTo: pickerNode.value.parentNode?.parentNode as HTMLElement,
// The primary input element should display the current value of the input using context._value
defaultDate: currentValue.value,
maxDate: props.context.maxDate,
minDate: getMinDate(),
weekNumbers: application.config.datepicker_show_calendar_weeks === true,
prevArrow: iconPrevArrow,
nextArrow: iconNextArrow,
animate: !VITE_TEST_MODE,
formatDate(date) {
const isoDate = date.toISOString()
if (time.value) return i18n.dateTime(isoDate)
return i18n.date(isoDate)
},
onChange([date]) {
if (!date) {
props.context.node.input(date)
return
}
const formatted = flatpickr.formatDate(date, time.value ? 'Z' : 'Y-m-d')
props.context.node.input(formatted)
},
})
}
// store calendar height to animate it's appearance later
let flatpickrHeight = 0
const recalculateHeight = async (
flatpickr?: flatpickr.Instance,
): Promise<void> => {
if (!flatpickr) return undefined
const { calendarContainer } = flatpickr
if (!calendarContainer) return undefined
const { height } = calendarContainer.style
calendarContainer.style.height = 'auto'
return new Promise<void>((resolve) => {
requestAnimationFrame(() => {
flatpickrHeight =
calendarContainer.clientHeight || calendarContainer.scrollHeight
calendarContainer.style.height = height
resolve()
})
})
}
const rerenderFlatpickr = async () => {
const flatpickr = createFlatpickr()
if (flatpickr) {
const footer = createCalendarFooter(flatpickr)
const calendarNode = flatpickr.calendarContainer
calendarNode.setAttribute('role', 'dialog')
calendarNode.setAttribute('aria-label', i18n.t('Calendar'))
calendarNode.appendChild(footer)
await recalculateHeight(flatpickr)
}
datepicker.value = flatpickr
}
onMounted(rerenderFlatpickr)
watch(time, (enable) => datepicker.value?.set('enableTime', enable))
// rerender flatpickr, if props dynamically change
const watchableProps = ['maxDate', 'minDate', 'disabled', 'futureOnly']
watch(
watchableProps.map((name) => () => props.context[name]),
rerenderFlatpickr,
)
watch(currentValue, (date: string) => {
datepicker.value?.setDate(date, false)
})
watch(
() => application.config.datepicker_show_calendar_weeks,
rerenderFlatpickr,
)
// toggle calendar visibility when showPicker changes
const showPicker = ref(false)
watchEffect(async () => {
if (!datepicker.value) return
const calendar = datepicker.value.calendarContainer
const clearButton = calendar.getElementsByTagName('button').namedItem('clear')
const todayButton = calendar.getElementsByTagName('button').namedItem('today')
if (!showPicker.value) {
clearButton?.setAttribute('tabindex', '-1')
todayButton?.setAttribute('tabindex', '-1')
calendar.setAttribute('aria-hidden', 'true')
calendar.style.height = '0px'
nextTick(() => {
testFlags.set(`field-date-time-${props.context.id}.opened`)
})
} else {
if (!flatpickrHeight) {
// if form was initially rendered as hidden, the height will be 0
await recalculateHeight(datepicker.value)
}
calendar.style.height = `${flatpickrHeight}px`
calendar.removeAttribute('aria-hidden')
clearButton?.removeAttribute('tabindex')
todayButton?.removeAttribute('tabindex')
nextTick(() => {
testFlags.set(`field-date-time-${props.context.id}.closed`)
})
}
})
// hide calendar, if clicked outside of calendar or input
useEventListener('click', (e) => {
const { target } = e
if (!target || !datepicker.value || !showPicker.value || !pickerNode.value)
return
const calendarNode = datepicker.value.calendarContainer
const outsideOfCalendar = !calendarNode.contains(target as Node)
const outsideOfInput = !pickerNode.value.contains(target as Node)
if (outsideOfCalendar && outsideOfInput) {
showPicker.value = false
}
})
onBeforeUnmount(() => {
datepicker.value?.destroy?.()
})
</script>
<template>
<!-- TODO add placeholder support when styling will be finished -->
<div class="flex w-full ltr:pr-3 rtl:pl-3">
<input
:id="props.context.id"
ref="pickerNode"
:name="props.context.node.name"
:class="props.context.classes.input"
:disabled="(props.context.disabled as boolean)"
@blur="context.handlers.blur"
@focus="showPicker = true"
/>
</div>
<div v-show="showPicker" class="mx-2 w-full">
<div class="h-[1px] w-full bg-white/10"></div>
</div>
</template>
<style>
.flatpickr-calendar,
.flatpickr-current-month .flatpickr-monthDropdown-months,
.flatpickr-months .flatpickr-month,
span.flatpickr-weekday {
@apply bg-transparent;
}
.flatpickr-calendar {
box-shadow: none;
overflow: hidden;
}
.flatpickr-calendar.animate {
transition: height 0.5s;
}
.flatpickr-calendar:not([aria-hidden]) {
@apply mb-2;
}
span.flatpickr-weekday {
@apply font-light uppercase;
}
.flatpickr-day.selected:hover,
.flatpickr-day.selected {
background: none;
@apply border-blue bg-blue;
}
.flatpickr-months .flatpickr-prev-month.flatpickr-prev-month:hover,
.flatpickr-months .flatpickr-prev-month.flatpickr-prev-month {
left: unset;
@apply right-8 w-auto p-2;
}
.flatpickr-months .flatpickr-next-month.flatpickr-next-month:hover,
.flatpickr-months .flatpickr-next-month.flatpickr-next-month {
@apply w-auto p-2;
}
.flatpickr-months .flatpickr-prev-month:hover,
.flatpickr-months .flatpickr-next-month:hover,
.flatpickr-months .flatpickr-next-month,
.flatpickr-months .flatpickr-prev-month {
@apply text-blue;
}
.flatpickr-months .flatpickr-prev-month:hover svg,
.flatpickr-months .flatpickr-next-month:hover svg {
@apply fill-current;
}
.flatpickr-months .flatpickr-prev-month svg,
.flatpickr-months .flatpickr-next-month svg {
@apply h-5 w-5;
}
.flatpickr-months .flatpickr-current-month {
left: 5px;
@apply text-left text-sm;
}
.flatpickr-current-month .flatpickr-monthDropdown-months,
.flatpickr-current-month input.cur-year {
@apply font-bold;
}
</style>