2020-11-15 18:47:05 +00:00
|
|
|
<template>
|
|
|
|
<q-input
|
|
|
|
filled
|
|
|
|
:readonly="readonly"
|
|
|
|
:label="label"
|
2021-01-23 17:36:07 +00:00
|
|
|
:value="getDateTime()"
|
|
|
|
:placeholder="placeholder"
|
2021-01-31 16:09:29 +00:00
|
|
|
@input="dateTimeChanged"
|
2021-01-27 23:55:17 +00:00
|
|
|
:rules="customRules"
|
2020-11-15 18:47:05 +00:00
|
|
|
>
|
2021-01-31 16:09:29 +00:00
|
|
|
<template #append>
|
2021-01-23 17:36:07 +00:00
|
|
|
<q-icon name="event" class="cursor-pointer" v-if="type == 'date' || type == 'datetime'">
|
|
|
|
<q-popup-proxy ref="qDateProxy" transition-show="scale" transition-hide="scale">
|
2021-01-31 16:09:29 +00:00
|
|
|
<q-date :value="getDate()" mask="YYYY-MM-DD" @input="dateChanged">
|
2021-01-23 17:36:07 +00:00
|
|
|
<div class="row items-center justify-end">
|
|
|
|
<q-btn v-close-popup label="Schließen" color="primary" flat />
|
|
|
|
</div>
|
|
|
|
</q-date>
|
|
|
|
</q-popup-proxy>
|
|
|
|
</q-icon>
|
2020-11-15 18:47:05 +00:00
|
|
|
<q-icon
|
2021-01-23 17:36:07 +00:00
|
|
|
name="mdi-clock-outline"
|
2020-11-15 18:47:05 +00:00
|
|
|
class="cursor-pointer"
|
2021-01-23 17:36:07 +00:00
|
|
|
v-if="type == 'time' || type == 'datetime'"
|
2020-11-15 18:47:05 +00:00
|
|
|
>
|
2021-01-23 17:36:07 +00:00
|
|
|
<q-popup-proxy ref="qTimeProxy" transition-show="scale" transition-hide="scale">
|
2021-01-31 16:09:29 +00:00
|
|
|
<q-time :value="getTime()" mask="HH:mm" @input="timeChanged">
|
2020-11-15 18:47:05 +00:00
|
|
|
<div class="row items-center justify-end">
|
2021-01-23 17:36:07 +00:00
|
|
|
<q-btn v-close-popup label="Schließen" color="primary" flat />
|
2020-11-15 18:47:05 +00:00
|
|
|
</div>
|
2021-01-23 17:36:07 +00:00
|
|
|
</q-time>
|
2020-11-15 18:47:05 +00:00
|
|
|
</q-popup-proxy>
|
|
|
|
</q-icon>
|
|
|
|
</template>
|
|
|
|
</q-input>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2021-01-30 03:16:17 +00:00
|
|
|
import { computed, defineComponent, ref } from 'vue';
|
2020-11-15 18:47:05 +00:00
|
|
|
import { date } from 'quasar';
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'IsoDateInput',
|
|
|
|
props: {
|
2021-01-30 05:19:43 +00:00
|
|
|
value: { type: String, required: true },
|
|
|
|
type: {
|
|
|
|
type: String,
|
|
|
|
default: 'date',
|
|
|
|
validator: (value: string) => ['date', 'time', 'datetime'].indexOf(value) !== -1,
|
2020-11-15 18:47:05 +00:00
|
|
|
},
|
2021-01-30 05:19:43 +00:00
|
|
|
label: String,
|
2020-11-15 18:47:05 +00:00
|
|
|
readonly: {
|
2021-01-30 05:19:43 +00:00
|
|
|
type: Boolean,
|
2021-03-18 16:23:57 +00:00
|
|
|
default: false,
|
2020-11-15 18:47:05 +00:00
|
|
|
},
|
2021-01-30 05:19:43 +00:00
|
|
|
|
2021-01-27 23:55:17 +00:00
|
|
|
rules: {
|
2021-03-18 16:23:57 +00:00
|
|
|
default: () => {
|
|
|
|
return [];
|
|
|
|
},
|
|
|
|
},
|
2020-11-15 18:47:05 +00:00
|
|
|
},
|
2021-01-30 05:19:43 +00:00
|
|
|
setup(props, { emit }) {
|
2021-01-23 17:36:07 +00:00
|
|
|
function getDateTime() {
|
2021-01-27 23:55:17 +00:00
|
|
|
if (typeof props.value == 'object') {
|
2021-01-23 17:36:07 +00:00
|
|
|
switch (props.type) {
|
|
|
|
case 'date':
|
|
|
|
return getDate();
|
|
|
|
case 'time':
|
|
|
|
return getTime();
|
|
|
|
case 'datetime':
|
|
|
|
return `${getDate()} ${getTime()}`;
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 23:55:17 +00:00
|
|
|
return props.value;
|
2021-01-23 17:36:07 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 18:47:05 +00:00
|
|
|
function getDate() {
|
2021-01-27 23:55:17 +00:00
|
|
|
if (typeof props.value == 'object') {
|
2020-11-15 18:47:05 +00:00
|
|
|
return date.formatDate(props.value, 'YYYY-MM-DD');
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2021-01-23 17:36:07 +00:00
|
|
|
function getTime() {
|
2021-01-27 23:55:17 +00:00
|
|
|
if (typeof props.value == 'object') {
|
2021-01-23 17:36:07 +00:00
|
|
|
return date.formatDate(props.value, 'HH:mm');
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const _date = ref('');
|
|
|
|
const _time = ref('');
|
|
|
|
const placeholder = computed(() => {
|
|
|
|
switch (props.type) {
|
|
|
|
case 'date':
|
|
|
|
return 'YYYY-MM-DD';
|
|
|
|
case 'time':
|
|
|
|
return 'HH:mm';
|
|
|
|
case 'datetime':
|
|
|
|
return 'YYYY-MM-DD HH:mm';
|
|
|
|
}
|
2021-01-30 07:38:44 +00:00
|
|
|
throw 'Invalid type given';
|
2021-01-23 17:36:07 +00:00
|
|
|
});
|
2020-11-15 18:47:05 +00:00
|
|
|
|
|
|
|
function dateChanged(dateString: string) {
|
2021-01-23 17:36:07 +00:00
|
|
|
_date.value = dateString;
|
2021-01-26 20:54:16 +00:00
|
|
|
console.log(dateString);
|
|
|
|
|
|
|
|
if (/^\d{4}-\d\d-\d\d$/.test(dateString)) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
emit('input', new Date(`${_date.value} ${_time.value}`));
|
|
|
|
} else {
|
2021-01-27 23:55:17 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
emit('input', dateString);
|
2021-01-26 20:54:16 +00:00
|
|
|
}
|
2021-01-23 17:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function timeChanged(timeString: string) {
|
|
|
|
_time.value = timeString;
|
|
|
|
if (_date.value == '') {
|
|
|
|
_date.value = date.formatDate(new Date(), 'YYYY-MM-DD');
|
|
|
|
}
|
2021-01-26 20:54:16 +00:00
|
|
|
if (/^\d\d:\d\d$/.test(timeString)) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
emit('input', new Date(`${_date.value} ${_time.value}`));
|
2021-01-27 23:55:17 +00:00
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
emit('input', timeString);
|
2021-01-26 20:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function dateTimeChanged(dateTimeString: string) {
|
|
|
|
switch (props.type) {
|
|
|
|
case 'date':
|
|
|
|
dateChanged(dateTimeString);
|
|
|
|
break;
|
|
|
|
case 'time':
|
|
|
|
timeChanged(dateTimeString);
|
|
|
|
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');
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
emit('input', new Date(`${_date.value} ${_time.value}`));
|
2021-01-27 23:55:17 +00:00
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
emit('input', dateTimeString);
|
2021-01-26 20:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-15 18:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isDate(val: string) {
|
|
|
|
return !val || /^\d{4}-\d\d-\d\d$/.test(val) || 'Datum ist nicht gültig.';
|
|
|
|
}
|
|
|
|
|
2021-01-23 17:36:07 +00:00
|
|
|
function isTime(val: string) {
|
|
|
|
return !val || /^\d\d:\d\d$/.test(val) || 'Zeit ist nicht gültig.';
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDateTime(val: string) {
|
|
|
|
return !val || /^\d{4}-\d\d-\d\d \d\d:\d\d$/.test(val) || 'Datum und Zeit ist nicht gültig.';
|
|
|
|
}
|
|
|
|
|
2021-01-27 23:55:17 +00:00
|
|
|
// const customRules = computed(() => {
|
|
|
|
// const _rules = props.rules;
|
|
|
|
// switch (props.type) {
|
|
|
|
// case 'date':
|
|
|
|
// _rules.push(isDate);
|
|
|
|
// case 'time':
|
|
|
|
// _rules.push(isTime);
|
|
|
|
// case 'datetime':
|
|
|
|
// _rules.push(isDateTime);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
const customRules = [
|
|
|
|
props.type == 'date' ? isDate : props.type == 'time' ? isTime : isDateTime,
|
2021-03-18 16:23:57 +00:00
|
|
|
...props.rules,
|
2021-01-27 23:55:17 +00:00
|
|
|
];
|
2021-01-23 17:36:07 +00:00
|
|
|
|
2020-11-15 18:47:05 +00:00
|
|
|
return {
|
|
|
|
getDate,
|
2021-01-23 17:36:07 +00:00
|
|
|
getTime,
|
|
|
|
getDateTime,
|
2020-11-15 18:47:05 +00:00
|
|
|
dateChanged,
|
2021-01-27 23:55:17 +00:00
|
|
|
customRules,
|
2021-01-23 17:36:07 +00:00
|
|
|
timeChanged,
|
2021-01-26 20:54:16 +00:00
|
|
|
placeholder,
|
2021-03-18 16:23:57 +00:00
|
|
|
dateTimeChanged,
|
2020-11-15 18:47:05 +00:00
|
|
|
};
|
2021-03-18 16:23:57 +00:00
|
|
|
},
|
2020-11-15 18:47:05 +00:00
|
|
|
});
|
|
|
|
</script>
|