-
Notifications
You must be signed in to change notification settings - Fork 0
feat: date range picker for charts #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| <template> | ||
| <UPopover :content="{ align: 'start' }" :modal="true"> | ||
| <UButton | ||
| color="neutral" | ||
| variant="outline" | ||
| icon="i-lucide-calendar" | ||
| size="lg" | ||
| class="data-[state=open]:bg-elevated group" | ||
| > | ||
| <span class="truncate"> | ||
| <template v-if="selected.start"> | ||
| <template v-if="selected.end"> | ||
| {{ df.format(selected.start) }} - {{ df.format(selected.end) }} | ||
| </template> | ||
| <template v-else> | ||
| {{ df.format(selected.start) }} | ||
| </template> | ||
| </template> | ||
| <template v-else> | ||
| Выберите даты | ||
| </template> | ||
| </span> | ||
|
|
||
| <template #trailing> | ||
| <UIcon name="i-lucide-chevron-down" class="shrink-0 text-dimmed size-5 group-data-[state=open]:rotate-180 transition-transform duration-200" /> | ||
| </template> | ||
| </UButton> | ||
|
|
||
| <template #content> | ||
| <div class="flex items-stretch sm:divide-x divide-default"> | ||
| <div class="hidden sm:flex flex-col justify-center"> | ||
| <UButton | ||
| v-for="(range, index) in ranges" | ||
| :key="index" | ||
| :label="range.label" | ||
| color="neutral" | ||
| variant="ghost" | ||
| class="rounded-none px-4" | ||
| :class="[isRangeSelected(range) ? 'bg-elevated' : 'hover:bg-elevated/50']" | ||
| truncate | ||
| @click="selectRange(range)" | ||
| /> | ||
| </div> | ||
|
|
||
| <UCalendar | ||
| v-model="calendarRange" | ||
| class="p-2" | ||
| :number-of-months="2" | ||
| range | ||
| /> | ||
| </div> | ||
| </template> | ||
| </UPopover> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import type { Range } from '#shared/types' | ||
| import { CalendarDate, DateFormatter, getLocalTimeZone, today } from '@internationalized/date' | ||
|
|
||
| const df = new DateFormatter('ru-RU', { | ||
| dateStyle: 'full', | ||
| }) | ||
|
|
||
| const selected = defineModel<Range>({ required: true }) | ||
|
|
||
| const ranges = [ | ||
| { label: 'Последние 7 дней', days: 7 - 1 }, | ||
| { label: 'Последние 14 дней', days: 14 - 1 }, | ||
| { label: 'Последние 30 дней', days: 30 - 1 }, | ||
| { label: 'Последние 3 месяца', days: 90 - 1 }, | ||
| { label: 'Последние 6 месяцев', days: 180 - 1 }, | ||
| { label: 'Последний год', days: 365 - 1 }, | ||
| ] | ||
|
|
||
| function toCalendarDate(date: Date) { | ||
| return new CalendarDate( | ||
| date.getFullYear(), | ||
| date.getMonth() + 1, | ||
| date.getDate(), | ||
| ) | ||
| } | ||
|
|
||
| const calendarRange = computed({ | ||
| get: () => ({ | ||
| start: selected.value.start ? toCalendarDate(selected.value.start) : undefined, | ||
| end: selected.value.end ? toCalendarDate(selected.value.end) : undefined, | ||
| }), | ||
| set: (newValue: { start: CalendarDate | null, end: CalendarDate | null }) => { | ||
| selected.value = { | ||
| start: newValue.start ? newValue.start.toDate(getLocalTimeZone()) : new Date(), | ||
| end: newValue.end ? newValue.end.toDate(getLocalTimeZone()) : new Date(), | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| function isRangeSelected(range: { days?: number, months?: number, years?: number }) { | ||
| if (!selected.value.start || !selected.value.end) { | ||
| return false | ||
| } | ||
|
|
||
| const currentDate = today(getLocalTimeZone()) | ||
| let startDate = currentDate.copy() | ||
|
|
||
| if (range.days) { | ||
| startDate = startDate.subtract({ days: range.days }) | ||
| } else if (range.months) { | ||
| startDate = startDate.subtract({ months: range.months }) | ||
| } else if (range.years) { | ||
| startDate = startDate.subtract({ years: range.years }) | ||
| } | ||
|
|
||
| const selectedStart = toCalendarDate(selected.value.start) | ||
| const selectedEnd = toCalendarDate(selected.value.end) | ||
|
|
||
| return selectedStart.compare(startDate) === 0 && selectedEnd.compare(currentDate) === 0 | ||
| } | ||
|
|
||
| function selectRange(range: { days?: number, months?: number, years?: number }) { | ||
| const endDate = today(getLocalTimeZone()) | ||
| let startDate = endDate.copy() | ||
|
|
||
| if (range.days) { | ||
| startDate = startDate.subtract({ days: range.days }) | ||
| } else if (range.months) { | ||
| startDate = startDate.subtract({ months: range.months }) | ||
| } else if (range.years) { | ||
| startDate = startDate.subtract({ years: range.years }) | ||
| } | ||
|
|
||
| selected.value = { | ||
| start: startDate.toDate(getLocalTimeZone()), | ||
| end: endDate.toDate(getLocalTimeZone()), | ||
| } | ||
| } | ||
| </script> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix null value handling in calendarRange setter.
The setter logic has an issue: when
newValue.startornewValue.endis null, it defaults tonew Date(). This means clearing a date selection unexpectedly sets it to the current date instead of keeping it unselected.Consider this fix to handle null values properly:
set: (newValue: { start: CalendarDate | null, end: CalendarDate | null }) => { selected.value = { - start: newValue.start ? newValue.start.toDate(getLocalTimeZone()) : new Date(), - end: newValue.end ? newValue.end.toDate(getLocalTimeZone()) : new Date(), + start: newValue.start ? newValue.start.toDate(getLocalTimeZone()) : null as any, + end: newValue.end ? newValue.end.toDate(getLocalTimeZone()) : null as any, } },Or update the Range interface to allow null values if that's the intended behavior.
📝 Committable suggestion
🤖 Prompt for AI Agents