flaschengeist-frontend/src/components/utils/IsoDateInput.vue

153 lines
4.3 KiB
Vue
Raw Normal View History

2020-11-15 18:47:05 +00:00
<template>
<q-input
v-model="dateTime"
2020-11-15 18:47:05 +00:00
filled
:readonly="readonly"
:label="label"
:placeholder="placeholder"
:rules="customRules"
:clearable="clearable"
v-bind="attrs"
@clear="dateTime = ''"
2020-11-15 18:47:05 +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">
<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">
<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
v-if="type == 'time' || type == 'datetime'"
name="mdi-clock-outline"
2020-11-15 18:47:05 +00:00
class="cursor-pointer"
>
<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">
<q-btn v-close-popup label="Schließen" color="primary" flat />
2020-11-15 18:47:05 +00:00
</div>
</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';
import { date as q_date } from 'quasar';
import { stringIsDate, stringIsTime, stringIsDateTime, Validator } from 'src/utils/validators';
2020-11-15 18:47:05 +00:00
export default defineComponent({
name: 'IsoDateInput',
props: {
modelValue: { type: Object as PropType<Date | undefined>, default: undefined },
type: {
type: String,
default: 'date',
validator: (value: string) => ['date', 'time', 'datetime'].indexOf(value) !== -1,
2020-11-15 18:47:05 +00:00
},
label: { type: String, default: 'Datum' },
readonly: Boolean,
rules: {
type: Array as PropType<Validator[]>,
default: () => [],
},
2020-11-15 18:47:05 +00:00
},
emits: { 'update:modelValue': (date?: Date) => !!date || !date },
setup(props, { emit, attrs }) {
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)
);
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';
}
throw 'Invalid type given';
});
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-03 01:13:18 +00:00
});
const dateTime = computed({
get: () => (props.modelValue ? q_date.formatDate(props.modelValue, placeholder.value) : ''),
2021-02-03 01:13:18 +00:00
set: (v: string) => {
if (!v) emit('update:modelValue', undefined);
switch (props.type) {
case 'date':
2021-02-03 01:13:18 +00:00
date.value = v;
break;
case 'time':
2021-02-03 01:13:18 +00:00
time.value = v;
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-03 01:13:18 +00:00
break;
}
},
});
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-02-03 01:13:18 +00:00
function modifyDate(v: string, d: Date | undefined = props.modelValue) {
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],
});
}
2020-11-15 18:47:05 +00:00
}
return {
attrs,
clearable,
customRules,
2021-02-03 01:13:18 +00:00
date,
dateTime,
placeholder,
time,
2020-11-15 18:47:05 +00:00
};
},
2020-11-15 18:47:05 +00:00
});
</script>