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"
|
2020-11-15 18:47:05 +00:00
|
|
|
v-on:input="dateChanged"
|
2021-01-23 17:36:07 +00:00
|
|
|
:rules="rules"
|
2020-11-15 18:47:05 +00:00
|
|
|
>
|
|
|
|
<template v-slot: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">
|
|
|
|
<q-date :value="getDate()" mask="YYYY-MM-DD" v-on:input="dateChanged">
|
|
|
|
<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">
|
|
|
|
<q-time :value="getTime()" mask="HH:mm" v-on: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-23 17:36:07 +00:00
|
|
|
import { computed, defineComponent, ref } from '@vue/composition-api';
|
2020-11-15 18:47:05 +00:00
|
|
|
import { date } from 'quasar';
|
|
|
|
interface Props {
|
|
|
|
value?: Date;
|
|
|
|
label?: string;
|
|
|
|
readonly: boolean;
|
2021-01-23 17:36:07 +00:00
|
|
|
type: string;
|
2020-11-15 18:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'IsoDateInput',
|
|
|
|
props: {
|
|
|
|
value: {
|
2021-01-23 17:36:07 +00:00
|
|
|
required: true
|
2020-11-15 18:47:05 +00:00
|
|
|
},
|
|
|
|
label: {},
|
|
|
|
readonly: {
|
2021-01-23 17:36:07 +00:00
|
|
|
default: false
|
2020-11-15 18:47:05 +00:00
|
|
|
},
|
2021-01-23 17:36:07 +00:00
|
|
|
type: {
|
|
|
|
default: 'date',
|
|
|
|
validator: function(value: string) {
|
|
|
|
return ['date', 'time', 'datetime'].indexOf(value) !== -1;
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 18:47:05 +00:00
|
|
|
},
|
2021-01-23 17:36:07 +00:00
|
|
|
setup(props: Props, { emit }: { emit: any }) {
|
|
|
|
function getDateTime() {
|
|
|
|
if (props.value) {
|
|
|
|
switch (props.type) {
|
|
|
|
case 'date':
|
|
|
|
return getDate();
|
|
|
|
case 'time':
|
|
|
|
return getTime();
|
|
|
|
case 'datetime':
|
|
|
|
return `${getDate()} ${getTime()}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 18:47:05 +00:00
|
|
|
function getDate() {
|
|
|
|
if (props.value) {
|
|
|
|
return date.formatDate(props.value, 'YYYY-MM-DD');
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2021-01-23 17:36:07 +00:00
|
|
|
function getTime() {
|
|
|
|
if (props.value) {
|
|
|
|
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';
|
|
|
|
break;
|
|
|
|
case 'time':
|
|
|
|
return 'HH:mm';
|
|
|
|
case 'datetime':
|
|
|
|
return 'YYYY-MM-DD HH:mm';
|
|
|
|
}
|
|
|
|
});
|
2020-11-15 18:47:05 +00:00
|
|
|
|
|
|
|
function dateChanged(dateString: string) {
|
2021-01-23 17:36:07 +00:00
|
|
|
_date.value = dateString;
|
|
|
|
console.log('dateChanged', new Date(`${_date.value} ${_time.value}`));
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
emit('input', new Date(`${_date.value} ${_time.value}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
function timeChanged(timeString: string) {
|
|
|
|
_time.value = timeString;
|
|
|
|
if (_date.value == '') {
|
|
|
|
_date.value = date.formatDate(new Date(), 'YYYY-MM-DD');
|
|
|
|
}
|
|
|
|
console.log('timeChanged', new Date(`${_date.value} ${_time.value}`));
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
emit('input', new Date(`${_date.value} ${_time.value}`));
|
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.';
|
|
|
|
}
|
|
|
|
|
|
|
|
const rules = computed(() => {
|
|
|
|
switch (props.type) {
|
|
|
|
case 'date':
|
|
|
|
return [isDate];
|
|
|
|
break;
|
|
|
|
case 'time':
|
|
|
|
return [isTime];
|
|
|
|
break;
|
|
|
|
case 'datetime':
|
|
|
|
return [isDateTime];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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-23 17:36:07 +00:00
|
|
|
rules,
|
|
|
|
timeChanged,
|
|
|
|
placeholder
|
2020-11-15 18:47:05 +00:00
|
|
|
};
|
2021-01-23 17:36:07 +00:00
|
|
|
}
|
2020-11-15 18:47:05 +00:00
|
|
|
});
|
|
|
|
</script>
|