Skip to content

Commit

Permalink
refactor(Dates)!: remove getYears, getMonths and formatter (#839)
Browse files Browse the repository at this point in the history
* refactor: remove redundant functions from Calendar components

* feat(radix-vue/date): add createYearRange function

* fix: refactor args of createYearRange

* docs: adjust docs

* fix: add default values for create year

* fix(Calendar/RangeCalendar): use utility functions in stories

* docs: update Calendar docs
  • Loading branch information
epr3 committed Apr 10, 2024
1 parent a3d98af commit 21666d0
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 116 deletions.
5 changes: 4 additions & 1 deletion docs/content/guides/dates.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ Specially designed to work well with [@internationalized/date](https://react-spe
import {
createDecade,
createMonth,
createMonths,
createYear,
createYearRange,
getDaysInMonth,
hasTime,
isAfter,
Expand Down Expand Up @@ -95,5 +95,8 @@ isAfterOrSame(date, minDate) // returns true
isBefore(date, maxDate) // returns true
isBetweenInclusive(date, minDate, maxDate) // returns true
isBetween(date, minDate, maxDate) // returns true
createYear({ dateObj: new CalendarDate(1995, 8, 18), numberOfMonths: 2, pagedNavigation: true }) // returns an array of months as DateValue, centered around the dateObj taking into account the numberOfMonths and pagedNavigation when returning the months
createDecade({ dateObj: new CalendarDate(1995, 8, 18), startIndex: -10, endIndex: 10 }) // returns a decade centered around the dateObj
createYearRange({ start: new CalendarDate(1995, 8, 18), end: new CalendarDate(2005, 8, 18) }) // returns an array of years as DateValue between the start and end date
```

15 changes: 0 additions & 15 deletions docs/content/meta/CalendarRoot.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,5 @@
'name': 'weekDays',
'description': '<p>The days of the week</p>\n',
'type': 'string[]'
},
{
'name': 'formatter',
'description': '<p>The formatter used inside the calendar for displaying dates</p>\n',
'type': 'Formatter'
},
{
'name': 'getMonths',
'description': '<p>The months that can be selected</p>\n',
'type': 'DateValue'
},
{
'name': 'getYears',
'description': '<p>The years that can be selected</p>\n',
'type': ''
}
]" />
5 changes: 0 additions & 5 deletions docs/content/meta/DatePickerCalendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@
'name': 'weekDays',
'description': '',
'type': 'string[]'
},
{
'name': 'formatter',
'description': '',
'type': 'Formatter'
}
]" />
5 changes: 0 additions & 5 deletions docs/content/meta/DateRangePickerCalendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@
'name': 'weekDays',
'description': '',
'type': 'string[]'
},
{
'name': 'formatter',
'description': '',
'type': 'Formatter'
}
]" />
15 changes: 0 additions & 15 deletions docs/content/meta/RangeCalendarRoot.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,5 @@
'name': 'weekDays',
'description': '<p>The days of the week</p>\n',
'type': 'string[]'
},
{
'name': 'formatter',
'description': '<p>The formatter used inside the calendar for displaying dates</p>\n',
'type': 'Formatter'
},
{
'name': 'getMonths',
'description': '<p>The months that can be selected</p>\n',
'type': 'DateValue'
},
{
'name': 'getYears',
'description': '<p>The years that can be selected</p>\n',
'type': ''
}
]" />
33 changes: 3 additions & 30 deletions packages/radix-vue/src/Calendar/CalendarRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { type Formatter, createContext, useDirection } from '@/shared'
import { useCalendar, useCalendarState } from './useCalendar'
import { getDefaultDate, handleCalendarInitialFocus } from '@/shared/date'
import { type Grid, type Matcher, type WeekDayFormat, createDecade, createYear } from '@/date'
import { type Grid, type Matcher, type WeekDayFormat } from '@/date'
import type { Direction } from '@/shared/types'
type CalendarRootContext = {
Expand Down Expand Up @@ -111,9 +111,9 @@ export const [injectCalendarRootContext, provideCalendarRootContext]
</script>

<script setup lang="ts">
import { computed, onMounted, toRefs, watch } from 'vue'
import { onMounted, toRefs, watch } from 'vue'
import { Primitive, usePrimitiveElement } from '@/Primitive'
import { useMemoize, useVModel } from '@vueuse/core'
import { useVModel } from '@vueuse/core'
const props = withDefaults(defineProps<CalendarRootProps>(), {
defaultValue: undefined,
Expand Down Expand Up @@ -142,12 +142,6 @@ defineSlots<{
grid: Grid<DateValue>[]
/** The days of the week */
weekDays: string[]
/** The formatter used inside the calendar for displaying dates */
formatter: Formatter
/** The months that can be selected */
getMonths: DateValue[]
/** The years that can be selected */
getYears: ({ startIndex, endIndex }: { startIndex?: number; endIndex: number }) => DateValue[]
}): any
}>()
Expand Down Expand Up @@ -279,24 +273,6 @@ function onDateChange(value: DateValue) {
}
}
const getMonths = computed(() => {
const dateObj = placeholder.value.copy()
return createYear({
dateObj,
numberOfMonths: numberOfMonths.value,
pagedNavigation: pagedNavigation.value,
})
})
const getYears = useMemoize(({ startIndex, endIndex }: { startIndex?: number; endIndex: number }) => {
const dateObj = placeholder.value.copy()
return createDecade({
dateObj,
startIndex,
endIndex,
})
})
onMounted(() => {
if (initialFocus.value)
handleCalendarInitialFocus(parentElement.value)
Expand Down Expand Up @@ -351,9 +327,6 @@ provideCalendarRootContext({
:date="placeholder"
:grid="grid"
:week-days="weekdays"
:formatter="formatter"
:get-months="getMonths"
:get-years="getYears"
/>
<div
style="border: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); height: 1px; margin: -1px; overflow: hidden; padding: 0px; position: absolute; white-space: nowrap; width: 1px;"
Expand Down
10 changes: 6 additions & 4 deletions packages/radix-vue/src/Calendar/story/CalendarPopover.story.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import { CalendarCell, CalendarCellTrigger, CalendarGrid, CalendarGridBody, Cale
import { type Ref, ref } from 'vue'
import { CalendarDate, type DateValue, getLocalTimeZone, today } from '@internationalized/date'
import { toDate } from '@/date'
import { createDecade, createYear, toDate } from '@/date'
import CalendarPopover from './_CalendarPopover.vue'
import { useDateFormatter } from '@/shared'
const value = ref(new CalendarDate(2024, 3, 20)) as Ref<DateValue>
const placeholder = ref(today(getLocalTimeZone())) as Ref<DateValue>
const formatter = useDateFormatter('en')
</script>

<template>
<Story title="Calendar/Popover" :layout="{ type: 'single' }">
<Variant title="default">
<CalendarRoot
v-slot="{ weekDays, grid, getMonths, getYears, formatter, date }"
v-slot="{ weekDays, grid, date }"
v-model="value"
v-model:placeholder="placeholder"
class="mt-6 rounded-[15px] border border-black bg-white p-[22px] shadow-md"
Expand All @@ -36,7 +38,7 @@ const placeholder = ref(today(getLocalTimeZone())) as Ref<DateValue>
</template>
<div class="grid grid-cols-4 bg-white rounded-[9px]">
<div
v-for="month in getMonths"
v-for="month in createYear({ dateObj: date })"
:key="month.toString()"
class="relative cursor-pointer flex items-center justify-center whitespace-nowrap rounded-[9px] border border-transparent bg-transparent text-sm font-normal text-black p-2 outline-none focus:shadow-[0_0_0_2px] focus:shadow-black hover:border-black"
:class="{ 'before:absolute before:top-[5px] before:rounded-full before:w-1 before:h-1 before:block before:bg-grass9': placeholder.month === month.month }"
Expand All @@ -52,7 +54,7 @@ const placeholder = ref(today(getLocalTimeZone())) as Ref<DateValue>
</template>
<div class="grid grid-cols-4 bg-white rounded-[9px] gap-4">
<div
v-for="yearValue in getYears({ startIndex: -10, endIndex: 10 })"
v-for="yearValue in createDecade({ dateObj: date, startIndex: -10, endIndex: 10 })"
:key="yearValue.toString()"
class="relative cursor-pointer flex items-center justify-center whitespace-nowrap rounded-[9px] border border-transparent bg-transparent text-sm font-normal text-black p-2 outline-none focus:shadow-[0_0_0_2px] focus:shadow-black hover:border-black"
:class="{ 'before:absolute before:top-[5px] before:rounded-full before:w-1 before:h-1 before:block before:bg-grass9': placeholder.year === yearValue.year }"
Expand Down
3 changes: 1 addition & 2 deletions packages/radix-vue/src/DatePicker/DatePickerCalendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootContext = injectDatePickerRootContext()

<template>
<CalendarRoot
v-slot="{ weekDays, grid, date, formatter }"
v-slot="{ weekDays, grid, date }"
v-bind="{
isDateDisabled: rootContext.isDateDisabled,
isDateUnavailable: rootContext.isDateUnavailable,
Expand Down Expand Up @@ -44,7 +44,6 @@ const rootContext = injectDatePickerRootContext()
:date="date"
:grid="grid"
:week-days="weekDays"
:formatter="formatter"
/>
</CalendarRoot>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootContext = injectDateRangePickerRootContext()

<template>
<RangeCalendarRoot
v-slot="{ weekDays, grid, date, formatter }"
v-slot="{ weekDays, grid, date }"
v-bind="{
isDateDisabled: rootContext.isDateDisabled,
isDateUnavailable: rootContext.isDateUnavailable,
Expand Down Expand Up @@ -43,7 +43,6 @@ const rootContext = injectDateRangePickerRootContext()
:date="date"
:grid="grid"
:week-days="weekDays"
:formatter="formatter"
/>
</RangeCalendarRoot>
</template>
33 changes: 3 additions & 30 deletions packages/radix-vue/src/RangeCalendar/RangeCalendarRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Ref } from 'vue'
import type { PrimitiveProps } from '@/Primitive'
import { type Formatter, createContext, useDirection } from '@/shared'
import { getDefaultDate, handleCalendarInitialFocus } from '@/shared/date'
import { type Grid, type Matcher, type WeekDayFormat, createDecade, createYear, isBefore } from '@/date'
import { type Grid, type Matcher, type WeekDayFormat, isBefore } from '@/date'
import type { DateRange } from '@/shared/date'
import { useRangeCalendarState } from './useRangeCalendar'
import { useCalendar } from '@/Calendar/useCalendar'
Expand Down Expand Up @@ -103,9 +103,9 @@ export const [injectRangeCalendarRootContext, provideRangeCalendarRootContext]
</script>

<script setup lang="ts">
import { computed, onMounted, ref, toRefs, watch } from 'vue'
import { onMounted, ref, toRefs, watch } from 'vue'
import { Primitive, usePrimitiveElement } from '@/Primitive'
import { useMemoize, useVModel } from '@vueuse/core'
import { useVModel } from '@vueuse/core'
const props = withDefaults(defineProps<RangeCalendarRootProps>(), {
defaultValue: undefined,
Expand Down Expand Up @@ -135,12 +135,6 @@ defineSlots<{
grid: Grid<DateValue>[]
/** The days of the week */
weekDays: string[]
/** The formatter used inside the calendar for displaying dates */
formatter: Formatter
/** The months that can be selected */
getMonths: DateValue[]
/** The years that can be selected */
getYears: ({ startIndex, endIndex }: { startIndex?: number; endIndex: number }) => DateValue[]
}): any
}>()
Expand Down Expand Up @@ -271,24 +265,6 @@ watch([startValue, endValue], () => {
}
})
const getMonths = computed(() => {
const dateObj = placeholder.value.copy()
return createYear({
dateObj,
numberOfMonths: numberOfMonths.value,
pagedNavigation: pagedNavigation.value,
})
})
const getYears = useMemoize(({ startIndex, endIndex }: { startIndex?: number; endIndex: number }) => {
const dateObj = placeholder.value.copy()
return createDecade({
dateObj,
startIndex,
endIndex,
})
})
provideRangeCalendarRootContext({
isDateUnavailable,
startValue,
Expand Down Expand Up @@ -354,9 +330,6 @@ onMounted(() => {
:date="placeholder"
:grid="grid"
:week-days="weekdays"
:formatter="formatter"
:get-months="getMonths"
:get-years="getYears"
/>
</Primitive>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import { type Ref, ref } from 'vue'
import { type DateValue, getLocalTimeZone, today } from '@internationalized/date'
import CalendarPopover from '@/Calendar/story/_CalendarPopover.vue'
import { toDate } from '@/shared'
import { createDecade, createYear, toDate } from '@/date'
import { useDateFormatter } from '@/shared'
const placeholder = ref(today(getLocalTimeZone())) as Ref<DateValue>
const formatter = useDateFormatter('en')
</script>

<template>
<Story title="Range Calendar/Popover" :layout="{ type: 'single' }">
<Variant title="default">
<RangeCalendarRoot
v-slot="{ weekDays, grid, getMonths, getYears, formatter, date }"
v-slot="{ weekDays, grid, date }"
v-model:placeholder="placeholder"
class="mt-6 rounded-[15px] border border-black bg-white p-[22px] shadow-md"
fixed-weeks
Expand All @@ -32,7 +34,7 @@ const placeholder = ref(today(getLocalTimeZone())) as Ref<DateValue>
</template>
<div class="grid grid-cols-4 bg-white rounded-[9px]">
<div
v-for="month in getMonths"
v-for="month in createYear({ dateObj: date })"
:key="month.toString()"
class="relative cursor-pointer flex items-center justify-center whitespace-nowrap rounded-[9px] border border-transparent bg-transparent text-sm font-normal text-black p-2 outline-none focus:shadow-[0_0_0_2px] focus:shadow-black hover:border-black"
:class="{ 'before:absolute before:top-[5px] before:rounded-full before:w-1 before:h-1 before:block before:bg-grass9': date.month === month.month }"
Expand All @@ -48,7 +50,7 @@ const placeholder = ref(today(getLocalTimeZone())) as Ref<DateValue>
</template>
<div class="grid grid-cols-4 bg-white rounded-[9px] gap-4">
<div
v-for="yearValue in getYears({ startIndex: -10, endIndex: 10 })"
v-for="yearValue in createDecade({ dateObj: date, startIndex: -10, endIndex: 10 })"
:key="yearValue.toString()"
class="relative cursor-pointer flex items-center justify-center whitespace-nowrap rounded-[9px] border border-transparent bg-transparent text-sm font-normal text-black p-2 outline-none focus:shadow-[0_0_0_2px] focus:shadow-black hover:border-black"
:class="{ 'before:absolute before:top-[5px] before:rounded-full before:w-1 before:h-1 before:block before:bg-grass9': date.year === yearValue.year }"
Expand Down
23 changes: 20 additions & 3 deletions packages/radix-vue/src/date/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type WeekDayFormat = 'narrow' | 'short' | 'long'

export type CreateSelectProps = {
/**
* The date object representing the month's date (usually the first day of the month).
* The date object representing the date (usually the first day of the month/year).
*/
dateObj: DateValue
}
Expand Down Expand Up @@ -104,7 +104,7 @@ type SetMonthProps = CreateMonthProps & {

type SetYearProps = CreateSelectProps & {
numberOfMonths?: number
pagedNavigation: boolean
pagedNavigation?: boolean
}

type SetDecadeProps = CreateSelectProps & {
Expand Down Expand Up @@ -136,7 +136,7 @@ export function createDecade(props: SetDecadeProps): DateValue[] {
}

export function createYear(props: SetYearProps): DateValue[] {
const { dateObj, numberOfMonths, pagedNavigation } = props
const { dateObj, numberOfMonths = 1, pagedNavigation = false } = props

if (numberOfMonths && pagedNavigation) {
const monthsArray = Array.from({ length: Math.floor(12 / numberOfMonths) }, (_, i) => startOfMonth(dateObj.set({ month: i * numberOfMonths + 1 })))
Expand Down Expand Up @@ -183,3 +183,20 @@ export function createMonths(props: SetMonthProps) {

return months
}

export function createYearRange({ start, end }: { start?: DateValue; end?: DateValue }): DateValue[] {
const years: DateValue[] = []

if (!start || !end)
return years

let current = startOfYear(start)

while (current.compare(end) <= 0) {
years.push(current)
// Move to the first day of the next year
current = startOfYear(current.add({ years: 1 }))
}

return years
}

0 comments on commit 21666d0

Please sign in to comment.