0
0
Fork 0

Update input date/time behaviour

This commit is contained in:
Kevin Martinez 2024-03-04 22:22:23 -03:00
parent 043d592037
commit 98cf665126
2 changed files with 28 additions and 10 deletions

View File

@ -1,6 +1,7 @@
<script setup> <script setup>
import { computed, ref } from 'vue'; import { computed, ref } from 'vue';
import { toDate } from 'src/filters'; import { toDate } from 'src/filters';
import VnInput from 'components/common/VnInput.vue';
const props = defineProps({ const props = defineProps({
modelValue: { modelValue: {
@ -17,12 +18,25 @@ const props = defineProps({
}, },
}); });
const emit = defineEmits(['update:modelValue']); const emit = defineEmits(['update:modelValue']);
const joinDateAndTime = (date, time) => {
if (!date) {
return null;
}
if (!time) {
return new Date(date).toISOString();
}
const [year, month, day] = date.split('/');
return new Date(`${year}-${month}-${day}T${time}`).toISOString();
};
const time = computed(() => (props.modelValue ? props.modelValue.split('T')?.[1] : null));
const value = computed({ const value = computed({
get() { get() {
return props.modelValue; return props.modelValue;
}, },
set(value) { set(value) {
emit('update:modelValue', value ? new Date(value).toISOString() : null); emit('update:modelValue', joinDateAndTime(value, time.value));
}, },
}); });
@ -53,12 +67,12 @@ const styleAttrs = computed(() => {
</script> </script>
<template> <template>
<QInput <VnInput
class="vn-input-date" class="vn-input-date"
rounded
readonly
:model-value="toDate(value)" :model-value="toDate(value)"
v-bind="{ ...$attrs, ...styleAttrs }" v-bind="{ ...$attrs, ...styleAttrs }"
readonly
@click="isPopupOpen = true"
> >
<template #append> <template #append>
<QIcon name="event" class="cursor-pointer"> <QIcon name="event" class="cursor-pointer">
@ -76,7 +90,7 @@ const styleAttrs = computed(() => {
</QPopupProxy> </QPopupProxy>
</QIcon> </QIcon>
</template> </template>
</QInput> </VnInput>
</template> </template>
<style lang="scss"> <style lang="scss">

View File

@ -3,6 +3,7 @@ import { computed, ref } from 'vue';
import { toHour } from 'src/filters'; import { toHour } from 'src/filters';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import isValidDate from 'filters/isValidDate'; import isValidDate from 'filters/isValidDate';
import VnInput from "components/common/VnInput.vue";
const props = defineProps({ const props = defineProps({
modelValue: { modelValue: {
@ -26,8 +27,8 @@ const value = computed({
}, },
set(value) { set(value) {
const [hours, minutes] = value.split(':'); const [hours, minutes] = value.split(':');
const date = new Date(); const date = new Date(props.modelValue);
date.setUTCHours( date.setHours(
Number.parseInt(hours) || 0, Number.parseInt(hours) || 0,
Number.parseInt(minutes) || 0, Number.parseInt(minutes) || 0,
0, 0,
@ -37,8 +38,10 @@ const value = computed({
}, },
}); });
const isPopupOpen = ref(false);
const onDateUpdate = (date) => { const onDateUpdate = (date) => {
internalValue.value = date; internalValue.value = date;
isPopupOpen.value = false;
}; };
const save = () => { const save = () => {
@ -70,16 +73,17 @@ const styleAttrs = computed(() => {
</script> </script>
<template> <template>
<QInput <VnInput
class="vn-input-time" class="vn-input-time"
rounded
readonly readonly
:model-value="toHour(value)" :model-value="toHour(value)"
v-bind="{ ...$attrs, ...styleAttrs }" v-bind="{ ...$attrs, ...styleAttrs }"
@click="isPopupOpen = true"
> >
<template #append> <template #append>
<QIcon name="event" class="cursor-pointer"> <QIcon name="event" class="cursor-pointer">
<QPopupProxy <QPopupProxy
v-model="isPopupOpen"
cover cover
transition-show="scale" transition-show="scale"
transition-hide="scale" transition-hide="scale"
@ -109,7 +113,7 @@ const styleAttrs = computed(() => {
</QPopupProxy> </QPopupProxy>
</QIcon> </QIcon>
</template> </template>
</QInput> </VnInput>
</template> </template>
<style lang="scss"> <style lang="scss">