2019-10-05 21:17:18 +02: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 17:19:30 +02:00
|
|
|
<template>
|
|
|
|
<b-field grouped horizontal :label="label">
|
2019-09-12 11:34:01 +02:00
|
|
|
<b-datepicker expanded v-model="date" :placeholder="$t('Click to select')" icon="calendar"></b-datepicker>
|
2019-06-07 17:19:30 +02: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-09-26 16:38:58 +02:00
|
|
|
@Component
|
2019-06-07 17:19:30 +02:00
|
|
|
export default class DateTimePicker extends Vue {
|
2019-10-05 21:17:18 +02:00
|
|
|
/**
|
|
|
|
* @model
|
|
|
|
* The DateTime value
|
|
|
|
*/
|
|
|
|
@Prop({ required: true, type: Date, default: () => new Date() }) value!: Date;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* What's shown besides the picker
|
|
|
|
*/
|
2019-06-07 17:19:30 +02:00
|
|
|
@Prop({ required: false, type: String, default: 'Datetime' }) label!: string;
|
2019-10-05 21:17:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The step for the time input
|
|
|
|
*/
|
2019-06-07 17:19:30 +02:00
|
|
|
@Prop({ required: false, type: Number, default: 1 }) step!: number;
|
|
|
|
|
|
|
|
date: Date = this.value;
|
|
|
|
time: string = '00:00';
|
|
|
|
|
|
|
|
created() {
|
|
|
|
let minutes = this.value.getHours() * 60 + this.value.getMinutes();
|
|
|
|
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 21:17:18 +02:00
|
|
|
updateTime(time: string) {
|
2019-06-07 17:19:30 +02:00
|
|
|
const [hours, minutes] = time.split(':', 2);
|
2019-10-05 21:17:18 +02:00
|
|
|
this.date.setHours(parseInt(hours, 10));
|
|
|
|
this.date.setMinutes(parseInt(minutes, 10));
|
2019-09-18 17:32:37 +02:00
|
|
|
this.updateDateTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Watch('date')
|
2019-10-05 21:17:18 +02:00
|
|
|
updateDate() {
|
2019-09-18 17:32:37 +02:00
|
|
|
this.updateDateTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDateTime() {
|
2019-10-05 21:17:18 +02:00
|
|
|
/**
|
|
|
|
* Returns the updated date
|
|
|
|
*
|
|
|
|
* @type {DateTime}
|
|
|
|
*/
|
2019-09-18 17:32:37 +02:00
|
|
|
this.$emit('input', this.date);
|
2019-06-07 17:19:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|