Skip to content

Commit f9220b8

Browse files
committed
Close #43 Close #46: v-model doesn't update the displayed value with Vuetify 2
1 parent 03f159e commit f9220b8

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuetify-datetime-picker",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"description": "DatetimePicker component for Vuetify.js.",
55
"main": "src/index.js",
66
"scripts": {
@@ -53,6 +53,6 @@
5353
"dependencies": {
5454
"date-fns": "^2.1.0",
5555
"vue": "^2.6.10",
56-
"vuetify": "^2.0.7"
56+
"vuetify": "^2.1.3"
5757
}
5858
}

src/app.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<v-flex xs4>
1717
<v-datetime-picker v-model="nullDatetime"></v-datetime-picker>
1818
</v-flex>
19+
<div>Datetime value: <span v-text="nullDatetime"></span></div>
1920
</v-expansion-panel-content>
2021
</v-expansion-panel>
2122

@@ -25,6 +26,7 @@
2526
<v-flex xs4>
2627
<v-datetime-picker v-model="datetime"></v-datetime-picker>
2728
</v-flex>
29+
<div>Datetime value: <span v-text="datetime"></span></div>
2830
</v-expansion-panel-content>
2931
</v-expansion-panel>
3032

@@ -34,6 +36,7 @@
3436
<v-flex xs4>
3537
<v-datetime-picker v-model="datetimeString"></v-datetime-picker>
3638
</v-flex>
39+
<div>Datetime value: <span v-text="datetimeString"></span></div>
3740
</v-expansion-panel-content>
3841
</v-expansion-panel>
3942

@@ -65,6 +68,7 @@
6568
<v-flex xs4>
6669
<v-datetime-picker v-model="datetime" disabled></v-datetime-picker>
6770
</v-flex>
71+
<div>Datetime value: <span v-text="datetime"></span></div>
6872
</v-expansion-panel-content>
6973
</v-expansion-panel>
7074

@@ -79,6 +83,7 @@
7983
</template>
8084
</v-datetime-picker>
8185
</v-flex>
86+
<div>Datetime value: <span v-text="datetime"></span></div>
8287
</v-expansion-panel-content>
8388
</v-expansion-panel>
8489

@@ -95,6 +100,7 @@
95100
</template>
96101
</v-datetime-picker>
97102
</v-flex>
103+
<div>Datetime value: <span v-text="datetime"></span></div>
98104
</v-expansion-panel-content>
99105
</v-expansion-panel>
100106

@@ -104,6 +110,7 @@
104110
<v-flex xs4>
105111
<v-datetime-picker v-model="formattedDatetime" date-format="MM/dd/yyyy"></v-datetime-picker>
106112
</v-flex>
113+
<div>Datetime value: <span v-text="formattedDatetime"></span></div>
107114
</v-expansion-panel-content>
108115
</v-expansion-panel>
109116

@@ -118,6 +125,7 @@
118125
time-format="HH:mm:ss"
119126
></v-datetime-picker>
120127
</v-flex>
128+
<div>Datetime value: <span v-text="datetime"></span></div>
121129
</v-expansion-panel-content>
122130
</v-expansion-panel>
123131

@@ -134,6 +142,7 @@
134142
time-format="HH:mm:ss"
135143
></v-datetime-picker>
136144
</v-flex>
145+
<div>Datetime value: <span v-text="datetime"></span></div>
137146
</v-expansion-panel-content>
138147
</v-expansion-panel>
139148
</v-expansion-panels>

src/components/DatetimePicker.vue

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</template>
2020

2121
<v-card>
22-
<v-card-text style="padding: 0;">
22+
<v-card-text class="px-0 py-0">
2323
<v-tabs fixed-tabs v-model="activeTab">
2424
<v-tab key="calendar">
2525
<slot name="dateIcon">
@@ -126,21 +126,8 @@ export default {
126126
time: DEFAULT_TIME
127127
}
128128
},
129-
created() {
130-
if (!this.datetime) {
131-
return
132-
}
133-
134-
let initDateTime
135-
if (this.datetime instanceof Date) {
136-
initDateTime = this.datetime
137-
} else if (typeof this.datetime === 'string' || this.datetime instanceof String) {
138-
// see https://stackoverflow.com/a/9436948
139-
initDateTime = parse(this.datetime, this.dateTimeFormat, new Date())
140-
}
141-
142-
this.date = format(initDateTime, DEFAULT_DATE_FORMAT)
143-
this.time = format(initDateTime, DEFAULT_TIME_FORMAT)
129+
mounted() {
130+
this.init()
144131
},
145132
computed: {
146133
dateTimeFormat() {
@@ -168,6 +155,22 @@ export default {
168155
}
169156
},
170157
methods: {
158+
init() {
159+
if (!this.datetime) {
160+
return
161+
}
162+
163+
let initDateTime
164+
if (this.datetime instanceof Date) {
165+
initDateTime = this.datetime
166+
} else if (typeof this.datetime === 'string' || this.datetime instanceof String) {
167+
// see https://stackoverflow.com/a/9436948
168+
initDateTime = parse(this.datetime, this.dateTimeFormat, new Date())
169+
}
170+
171+
this.date = format(initDateTime, DEFAULT_DATE_FORMAT)
172+
this.time = format(initDateTime, DEFAULT_TIME_FORMAT)
173+
},
171174
okHandler() {
172175
this.resetPicker()
173176
this.$emit('input', this.selectedDatetime)
@@ -188,6 +191,11 @@ export default {
188191
showTimePicker() {
189192
this.activeTab = 1
190193
}
194+
},
195+
watch: {
196+
datetime: function() {
197+
this.init()
198+
}
191199
}
192200
}
193201
</script>

0 commit comments

Comments
 (0)