Fixed IsoDateInput

This commit is contained in:
Ferdinand Thiessen 2021-02-03 02:13:18 +01:00
parent 17ffd19c5b
commit 9967296698
1 changed files with 44 additions and 66 deletions

View File

@ -10,7 +10,7 @@
<template #append>
<q-icon v-if="type == 'date' || type == 'datetime'" name="event" class="cursor-pointer">
<q-popup-proxy ref="qDateProxy" transition-show="scale" transition-hide="scale">
<q-date :model-value="getDate()" mask="YYYY-MM-DD" @input="dateChanged">
<q-date v-model="date" mask="YYYY-MM-DD">
<div class="row items-center justify-end">
<q-btn v-close-popup label="Schließen" color="primary" flat />
</div>
@ -23,7 +23,7 @@
class="cursor-pointer"
>
<q-popup-proxy ref="qTimeProxy" transition-show="scale" transition-hide="scale">
<q-time :model-value="getTime()" mask="HH:mm" @update:modelValue="timeChanged">
<q-time v-model="time" mask="HH:mm">
<div class="row items-center justify-end">
<q-btn v-close-popup label="Schließen" color="primary" flat />
</div>
@ -35,13 +35,13 @@
</template>
<script lang="ts">
import { computed, defineComponent, ref, PropType } from 'vue';
import { computed, defineComponent, PropType } from 'vue';
import { date as q_date } from 'quasar';
export default defineComponent({
name: 'IsoDateInput',
props: {
modelValue: { type: Date, default: new Date() },
modelValue: { type: Object as PropType<Date>, default: new Date() },
type: {
type: String,
default: 'date',
@ -54,10 +54,8 @@ export default defineComponent({
default: () => [],
},
},
emits: { input: (date: string) => date.length > 5 },
emits: { 'update:modelValue': (date: Date) => !!date },
setup(props, { emit }) {
const _date = ref('');
const _time = ref('');
const placeholder = computed(() => {
switch (props.type) {
case 'date':
@ -70,77 +68,59 @@ export default defineComponent({
throw 'Invalid type given';
});
const dateTime = computed({
get() {
if (typeof props.modelValue == 'object') {
switch (props.type) {
case 'date':
return getDate();
case 'time':
return getTime();
case 'datetime':
return `${getDate()} ${getTime()}`;
}
}
return props.modelValue;
const date = computed({
get: () => q_date.formatDate(props.modelValue, 'YYYY-MM-DD'),
set: (v: string) => {
const d = modifyDate(v);
if (d) emit('update:modelValue', d);
},
set(dateTimeString: string) {
});
const time = computed({
get: () => q_date.formatDate(props.modelValue, 'HH:mm'),
set: (v: string) => {
const d = modifyTime(v);
if (d) emit('update:modelValue', d);
},
});
const dateTime = computed({
get: () => q_date.formatDate(props.modelValue, placeholder.value),
set: (v: string) => {
switch (props.type) {
case 'date':
dateChanged(dateTimeString);
date.value = v;
break;
case 'time':
timeChanged(dateTimeString);
time.value = v;
break;
case 'datetime':
const _dateTime = dateTimeString.split(' ');
_date.value = _dateTime[0];
_time.value = _dateTime[1];
console.log(dateTimeString, _dateTime);
if (/^\d{4}-\d\d-\d\d \d\d:\d\d$/.test(dateTimeString)) {
console.log('dateTimeChanged');
emit('input', new Date(`${_date.value} ${_time.value}`).toISOString());
} else {
emit('input', dateTimeString);
const split = v.split(' ').filter((c) => c !== '');
if (split.length == 2) {
const d = modifyTime(split[1], modifyDate(split[0]));
if (d) emit('update:modelValue', d);
}
break;
}
},
});
function dateChanged(dateString: string) {
_date.value = dateString;
console.log(dateString);
if (/^\d{4}-\d\d-\d\d$/.test(dateString)) {
emit('input', new Date(`${_date.value} ${_time.value}`).toISOString());
} else {
emit('input', dateString);
function modifyTime(v: string, d: Date | undefined = props.modelValue) {
if (d && /^\d\d:\d\d$/.test(v)) {
const split = v.split(':');
return q_date.adjustDate(d, { hours: +split[0], minutes: +split[1] });
}
}
function timeChanged(timeString: string) {
_time.value = timeString;
if (_date.value == '') {
_date.value = q_date.formatDate(new Date(), 'YYYY-MM-DD');
function modifyDate(v: string, d: Date | undefined = props.modelValue) {
if (d && /^\d{4}-\d\d-\d\d$/.test(v)) {
const split = v.split('-');
return q_date.adjustDate(d, {
year: +split[0],
month: +split[1],
date: +split[2],
});
}
if (/^\d\d:\d\d$/.test(timeString)) {
emit('input', new Date(`${_date.value} ${_time.value}`).toISOString());
} else {
emit('input', timeString);
}
}
function getDate() {
if (typeof props.modelValue == 'object') {
return q_date.formatDate(props.modelValue, 'YYYY-MM-DD');
}
return '';
}
function getTime() {
if (typeof props.modelValue == 'object') {
return q_date.formatDate(props.modelValue, 'HH:mm');
}
return '';
}
function isDate(val: string) {
@ -161,12 +141,10 @@ export default defineComponent({
];
return {
date,
time,
dateTime,
getDate,
getTime,
dateChanged,
customRules,
timeChanged,
placeholder,
};
},