scinote-web/app/javascript/vue/shared/date_time_picker.vue

77 lines
1.8 KiB
Vue
Raw Normal View History

<template>
2023-08-28 19:10:02 +08:00
<div class="date-time-picker grow">
2023-10-25 19:16:34 +08:00
<VueDatePicker
v-if="mode == 'datetime'"
v-model="datetime"
:teleport="true"
:format="dateTimeFormat"
:placeholder="placeholder" ></VueDatePicker>
<VueDatePicker
v-if="mode == 'date'"
v-model="datetime"
:teleport="true"
:format="dateFormat"
:enable-time-picker="false"
:placeholder="placeholder" ></VueDatePicker>
<VueDatePicker
v-if="mode == 'time'"
v-model="datetime"
:teleport="true"
:format="timeFormat"
time-picker
:placeholder="placeholder" ></VueDatePicker>
</div>
</template>
<script>
2023-10-25 19:16:34 +08:00
import VueDatePicker from '@vuepic/vue-datepicker';
export default {
name: 'DateTimePicker',
props: {
2023-10-25 19:16:34 +08:00
mode: { type: String, default: 'datetime' },
2023-08-23 17:59:21 +08:00
defaultValue: { type: Date, required: false },
placeholder: { type: String }
},
data() {
return {
2023-10-25 19:16:34 +08:00
datetime: this.defaultValue
}
},
2023-10-30 22:14:12 +08:00
watch: {
defaultValue: function () {
this.datetime = this.defaultValue;
}
},
components: {
2023-10-25 19:16:34 +08:00
VueDatePicker
},
2023-10-25 19:16:34 +08:00
watch: {
datetime: function () {
if (this.defaultValue != this.datetime) {
let newDate = this.datetime;
if (this.mode == 'time') {
newDate = new Date();
newDate.setHours(this.datetime.hours);
newDate.setMinutes(this.datetime.minutes);
}
2023-10-25 19:16:34 +08:00
this.$emit('change', newDate);
}
}
2023-10-25 19:16:34 +08:00
},
computed: {
dateTimeFormat() {
return `${document.body.dataset.datetimePickerFormatVue} HH:mm`
},
dateFormat() {
return document.body.dataset.datetimePickerFormatVue
},
timeFormat() {
return 'HH:mm'
}
}
}
</script>