|
| 1 | +<template> |
| 2 | + <UPopover :content="{ align: 'start' }" :modal="true"> |
| 3 | + <UButton |
| 4 | + color="neutral" |
| 5 | + variant="outline" |
| 6 | + icon="i-lucide-calendar" |
| 7 | + size="lg" |
| 8 | + class="data-[state=open]:bg-elevated group" |
| 9 | + > |
| 10 | + <span class="truncate"> |
| 11 | + <template v-if="selected.start"> |
| 12 | + <template v-if="selected.end"> |
| 13 | + {{ df.format(selected.start) }} - {{ df.format(selected.end) }} |
| 14 | + </template> |
| 15 | + <template v-else> |
| 16 | + {{ df.format(selected.start) }} |
| 17 | + </template> |
| 18 | + </template> |
| 19 | + <template v-else> |
| 20 | + Выберите даты |
| 21 | + </template> |
| 22 | + </span> |
| 23 | + |
| 24 | + <template #trailing> |
| 25 | + <UIcon name="i-lucide-chevron-down" class="shrink-0 text-dimmed size-5 group-data-[state=open]:rotate-180 transition-transform duration-200" /> |
| 26 | + </template> |
| 27 | + </UButton> |
| 28 | + |
| 29 | + <template #content> |
| 30 | + <div class="flex items-stretch sm:divide-x divide-default"> |
| 31 | + <div class="hidden sm:flex flex-col justify-center"> |
| 32 | + <UButton |
| 33 | + v-for="(range, index) in ranges" |
| 34 | + :key="index" |
| 35 | + :label="range.label" |
| 36 | + color="neutral" |
| 37 | + variant="ghost" |
| 38 | + class="rounded-none px-4" |
| 39 | + :class="[isRangeSelected(range) ? 'bg-elevated' : 'hover:bg-elevated/50']" |
| 40 | + truncate |
| 41 | + @click="selectRange(range)" |
| 42 | + /> |
| 43 | + </div> |
| 44 | + |
| 45 | + <UCalendar |
| 46 | + v-model="calendarRange" |
| 47 | + class="p-2" |
| 48 | + :number-of-months="2" |
| 49 | + range |
| 50 | + /> |
| 51 | + </div> |
| 52 | + </template> |
| 53 | + </UPopover> |
| 54 | +</template> |
| 55 | + |
| 56 | +<script setup lang="ts"> |
| 57 | +import type { Range } from '#shared/types' |
| 58 | +import { CalendarDate, DateFormatter, getLocalTimeZone, today } from '@internationalized/date' |
| 59 | +
|
| 60 | +const df = new DateFormatter('ru-RU', { |
| 61 | + dateStyle: 'full', |
| 62 | +}) |
| 63 | +
|
| 64 | +const selected = defineModel<Range>({ required: true }) |
| 65 | +
|
| 66 | +const ranges = [ |
| 67 | + { label: 'Последние 7 дней', days: 7 - 1 }, |
| 68 | + { label: 'Последние 14 дней', days: 14 - 1 }, |
| 69 | + { label: 'Последние 30 дней', days: 30 - 1 }, |
| 70 | + { label: 'Последние 3 месяца', days: 90 - 1 }, |
| 71 | + { label: 'Последние 6 месяцев', days: 180 - 1 }, |
| 72 | + { label: 'Последний год', days: 365 - 1 }, |
| 73 | +] |
| 74 | +
|
| 75 | +function toCalendarDate(date: Date) { |
| 76 | + return new CalendarDate( |
| 77 | + date.getFullYear(), |
| 78 | + date.getMonth() + 1, |
| 79 | + date.getDate(), |
| 80 | + ) |
| 81 | +} |
| 82 | +
|
| 83 | +const calendarRange = computed({ |
| 84 | + get: () => ({ |
| 85 | + start: selected.value.start ? toCalendarDate(selected.value.start) : undefined, |
| 86 | + end: selected.value.end ? toCalendarDate(selected.value.end) : undefined, |
| 87 | + }), |
| 88 | + set: (newValue: { start: CalendarDate | null, end: CalendarDate | null }) => { |
| 89 | + selected.value = { |
| 90 | + start: newValue.start ? newValue.start.toDate(getLocalTimeZone()) : new Date(), |
| 91 | + end: newValue.end ? newValue.end.toDate(getLocalTimeZone()) : new Date(), |
| 92 | + } |
| 93 | + }, |
| 94 | +}) |
| 95 | +
|
| 96 | +function isRangeSelected(range: { days?: number, months?: number, years?: number }) { |
| 97 | + if (!selected.value.start || !selected.value.end) { |
| 98 | + return false |
| 99 | + } |
| 100 | +
|
| 101 | + const currentDate = today(getLocalTimeZone()) |
| 102 | + let startDate = currentDate.copy() |
| 103 | +
|
| 104 | + if (range.days) { |
| 105 | + startDate = startDate.subtract({ days: range.days }) |
| 106 | + } else if (range.months) { |
| 107 | + startDate = startDate.subtract({ months: range.months }) |
| 108 | + } else if (range.years) { |
| 109 | + startDate = startDate.subtract({ years: range.years }) |
| 110 | + } |
| 111 | +
|
| 112 | + const selectedStart = toCalendarDate(selected.value.start) |
| 113 | + const selectedEnd = toCalendarDate(selected.value.end) |
| 114 | +
|
| 115 | + return selectedStart.compare(startDate) === 0 && selectedEnd.compare(currentDate) === 0 |
| 116 | +} |
| 117 | +
|
| 118 | +function selectRange(range: { days?: number, months?: number, years?: number }) { |
| 119 | + const endDate = today(getLocalTimeZone()) |
| 120 | + let startDate = endDate.copy() |
| 121 | +
|
| 122 | + if (range.days) { |
| 123 | + startDate = startDate.subtract({ days: range.days }) |
| 124 | + } else if (range.months) { |
| 125 | + startDate = startDate.subtract({ months: range.months }) |
| 126 | + } else if (range.years) { |
| 127 | + startDate = startDate.subtract({ years: range.years }) |
| 128 | + } |
| 129 | +
|
| 130 | + selected.value = { |
| 131 | + start: startDate.toDate(getLocalTimeZone()), |
| 132 | + end: endDate.toDate(getLocalTimeZone()), |
| 133 | + } |
| 134 | +} |
| 135 | +</script> |
0 commit comments