2019-10-05 19:17:18 +00:00
|
|
|
<docs>
|
|
|
|
### Datetime Picker
|
|
|
|
|
|
|
|
> We're wrapping the Buefy datepicker & an input
|
|
|
|
|
|
|
|
### Defaults
|
|
|
|
- step: 10
|
|
|
|
|
|
|
|
### Example
|
|
|
|
```vue
|
|
|
|
<DateTimePicker :value="new Date()" />
|
|
|
|
```
|
|
|
|
</docs>
|
2019-06-07 15:19:30 +00:00
|
|
|
<template>
|
|
|
|
<b-field grouped horizontal :label="label">
|
2019-10-11 16:41:29 +00:00
|
|
|
<b-datepicker
|
|
|
|
:day-names="localeShortWeekDayNamesProxy"
|
|
|
|
:month-names="localeMonthNamesProxy"
|
|
|
|
expanded
|
|
|
|
:first-day-of-week="parseInt($t('firstDayOfWeek'), 10)"
|
|
|
|
:min-date="minDate"
|
|
|
|
v-model="date"
|
|
|
|
:placeholder="$t('Click to select')"
|
|
|
|
icon="calendar"
|
|
|
|
/>
|
2019-06-07 15:19:30 +00:00
|
|
|
<b-input expanded type="time" required v-model="time" />
|
|
|
|
</b-field>
|
|
|
|
</template>
|
|
|
|
<script lang="ts">
|
|
|
|
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
|
2019-10-11 16:41:29 +00:00
|
|
|
import { localeMonthNames, localeShortWeekDayNames } from '@/utils/datetime';
|
|
|
|
|
2019-09-26 14:38:58 +00:00
|
|
|
@Component
|
2019-06-07 15:19:30 +00:00
|
|
|
export default class DateTimePicker extends Vue {
|
2019-10-05 19:17:18 +00:00
|
|
|
/**
|
|
|
|
* @model
|
|
|
|
* The DateTime value
|
|
|
|
*/
|
|
|
|
@Prop({ required: true, type: Date, default: () => new Date() }) value!: Date;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* What's shown besides the picker
|
|
|
|
*/
|
2019-06-07 15:19:30 +00:00
|
|
|
@Prop({ required: false, type: String, default: 'Datetime' }) label!: string;
|
2019-10-05 19:17:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The step for the time input
|
|
|
|
*/
|
2019-06-07 15:19:30 +00:00
|
|
|
@Prop({ required: false, type: Number, default: 1 }) step!: number;
|
|
|
|
|
2019-10-11 16:41:29 +00:00
|
|
|
/**
|
|
|
|
* Earliest date available for selection
|
|
|
|
*/
|
|
|
|
@Prop({ required: false, type: Date, default: null }) minDate!: Date;
|
|
|
|
|
2019-06-07 15:19:30 +00:00
|
|
|
date: Date = this.value;
|
|
|
|
time: string = '00:00';
|
|
|
|
|
2019-10-11 16:41:29 +00:00
|
|
|
localeShortWeekDayNamesProxy = localeShortWeekDayNames();
|
|
|
|
localeMonthNamesProxy = localeMonthNames();
|
|
|
|
|
2019-10-09 10:54:09 +00:00
|
|
|
mounted() {
|
|
|
|
this.convertTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
convertTime() {
|
|
|
|
let minutes = this.date.getHours() * 60 + this.date.getMinutes();
|
2019-06-07 15:19:30 +00:00
|
|
|
minutes = Math.ceil(minutes / this.step) * this.step;
|
|
|
|
|
|
|
|
this.time = [Math.floor(minutes / 60), minutes % 60].map((v) => { return v < 10 ? `0${v}` : v; }).join(':');
|
|
|
|
}
|
|
|
|
|
|
|
|
@Watch('time')
|
2019-10-05 19:17:18 +00:00
|
|
|
updateTime(time: string) {
|
2019-06-07 15:19:30 +00:00
|
|
|
const [hours, minutes] = time.split(':', 2);
|
2019-10-05 19:17:18 +00:00
|
|
|
this.date.setHours(parseInt(hours, 10));
|
|
|
|
this.date.setMinutes(parseInt(minutes, 10));
|
2019-09-18 15:32:37 +00:00
|
|
|
this.updateDateTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Watch('date')
|
2019-10-05 19:17:18 +00:00
|
|
|
updateDate() {
|
2019-10-09 10:54:09 +00:00
|
|
|
this.updateTime(this.time);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Watch('value')
|
|
|
|
updateValue() {
|
|
|
|
this.date = this.value;
|
|
|
|
this.convertTime();
|
2019-09-18 15:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateDateTime() {
|
2019-10-05 19:17:18 +00:00
|
|
|
/**
|
|
|
|
* Returns the updated date
|
|
|
|
*
|
2019-10-09 10:54:09 +00:00
|
|
|
* @type {Date}
|
2019-10-05 19:17:18 +00:00
|
|
|
*/
|
2019-09-18 15:32:37 +00:00
|
|
|
this.$emit('input', this.date);
|
2019-06-07 15:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|