Files
docuseal/app/javascript/submission_form/date_step.vue
T

226 lines
5.8 KiB
Vue
Raw Normal View History

2023-10-26 21:26:08 +03:00
<template>
2024-01-07 14:19:21 +02:00
<div dir="auto">
2024-03-09 18:24:21 +02:00
<div
2025-02-02 12:30:01 +02:00
class="flex justify-between items-end w-full mb-3.5 sm:mb-4"
2024-03-09 18:24:21 +02:00
:class="{ 'mb-2': !field.description }"
>
2023-10-26 21:26:08 +03:00
<label
v-if="showFieldNames"
2023-10-26 21:26:08 +03:00
:for="field.uuid"
2025-03-18 17:34:11 +02:00
class="label text-xl sm:text-2xl py-0 field-name-label"
2024-03-09 18:24:21 +02:00
>
<MarkdownContent
v-if="field.title"
:string="field.title"
/>
<template v-else>
{{ field.name || t('date') }}
</template>
<template v-if="!field.required">
<span
class="ml-1"
:class="{ 'hidden sm:inline': (field.title || field.name || t('date') ).length > 10 }"
>
2024-06-13 14:24:37 +03:00
({{ t('optional') }})
</span>
2024-03-09 18:24:21 +02:00
</template>
2023-10-26 21:26:08 +03:00
</label>
<button
2025-08-20 18:19:23 +03:00
v-if="withToday"
2026-05-31 09:09:57 +03:00
type="button"
2025-03-18 17:34:11 +02:00
class="btn btn-outline btn-sm !normal-case font-normal set-current-date-button"
2024-04-08 18:34:27 +03:00
@click.prevent="[setCurrentDate(), $emit('focus')]"
2023-10-26 21:26:08 +03:00
>
2026-04-17 14:49:14 +03:00
<IconCalendarCheck
:width="16"
aria-hidden="true"
/>
2023-10-26 21:26:08 +03:00
{{ t('set_today') }}
</button>
</div>
2024-03-09 18:24:21 +02:00
<div
v-if="field.description"
2026-04-16 12:41:42 +03:00
:id="field.uuid + '-desc'"
2025-03-18 17:34:11 +02:00
class="mb-3 px-1 field-description-text"
2024-03-09 18:24:21 +02:00
dir="auto"
>
<MarkdownContent :string="field.description" />
</div>
2024-01-06 17:21:15 +02:00
<AppearsOn :field="field" />
2026-02-04 16:56:35 +02:00
<div class="text-center flex">
2023-10-26 21:26:08 +03:00
<input
2024-11-17 15:57:10 +02:00
:id="field.uuid"
2023-10-26 21:26:08 +03:00
ref="input"
v-model="value"
2025-08-20 18:19:23 +03:00
:min="validationMin"
:max="validationMax"
2023-10-26 21:26:08 +03:00
class="base-input !text-2xl text-center w-full"
:required="field.required"
2026-04-16 12:41:42 +03:00
:aria-describedby="field.description ? field.uuid + '-desc' : undefined"
2026-04-23 20:25:41 +03:00
:type="inputType"
:name="formatType === 'datetime' ? undefined : `values[${field.uuid}]`"
2024-08-01 18:47:17 +03:00
@keydown.enter="onEnter"
2023-10-26 21:26:08 +03:00
@focus="$emit('focus')"
2024-10-15 19:24:30 +03:00
@paste="onPaste"
2023-10-26 21:26:08 +03:00
>
2026-04-23 20:25:41 +03:00
<input
v-if="formatType === 'datetime'"
type="hidden"
:name="`values[${field.uuid}]`"
:value="modelValue"
>
2023-10-26 21:26:08 +03:00
</div>
</div>
</template>
<script>
import { IconCalendarCheck } from '@tabler/icons-vue'
2024-01-06 17:21:15 +02:00
import AppearsOn from './appears_on'
2024-03-09 18:24:21 +02:00
import MarkdownContent from './markdown_content'
2023-10-26 21:26:08 +03:00
export default {
name: 'DateStep',
components: {
2024-01-06 17:21:15 +02:00
IconCalendarCheck,
2024-03-09 18:24:21 +02:00
MarkdownContent,
2024-01-06 17:21:15 +02:00
AppearsOn
2023-10-26 21:26:08 +03:00
},
inject: ['t'],
props: {
field: {
type: Object,
required: true
},
2024-01-24 23:20:53 +02:00
showFieldNames: {
type: Boolean,
required: false,
default: true
},
2023-10-26 21:26:08 +03:00
modelValue: {
type: String,
required: false,
default: ''
}
},
2024-08-01 18:47:17 +03:00
emits: ['update:model-value', 'focus', 'submit'],
2023-10-26 21:26:08 +03:00
computed: {
2026-04-23 20:25:41 +03:00
formatType () {
const format = this.field.preferences?.format || ''
2025-08-20 18:19:23 +03:00
2026-04-23 20:25:41 +03:00
if (/[HhAasz]/.test(format)) return 'datetime'
if (format && !/[Dd]/.test(format)) return 'month'
2025-08-20 18:19:23 +03:00
2026-04-23 20:25:41 +03:00
return 'date'
},
inputType () {
return { datetime: 'datetime-local', month: 'month', date: 'date' }[this.formatType]
},
dateNowString () {
return this.formatDateValue(new Date())
2025-08-20 18:19:23 +03:00
},
validationMin () {
if (this.field.validation?.min) {
return ['{{date}}', '{date}'].includes(this.field.validation.min) ? this.dateNowString : this.field.validation.min
} else {
return ''
}
},
validationMax () {
if (this.field.validation?.max) {
return ['{{date}}', '{date}'].includes(this.field.validation.max) ? this.dateNowString : this.field.validation.max
} else {
return ''
}
},
withToday () {
2026-04-23 20:25:41 +03:00
if (this.formatType === 'datetime') return false
2025-08-20 18:19:23 +03:00
const todayDate = new Date().setHours(0, 0, 0, 0)
if (this.validationMin) {
if (new Date(this.validationMin).setHours(0, 0, 0, 0) <= todayDate) {
return this.validationMax ? (new Date(this.validationMax).setHours(0, 0, 0, 0) >= todayDate) : true
} else {
return false
}
} else if (this.validationMax) {
return new Date(this.validationMax).setHours(0, 0, 0, 0) >= todayDate
} else {
return true
}
},
2023-10-26 21:26:08 +03:00
value: {
set (value) {
2026-04-23 20:25:41 +03:00
if (this.formatType === 'datetime' && value) {
const d = new Date(value)
if (!isNaN(d)) {
this.$emit('update:model-value', d.toISOString())
return
}
}
2023-10-26 21:26:08 +03:00
this.$emit('update:model-value', value)
},
get () {
2026-04-23 20:25:41 +03:00
if (this.formatType === 'datetime') {
const d = new Date(this.modelValue)
return isNaN(d) ? '' : this.formatDateValue(d)
}
2023-10-26 21:26:08 +03:00
return this.modelValue
}
}
},
methods: {
2024-08-01 18:47:17 +03:00
onEnter (e) {
if (this.modelValue) {
e.preventDefault()
this.$emit('submit')
}
},
2024-10-15 19:24:30 +03:00
onPaste (e) {
e.preventDefault()
let pasteData = e.clipboardData.getData('text').trim()
if (pasteData.match(/^\d{2}\.\d{2}\.\d{4}$/)) {
pasteData = pasteData.split('.').reverse().join('-')
}
const parsedDate = new Date(pasteData)
2026-04-23 20:25:41 +03:00
if (isNaN(parsedDate)) return
2024-10-15 19:24:30 +03:00
2026-04-23 20:25:41 +03:00
this.setInputValue(parsedDate)
2024-10-15 19:24:30 +03:00
},
2023-10-26 21:26:08 +03:00
setCurrentDate () {
2026-04-23 20:25:41 +03:00
this.setInputValue(new Date())
},
setInputValue (date) {
2023-10-26 21:26:08 +03:00
const inputEl = this.$refs.input
2026-04-23 20:25:41 +03:00
if (this.formatType === 'date') {
inputEl.valueAsDate = new Date(date.getTime() - date.getTimezoneOffset() * 60000)
} else {
inputEl.value = this.formatDateValue(date)
}
2023-10-26 21:26:08 +03:00
inputEl.dispatchEvent(new Event('input', { bubbles: true }))
2026-04-23 20:25:41 +03:00
},
formatDateValue (date) {
const pad = (n) => String(n).padStart(2, '0')
const ymd = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
if (this.formatType === 'month') return ymd.slice(0, 7)
if (this.formatType === 'datetime') return `${ymd}T${pad(date.getHours())}:${pad(date.getMinutes())}`
return ymd
2023-10-26 21:26:08 +03:00
}
}
}
</script>