-
Notifications
You must be signed in to change notification settings - Fork 1k
docs(calendar): improve date range picker example #6334
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
+60
−21
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b613022
docs(InputDate): add date range filter example
onmax 30025a1
fix off-by-one in preset date ranges
onmax 221661c
move date range picker example to calendar.md
onmax 06b41ec
responsive: hide presets, 1 month on mobile
onmax 3769a5d
fix coderabbit review comments
onmax d451c05
use tailwind breakpoints, center presets
onmax 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
74 changes: 54 additions & 20 deletions
74
docs/app/components/content/examples/calendar/CalendarDateRangePickerExample.vue
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 |
|---|---|---|
| @@ -1,35 +1,69 @@ | ||
| <script setup lang="ts"> | ||
| import { CalendarDate, DateFormatter, getLocalTimeZone } from '@internationalized/date' | ||
| import { DateFormatter, getLocalTimeZone, today } from '@internationalized/date' | ||
|
|
||
| const df = new DateFormatter('en-US', { | ||
| dateStyle: 'medium' | ||
| }) | ||
| const df = new DateFormatter('en-US', { dateStyle: 'medium' }) | ||
| const tz = getLocalTimeZone() | ||
|
|
||
| const ranges = [ | ||
| { label: 'Last 7 days', days: 7 }, | ||
| { label: 'Last 14 days', days: 14 }, | ||
| { label: 'Last 30 days', days: 30 }, | ||
| { label: 'Last 3 months', months: 3 }, | ||
| { label: 'Last 6 months', months: 6 }, | ||
| { label: 'Last year', years: 1 } | ||
| ] | ||
|
|
||
| const modelValue = shallowRef({ | ||
| start: new CalendarDate(2022, 1, 20), | ||
| end: new CalendarDate(2022, 2, 10) | ||
| start: today(tz).subtract({ days: 14 }), | ||
| end: today(tz) | ||
| }) | ||
|
|
||
| const label = computed(() => { | ||
| const { start, end } = modelValue.value | ||
| if (!start) return 'Pick a date' | ||
| if (!end) return df.format(start.toDate(tz)) | ||
| return `${df.format(start.toDate(tz))} - ${df.format(end.toDate(tz))}` | ||
| }) | ||
|
|
||
| function computeStart(range: typeof ranges[number]) { | ||
| const end = today(tz) | ||
| return { start: end.subtract({ days: range.days, months: range.months, years: range.years }), end } | ||
| } | ||
|
|
||
| function isRangeSelected(range: typeof ranges[number]) { | ||
| const { start, end } = computeStart(range) | ||
| return modelValue.value.start.compare(start) === 0 && modelValue.value.end.compare(end) === 0 | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| function selectRange(range: typeof ranges[number]) { | ||
| modelValue.value = computeStart(range) | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <UPopover> | ||
| <UPopover :content="{ align: 'center' }"> | ||
| <UButton color="neutral" variant="subtle" icon="i-lucide-calendar"> | ||
| <template v-if="modelValue.start"> | ||
| <template v-if="modelValue.end"> | ||
| {{ df.format(modelValue.start.toDate(getLocalTimeZone())) }} - {{ df.format(modelValue.end.toDate(getLocalTimeZone())) }} | ||
| </template> | ||
|
|
||
| <template v-else> | ||
| {{ df.format(modelValue.start.toDate(getLocalTimeZone())) }} | ||
| </template> | ||
| </template> | ||
| <template v-else> | ||
| Pick a date | ||
| </template> | ||
| {{ label }} | ||
| </UButton> | ||
|
|
||
| <template #content> | ||
| <UCalendar v-model="modelValue" class="p-2" :number-of-months="2" range /> | ||
| <div class="flex items-stretch divide-x divide-(--ui-border)"> | ||
| <div class="flex flex-col py-2"> | ||
| <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="modelValue" class="p-2" :number-of-months="2" range /> | ||
| </div> | ||
| </template> | ||
| </UPopover> | ||
| </template> | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.