Skip to content

Instantly share code, notes, and snippets.

@asvae
Created July 17, 2017 19:41
Show Gist options
  • Save asvae/238d87a28c6e815e940468e5cde1b613 to your computer and use it in GitHub Desktop.
Save asvae/238d87a28c6e815e940468e5cde1b613 to your computer and use it in GitHub Desktop.
Time picker on vue 2 (stateful computed property example)
<template>
<div>
<vm-clock
v-if="! showMinutes"
type="hours"
v-model="hours"
@input="showMinutes = true"
>
<vm-am-pm-switch v-model="amPm"/>
</vm-clock>
<vm-clock
v-else
type="minutes"
v-model="minutes"
@input="$emit('submit')"
></vm-clock>
</div>
</template>
<script>
import Time from '../../../classes/Calendar/Time.js'
import vmClock from './Clock.vue'
import vmAmPmSwitch from './AmPmSwitch.vue'
export default {
data () {
return {
timeTemporary: null,
showMinutes: false,
}
},
props: {
value: {
type: Time,
},
},
computed: {
amPm: {
get () {
const time = this.timeTemporary || this.time
return time.getAmPm()
},
set (amPm) {
const time = this.timeTemporary || this.time
this.timeTemporary = time.setAmPm(amPm)
},
},
minutes: {
get () {
return this.time.getMinutes()
},
set (minutes) {
this.time = this.timeTemporary.setMinutes(minutes)
},
},
hours: {
get () {
const time = this.timeTemporary || this.time
return time.getHours12()
},
set (hours) {
const time = this.timeTemporary || this.time
this.timeTemporary = time.setHours12(hours)
},
},
time: {
get (): Time {
return this.value || new Time('12:00:00')
},
set (time: Time) {
this.$emit('input', time)
}
}
},
components: {
vmClock,
vmAmPmSwitch,
},
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment