Skip to content

Commit 6c097e0

Browse files
committed
fix(NcDateTimePickerNative): do not set invalid values
Signed-off-by: Maksim Sukharev <[email protected]>
1 parent d29d6d3 commit 6c097e0

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

src/components/NcDateTimePickerNative/NcDateTimePickerNative.vue

+39-16
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,50 @@ All available types are: 'date', 'datetime-local', 'month', 'time' and 'week', p
1313

1414
### Examples
1515

16-
#### Usage: type='datetime-local'
1716
```vue
1817
<template>
1918
<div>
2019
<span>Picked date: {{ value || 'null' }}</span>
21-
<NcDateTimePickerNative
22-
v-model="value"
23-
:id="id"
24-
:label="label"
25-
type="datetime-local" />
20+
<hr/>
21+
<div class="flex">
22+
<NcSelect v-bind="props" v-model="type" />
23+
<NcDateTimePickerNative
24+
v-model="value"
25+
:id="id"
26+
:label="label"
27+
:type="type" />
28+
</div>
2629
</div>
2730
</template>
2831
<script>
2932
export default {
3033
data() {
3134
return {
35+
props: {
36+
clearable: false,
37+
inputLabel: 'Picker type',
38+
options: [
39+
'date',
40+
'datetime-local',
41+
'month',
42+
'time',
43+
'week',
44+
],
45+
},
46+
type: 'datetime-local',
3247
value: new Date(),
3348
id: 'date-time-picker',
34-
label: 'please select a new date',
49+
label: 'Select a new date or time',
3550
}
3651
},
3752
}
3853
</script>
54+
<style>
55+
.flex {
56+
display: flex;
57+
gap: 4px;
58+
}
59+
</style>
3960
```
4061

4162
#### Usage: type='datetime-local' with min date and max date
@@ -309,8 +330,7 @@ export default {
309330
input: ($event) => {
310331
if (isNaN($event.target.valueAsNumber)) {
311332
this.model = null
312-
}
313-
if (this.type === 'time') {
333+
} else if (this.type === 'time') {
314334
const time = $event.target.value
315335
if (this.model === '') {
316336
// this case is because of Chrome bug
@@ -321,21 +341,24 @@ export default {
321341
* @return {Date} new chosen Date()
322342
*/
323343
this.model = new Date(`${yyyy}-${MM}-${dd}T${time}`)
344+
} else {
345+
const { yyyy, MM, dd } = this.getReadableDate(this.model)
346+
this.model = new Date(`${yyyy}-${MM}-${dd}T${time}`)
324347
}
325-
const { yyyy, MM, dd } = this.getReadableDate(this.model)
326-
this.model = new Date(`${yyyy}-${MM}-${dd}T${time}`)
327348
} else if (this.type === 'month') {
328349
const MM = (new Date($event.target.value).getMonth() + 1).toString().padStart(2, '0')
329350
if (this.model === '') {
330351
const { yyyy, dd, hh, mm } = this.getReadableDate(new Date())
331352
this.model = new Date(`${yyyy}-${MM}-${dd}T${hh}:${mm}`)
353+
} else {
354+
const { yyyy, dd, hh, mm } = this.getReadableDate(this.model)
355+
this.model = new Date(`${yyyy}-${MM}-${dd}T${hh}:${mm}`)
332356
}
333-
const { yyyy, dd, hh, mm } = this.getReadableDate(this.model)
334-
this.model = new Date(`${yyyy}-${MM}-${dd}T${hh}:${mm}`)
357+
} else {
358+
const timezoneOffsetSeconds = new Date($event.target.valueAsNumber).getTimezoneOffset() * 1000 * 60
359+
const inputDateWithTimezone = $event.target.valueAsNumber + timezoneOffsetSeconds
360+
this.model = new Date(inputDateWithTimezone)
335361
}
336-
const timezoneOffsetSeconds = new Date($event.target.valueAsNumber).getTimezoneOffset() * 1000 * 60
337-
const inputDateWithTimezone = $event.target.valueAsNumber + timezoneOffsetSeconds
338-
this.model = new Date(inputDateWithTimezone)
339362
},
340363
}
341364
},

0 commit comments

Comments
 (0)