From 1e46849c784907fd3eff8f49b98027f26b529584 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 9 Jan 2022 14:05:23 +0100 Subject: [PATCH] feat: make sure date ranges work with date picker and vice-versa --- src/components/date/datepickerWithRange.vue | 70 +++++++++++++-------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/src/components/date/datepickerWithRange.vue b/src/components/date/datepickerWithRange.vue index 77af08ec..058c46c1 100644 --- a/src/components/date/datepickerWithRange.vue +++ b/src/components/date/datepickerWithRange.vue @@ -15,10 +15,10 @@ v-for="(value, text) in dateRanges" :key="text" @click="setDateRange(value)" - :class="{'is-active': dateRange === value}"> + :class="{'is-active': from === value[0] && to === value[1]}"> {{ $t(text) }} - @@ -27,7 +27,7 @@ {{ $t('input.datepickerRange.from') }}
- +
@@ -38,7 +38,7 @@ {{ $t('input.datepickerRange.to') }}
- +
@@ -47,7 +47,7 @@
@@ -83,10 +83,20 @@ const flatPickerConfig = computed(() => ({ }, })) -const dateRange = ref('') +const flatpickrRange = ref('') + +const from = ref('') +const to = ref('') + +function emitChanged() { + emit('dateChanged', { + dateFrom: from.value, + dateTo: to.value, + }) +} watch( - () => dateRange.value, + () => flatpickrRange.value, (newVal: string | null) => { if (newVal === null) { return @@ -98,30 +108,40 @@ watch( return } - emit('dateChanged', { - dateFrom: fromDate, - dateTo: toDate, - }) + from.value = fromDate + to.value = toDate + + emitChanged() }, ) -function setDateRange(range: string) { - dateRange.value = range +function setDateRange(range: string[] | null) { + if (range === null) { + from.value = '' + to.value = '' + emitChanged() + + return + } + + from.value = range[0] + to.value = range[1] + + emitChanged() } const dateRanges = { - // Still using strings for the range instead of an array or object to keep it compatible with the dates from flatpickr - 'task.show.today': 'now/d to now/d+1d', - 'task.show.thisWeek': 'now/w to now/w+1w', - 'task.show.nextWeek': 'now/w+1w to now/w+2w', - 'task.show.next7Days': 'now to now+7d', - 'task.show.thisMonth': 'now/M to now/M+1M', - 'task.show.nextMonth': 'now/M+1M to now/M+2M', - 'task.show.next30Days': 'now to now+30d', + 'task.show.today': ['now/d', 'now/d+1d'], + 'task.show.thisWeek': ['now/w', 'now/w+1w'], + 'task.show.nextWeek': ['now/w+1w', 'now/w+2w'], + 'task.show.next7Days': ['now', 'now+7d'], + 'task.show.thisMonth': ['now/M', 'now/M+1M'], + 'task.show.nextMonth': ['now/M+1M', 'now/M+2M'], + 'task.show.next30Days': ['now', 'now+30d'], } const customRangeActive = computed(() => { - return !Object.values(dateRanges).includes(dateRange.value) + return !Object.values(dateRanges).some(el => from.value === el[0] && to.value === el[1]) }) @@ -168,16 +188,16 @@ const customRangeActive = computed(() => { padding: 0; border: 0; } - + .field .control :deep(.button) { border: 1px solid var(--input-border-color); height: 2.25rem; - + &:hover { border: 1px solid var(--input-hover-border-color); } } - + .label, .input, :deep(.button) { font-size: .9rem; }