2021-12-10 21:12:06 +08:00
|
|
|
<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>
|
2021-12-10 21:12:06 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-10-25 19:16:34 +08:00
|
|
|
import VueDatePicker from '@vuepic/vue-datepicker';
|
2021-12-15 19:22:24 +08:00
|
|
|
|
2021-12-10 21:12:06 +08:00
|
|
|
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 }
|
2021-12-10 21:12:06 +08:00
|
|
|
},
|
2021-12-15 19:22:24 +08:00
|
|
|
data() {
|
|
|
|
return {
|
2023-10-25 19:16:34 +08:00
|
|
|
datetime: this.defaultValue
|
2021-12-15 19:22:24 +08:00
|
|
|
}
|
|
|
|
},
|
2023-10-30 22:14:12 +08:00
|
|
|
watch: {
|
|
|
|
defaultValue: function () {
|
|
|
|
this.datetime = this.defaultValue;
|
|
|
|
}
|
|
|
|
},
|
2021-12-15 19:22:24 +08:00
|
|
|
components: {
|
2023-10-25 19:16:34 +08:00
|
|
|
VueDatePicker
|
2021-12-10 21:12:06 +08:00
|
|
|
},
|
2023-10-25 19:16:34 +08:00
|
|
|
watch: {
|
|
|
|
datetime: function () {
|
2023-11-02 20:35:10 +08:00
|
|
|
if ( this.datetime == null) {
|
|
|
|
this.$emit('cleared');
|
|
|
|
}
|
|
|
|
|
2023-10-25 19:16:34 +08:00
|
|
|
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);
|
2022-02-21 18:10:50 +08:00
|
|
|
}
|
2023-10-25 19:16:34 +08:00
|
|
|
this.$emit('change', newDate);
|
2021-12-15 19:22:24 +08:00
|
|
|
}
|
2021-12-10 21:12:06 +08:00
|
|
|
}
|
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'
|
|
|
|
}
|
2021-12-10 21:12:06 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-25 22:27:31 +08:00
|
|
|
</script>
|