Files
baserow/web-frontend/modules/builder/components/elements/baseComponents/ABDateTimePicker.vue
Jonathan Adelineandpeter_baserow 062a3f829d feat: add onClick and onChange events to more elements (#6686)
* feat(builder): add click events to images, text and headings

* fix(builder): allow element clicks to dismiss menus

* feat(builder): add click actions to columns and containers

* fix(builder): isolate nested interactive controls

* feat(builder): add selection change events

* test(builder): complete event fixtures for CI

* fix: keep heading semantics and limit click areas on clickable builder elements

---------

Co-authored-by: peter_baserow <peter@baserow.io>
2026-11-17 10:27:34 +04:00

270 lines
7.1 KiB
Vue

<template>
<div
ref="wrapper"
class="ab-datetime-picker__wrapper"
data-builder-interactive
>
<ABIcon
class="ab-datetime-picker__icon"
icon="iconoir-calendar"
@click="$refs.dateContext.show($refs.wrapper, 'bottom', 'left', 0)"
/>
<ABInput
v-model="dateInputValue"
class="ab-datetime-picker__input ab-datetime-picker__input--date"
:placeholder="dateFormat"
@focus="$refs.dateContext.toggle($refs.wrapper, 'bottom', 'left', 0)"
@click="$refs.dateContext.show($refs.wrapper, 'bottom', 'left', 0)"
@input="updateCalendar"
@keydown.enter.prevent="$event.target.blur()"
@blur="handleDateBlur($event)"
/>
<ABInput
v-if="includeTime"
ref="timeWrapper"
v-model="timeInputValue"
class="ab-datetime-picker__input ab-datetime-picker__input--time"
:placeholder="timeFormat"
@click="$refs.timeContext.show($refs.timeWrapper.$el, 'bottom', 'left')"
@focus="$refs.timeContext.show($refs.timeWrapper.$el, 'bottom', 'left')"
@keydown.enter.prevent="$event.target.blur()"
@blur="handleTimeBlur($event)"
/>
<div class="flex-grow-1" />
<ABIcon
v-if="dateInputValue"
class="ab-datetime-picker__icon"
icon="iconoir-cancel"
@click="clearValue"
/>
<Context
ref="dateContext"
:hide-on-click-outside="true"
:style="calendarStyle"
class="ab-datetime-picker__calendar--context"
>
<client-only>
<date-picker
v-model="calendarValue"
:open-date="calendarValue || new Date()"
:inline="true"
:monday-first="true"
:language="datePickerLanguage"
class="ab-datetime-picker__calendar"
@update:model-value="updateDate"
/>
</client-only>
</Context>
<Context
ref="timeContext"
:hide-on-click-outside="true"
class="ab-datetime-picker__clock"
:style="clockStyle"
>
<client-only>
<ul @mousedown="$event.preventDefault()">
<li v-for="time in getTimePickerTimes()" :key="time">
<a
:class="{ active: time === timeInputValue }"
@click="updateTime(time)"
>
{{ time }}
</a>
</li>
</ul>
</client-only>
</Context>
</div>
</template>
<script>
import { DATE_FORMATS, TIME_FORMATS } from '@baserow/modules/builder/enums'
import { useDatePickerLanguage } from '@baserow/modules/core/composables/useDatePickerLanguage'
import {
generateTimePickerTimes,
parseDateForCalendar,
updateDateTime,
} from '@baserow/modules/builder/date'
import { DateOnly } from '@baserow/modules/core/utils/date'
import moment from '@baserow/modules/core/moment'
export default {
name: 'ABDateTimePicker',
props: {
modelValue: {
type: [String, DateOnly, Date],
required: false,
default: null,
},
dateFormat: {
type: String,
required: false,
default: DATE_FORMATS.ISO.format,
},
timeFormat: {
type: String,
required: false,
default: TIME_FORMATS['24'].format,
},
includeTime: {
type: Boolean,
required: false,
default: false,
},
calendarStyle: {
type: Object,
required: false,
default: () => ({}),
},
clockStyle: {
type: Object,
required: false,
default: () => ({}),
},
},
emits: ['update:modelValue'],
setup() {
return useDatePickerLanguage()
},
data() {
return {
dateInputValue: '',
timeInputValue: '',
calendarValue: null,
}
},
watch: {
modelValue: {
handler(value) {
this.refreshDate(value)
},
immediate: true,
},
dateFormat: {
handler(value) {
this.refreshDate(this.modelValue)
},
immediate: true,
},
timeFormat: {
handler(value) {
this.refreshDate(this.modelValue)
},
immediate: true,
},
},
methods: {
refreshDate(value) {
if (!value) {
// The input values should be empty when the value is initialized
// or when the user wipes out the fields
this.dateInputValue = ''
this.timeInputValue = ''
this.calendarValue = null
} else {
const dateValue = moment(value)
if (!dateValue.isValid()) {
// The value is invalid
// It is not possible to display an invalid date in the calendar,
// so we clean it when the date is invalid
this.calendarValue = null
return
}
this.dateInputValue = dateValue.format(this.dateFormat)
this.timeInputValue = this.includeTime
? dateValue.format(this.timeFormat)
: null
this.calendarValue = dateValue.toDate()
}
},
/**
* Generate a list of times the user can choose from, spaced 30 min apart.
*/
getTimePickerTimes() {
return generateTimePickerTimes(this.timeFormat)
},
/**
* Update the current input 'value' with the given date.
* @param {Date|string} value - The date value (JS Date from picker, string from input).
*/
updateDate(value) {
// Update the input value even if the final date is invalid
const date = moment.utc(value, this.dateFormat, true)
if (date.isValid()) {
this.dateInputValue = date.format(this.dateFormat)
}
this.$emit(
'update:modelValue',
updateDateTime(
value,
this.timeInputValue,
this.includeTime,
false,
this.dateFormat,
this.timeFormat
)
)
},
/**
* Update the current input 'value' with the given time.
* @param {string} value - The string time value.
*/
updateTime(value) {
// Update the input value even if the final date is invalid
const date = moment.utc(value, this.timeFormat, true)
if (date.isValid()) {
this.timeInputValue = date.format(this.timeFormat)
}
this.timeInputValue = value
this.$emit(
'update:modelValue',
updateDateTime(
this.dateInputValue,
value,
this.includeTime,
true,
this.dateFormat,
this.timeFormat
)
)
this.$refs.timeContext.hide()
},
/**
* Update the calendar value based on a string date value.
* @param {string} value - The string date value.
*/
updateCalendar(value) {
this.calendarValue = parseDateForCalendar(value, this.dateFormat)
},
/**
* Handle blur event on the date input field.
* @param {Event} event - The blur event.
*/
handleDateBlur(event) {
this.updateDate(event.target.value)
this.$refs.dateContext?.hide()
},
/**
* Handle blur event on the time input field.
* @param {Event} event - The blur event.
*/
handleTimeBlur(event) {
this.updateTime(event.target.value)
this.$refs.timeContext.hide()
},
clearValue() {
this.$refs.dateContext.hide()
if (this.includeTime) {
this.$refs.timeContext.hide()
}
this.$emit('update:modelValue', null)
},
},
}
</script>