2020-11-15 18:47:05 +00:00
|
|
|
<template>
|
|
|
|
<q-input
|
2021-02-02 00:28:23 +00:00
|
|
|
v-model="dateTime"
|
2020-11-15 18:47:05 +00:00
|
|
|
filled
|
|
|
|
:readonly="readonly"
|
|
|
|
:label="label"
|
2021-01-23 17:36:07 +00:00
|
|
|
:placeholder="placeholder"
|
2021-01-27 23:55:17 +00:00
|
|
|
:rules="customRules"
|
2021-03-20 16:11:51 +00:00
|
|
|
:clearable="clearable"
|
2021-03-31 15:22:55 +00:00
|
|
|
v-bind="attrs"
|
2021-03-20 16:11:51 +00:00
|
|
|
@clear="dateTime = ''"
|
2020-11-15 18:47:05 +00:00
|
|
|
>
|
2021-01-31 16:09:29 +00:00
|
|
|
<template #append>
|
2021-04-03 11:24:19 +00:00
|
|
|
<q-icon v-if="'date' || type == 'datetime'" name="mdi-calendar" class="cursor-pointer">
|
2021-01-23 17:36:07 +00:00
|
|
|
<q-popup-proxy ref="qDateProxy" transition-show="scale" transition-hide="scale">
|
2021-02-03 01:13:18 +00:00
|
|
|
<q-date v-model="date" mask="YYYY-MM-DD">
|
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-02-01 23:48:33 +00:00
|
|
|
v-if="type == 'time' || type == 'datetime'"
|
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
|
|
|
<q-popup-proxy ref="qTimeProxy" transition-show="scale" transition-hide="scale">
|
2021-02-03 01:13:18 +00:00
|
|
|
<q-time v-model="time" mask="HH:mm">
|
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-02-03 01:13:18 +00:00
|
|
|
import { computed, defineComponent, PropType } from 'vue';
|
2021-02-02 00:28:23 +00:00
|
|
|
import { date as q_date } from 'quasar';
|
2021-03-22 02:24:27 +00:00
|
|
|
import { stringIsDate, stringIsTime, stringIsDateTime, Validator } from 'src/utils/validators';
|
2020-11-15 18:47:05 +00:00
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'IsoDateInput',
|
|
|
|
props: {
|
2021-03-20 16:11:51 +00:00
|
|
|
modelValue: { type: Object as PropType<Date | undefined>, default: undefined },
|
2021-01-30 05:19:43 +00:00
|
|
|
type: {
|
|
|
|
type: String,
|
|
|
|
default: 'date',
|
|
|
|
validator: (value: string) => ['date', 'time', 'datetime'].indexOf(value) !== -1,
|
2020-11-15 18:47:05 +00:00
|
|
|
},
|
2021-01-31 21:21:49 +00:00
|
|
|
label: { type: String, default: 'Datum' },
|
|
|
|
readonly: Boolean,
|
2021-01-27 23:55:17 +00:00
|
|
|
rules: {
|
2021-03-20 16:11:51 +00:00
|
|
|
type: Array as PropType<Validator[]>,
|
2021-01-31 21:21:49 +00:00
|
|
|
default: () => [],
|
2021-03-18 16:23:57 +00:00
|
|
|
},
|
2020-11-15 18:47:05 +00:00
|
|
|
},
|
2021-03-20 16:11:51 +00:00
|
|
|
emits: { 'update:modelValue': (date?: Date) => !!date || !date },
|
2021-03-31 15:22:55 +00:00
|
|
|
setup(props, { emit, attrs }) {
|
2021-03-20 16:11:51 +00:00
|
|
|
const customRules = computed(() => [
|
|
|
|
props.type == 'date' ? stringIsDate : props.type == 'time' ? stringIsTime : stringIsDateTime,
|
|
|
|
...props.rules,
|
|
|
|
]);
|
|
|
|
|
|
|
|
const clearable = computed(() =>
|
|
|
|
customRules.value.every((r) => (<Validator>r)(undefined) === true)
|
|
|
|
);
|
|
|
|
|
2021-01-23 17:36:07 +00:00
|
|
|
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
|
|
|
|
2021-02-03 01:13:18 +00:00
|
|
|
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);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const time = computed({
|
|
|
|
get: () => q_date.formatDate(props.modelValue, 'HH:mm'),
|
|
|
|
set: (v: string) => {
|
|
|
|
const d = modifyTime(v);
|
|
|
|
if (d) emit('update:modelValue', d);
|
2021-02-02 00:28:23 +00:00
|
|
|
},
|
2021-02-03 01:13:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const dateTime = computed({
|
2021-03-20 16:11:51 +00:00
|
|
|
get: () => (props.modelValue ? q_date.formatDate(props.modelValue, placeholder.value) : ''),
|
2021-02-03 01:13:18 +00:00
|
|
|
set: (v: string) => {
|
2021-03-20 16:11:51 +00:00
|
|
|
if (!v) emit('update:modelValue', undefined);
|
2021-02-02 00:28:23 +00:00
|
|
|
switch (props.type) {
|
|
|
|
case 'date':
|
2021-02-03 01:13:18 +00:00
|
|
|
date.value = v;
|
2021-02-02 00:28:23 +00:00
|
|
|
break;
|
|
|
|
case 'time':
|
2021-02-03 01:13:18 +00:00
|
|
|
time.value = v;
|
2021-02-02 00:28:23 +00:00
|
|
|
break;
|
|
|
|
case 'datetime':
|
2021-02-03 01:13:18 +00:00
|
|
|
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);
|
2021-02-02 00:28:23 +00:00
|
|
|
}
|
2021-02-03 01:13:18 +00:00
|
|
|
break;
|
2021-02-02 00:28:23 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-02-03 01:13:18 +00:00
|
|
|
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] });
|
2021-01-26 20:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 01:13:18 +00:00
|
|
|
function modifyDate(v: string, d: Date | undefined = props.modelValue) {
|
2021-03-20 16:11:51 +00:00
|
|
|
if (!d) d = new Date();
|
|
|
|
if (/^\d{4}-\d\d-\d\d$/.test(v)) {
|
2021-02-03 01:13:18 +00:00
|
|
|
const split = v.split('-');
|
|
|
|
return q_date.adjustDate(d, {
|
|
|
|
year: +split[0],
|
|
|
|
month: +split[1],
|
|
|
|
date: +split[2],
|
|
|
|
});
|
2021-02-02 00:28:23 +00:00
|
|
|
}
|
2020-11-15 18:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-03-31 15:22:55 +00:00
|
|
|
attrs,
|
2021-03-20 16:11:51 +00:00
|
|
|
clearable,
|
2021-03-31 15:22:55 +00:00
|
|
|
customRules,
|
2021-02-03 01:13:18 +00:00
|
|
|
date,
|
2021-02-02 00:28:23 +00:00
|
|
|
dateTime,
|
2021-01-26 20:54:16 +00:00
|
|
|
placeholder,
|
2021-03-31 15:22:55 +00:00
|
|
|
time,
|
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>
|