Skip to content

Commit ac44927

Browse files
pdanpdanrstoenescu
authored andcommitted
fix(QDatePicker): clone, isValid, inferDateFormat, add formatModel (quasarframework#1489)
1 parent 2493dec commit ac44927

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

dev/components/form/datetime.vue

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@
5151
<q-datetime inverted color="secondary" stack-label="Stack Label" v-model="model" type="date" />
5252
<q-datetime inverted color="amber" float-label="Float Label" v-model="model" type="date" />
5353

54+
<p class="caption">Format Model</p>
55+
<div class="bg-secondary text-white">
56+
Model: <em>{{modelVar}}</em> <strong>{{modelVarType}}</strong>
57+
</div>
58+
<div class="bg-secondary text-white">
59+
Formatted: <em>{{modelVarFormatted}}</em>
60+
</div>
61+
<q-datetime @change="value => log('@change', value)" @input="value => log('@input', value)" v-model="modelVar" type="date" clearable stack-label="Format Model 'auto'" format-model="auto" />
62+
<q-datetime @change="value => log('@change', value)" @input="value => log('@input', value)" v-model="modelVar" type="date" clearable stack-label="Format Model 'date'" format-model="date" />
63+
<q-datetime @change="value => log('@change', value)" @input="value => log('@input', value)" v-model="modelVar" type="date" clearable stack-label="Format Model 'number'" format-model="number" />
64+
<q-datetime @change="value => log('@change', value)" @input="value => log('@input', value)" v-model="modelVar" type="date" clearable stack-label="Format Model 'string'" format-model="string" />
65+
5466
<p class="caption">
5567
Lazy Input
5668
</p>
@@ -85,12 +97,12 @@
8597
<q-datetime v-model="model" popover type="date" float-label="Pick Date" />
8698
<q-datetime v-model="model" popover type="time" float-label="Pick Time" />
8799
<q-datetime v-model="model" popover type="datetime" float-label="Pick DateTime" />
88-
100+
89101
<p class="caption">With explicit modal</p>
90102
<q-datetime v-model="model" modal type="date" float-label="Pick Date" />
91103
<q-datetime v-model="model" modal type="time" float-label="Pick Time" />
92104
<q-datetime v-model="model" modal type="datetime" float-label="Pick DateTime" />
93-
105+
94106
<p class="caption">With Label</p>
95107
<q-datetime v-model="model" type="date" label="Pick Date" />
96108

@@ -204,6 +216,7 @@ export default {
204216
return {
205217
// model: '2016-09-18T10:45:00.000Z',
206218
model: undefined,
219+
modelVar: undefined,
207220
// model: 0,
208221
defaultSelection: '2016-09-18T10:45:00.000Z',
209222
@@ -218,6 +231,12 @@ export default {
218231
computed: {
219232
modelFormatted () {
220233
return date.formatDate(this.model, this.format)
234+
},
235+
modelVarFormatted () {
236+
return date.formatDate(this.modelVar, this.format)
237+
},
238+
modelVarType () {
239+
return typeof this.modelVar
221240
}
222241
},
223242
methods: {

src/components/datetime/QDatetime.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import { QInputFrame } from '../input-frame'
66
import { QPopover } from '../popover'
77
import QDatetimePicker from './QDatetimePicker'
88
import { QBtn } from '../btn'
9-
import { formatDate, isSameDate, isValid } from '../../utils/date'
9+
import { clone, formatDate, isSameDate, isValid } from '../../utils/date'
1010
import { QModal } from '../modal'
11-
import clone from '../../utils/clone'
1211

1312
const contentCss = __THEME__ === 'ios'
1413
? {
@@ -152,6 +151,7 @@ export default {
152151
type: this.type,
153152
min: this.min,
154153
max: this.max,
154+
formatModel: this.formatModel,
155155
format24h: this.format24h,
156156
firstDayOfWeek: this.firstDayOfWeek,
157157
defaultView: this.defaultView,

src/components/datetime/datetime-mixin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { inline as props } from './datetime-props'
33
import {
44
convertDateToFormat,
55
getDateBetween,
6+
inferDateFormat,
67
startOfDate,
78
isSameDate,
89
isValid
@@ -25,7 +26,7 @@ export default {
2526
},
2627
set (val) {
2728
const date = getDateBetween(val, this.pmin, this.pmax)
28-
const value = convertDateToFormat(date, this.value)
29+
const value = convertDateToFormat(date, this.formatModel === 'auto' ? inferDateFormat(this.value) : this.formatModel)
2930
this.$emit('input', value)
3031
this.$nextTick(() => {
3132
if (!isSameDate(value, this.value)) {

src/components/datetime/datetime-props.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ export const inline = {
1717
type: {
1818
type: String,
1919
default: 'date',
20-
validator (value) {
21-
return ['date', 'time', 'datetime'].includes(value)
22-
}
20+
validator: v => ['date', 'time', 'datetime'].includes(v)
2321
},
2422
color: {
2523
type: String,
@@ -34,6 +32,11 @@ export const inline = {
3432
default: null
3533
},
3634
firstDayOfWeek: Number,
35+
formatModel: {
36+
type: String,
37+
default: 'auto',
38+
validator: v => ['auto', 'date', 'number', 'string'].includes(v)
39+
},
3740
format24h: {
3841
type: [Boolean, Number],
3942
default: 0,

src/utils/date.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ function getChange (date, mod, add) {
4848
}
4949

5050
export function isValid (date) {
51+
if (typeof date === 'number') {
52+
return true
53+
}
5154
const t = Date.parse(date)
5255
return isNaN(t) === false
5356
}
@@ -208,19 +211,30 @@ export function getDayOfYear (date) {
208211
return getDateDiff(date, startOfDate(date, 'year'), 'days') + 1
209212
}
210213

211-
export function convertDateToFormat (date, example) {
212-
if (!date) {
213-
return
214-
}
215-
214+
export function inferDateFormat (example) {
216215
if (isDate(example)) {
217-
return date
216+
return 'date'
218217
}
219218
if (typeof example === 'number') {
220-
return date.getTime()
219+
return 'number'
220+
}
221+
222+
return 'string'
223+
}
224+
225+
export function convertDateToFormat (date, type) {
226+
if (!date && date !== 0) {
227+
return
221228
}
222229

223-
return formatDate(date)
230+
switch (type) {
231+
case 'date':
232+
return date
233+
case 'number':
234+
return date.getTime()
235+
default:
236+
return formatDate(date)
237+
}
224238
}
225239

226240
export function getDateBetween (date, min, max) {
@@ -520,3 +534,7 @@ export function formatDate (val, mask = 'YYYY-MM-DDTHH:mm:ss.SSSZ') {
520534
export function matchFormat (format = '') {
521535
return format.match(token)
522536
}
537+
538+
export function clone (value) {
539+
return isDate(value) ? new Date(value.getTime()) : value
540+
}

0 commit comments

Comments
 (0)