Skip to content

Commit

Permalink
feat(Calendar): pass calendar instance instead of creating it with cr…
Browse files Browse the repository at this point in the history
…eateCalendar
  • Loading branch information
Solant committed Mar 4, 2025
1 parent 288c9d1 commit 98438ab
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/Calendar/CalendarRoot.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { type DateValue, isEqualDay, isSameDay } from '@internationalized/date'
import { type Calendar, type DateValue, GregorianCalendar, isEqualDay, isSameDay } from '@internationalized/date'
import type { Ref } from 'vue'
import type { PrimitiveProps } from '@/Primitive'
Expand Down Expand Up @@ -85,6 +85,7 @@ interface BaseCalendarRootProps extends PrimitiveProps {
nextPage?: (placeholder: DateValue) => DateValue
/** A function that returns the previous page of the calendar. It receives the current placeholder as an argument inside the component. */
prevPage?: (placeholder: DateValue) => DateValue
calendar?: Calendar
}
export interface MultipleCalendarRootProps extends BaseCalendarRootProps {
Expand Down Expand Up @@ -135,6 +136,7 @@ const props = withDefaults(defineProps<CalendarRootProps>(), {
placeholder: undefined,
isDateDisabled: undefined,
isDateUnavailable: undefined,
calendar: () => new GregorianCalendar(),
})
const emits = defineEmits<CalendarRootEmits>()
defineSlots<{
Expand Down Expand Up @@ -193,6 +195,7 @@ const defaultDate = getDefaultDate({
defaultPlaceholder: props.placeholder,
defaultValue: modelValue.value,
locale: props.locale,
calendar: props.calendar,
})
const placeholder = useVModel(props, 'placeholder', emits, {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/DateField/DateFieldRoot.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import type { DateValue } from '@internationalized/date'
import { type Calendar, type DateValue, GregorianCalendar } from '@internationalized/date'
import type { Ref } from 'vue'
import type { PrimitiveProps } from '@/Primitive'
Expand Down Expand Up @@ -63,6 +63,7 @@ export interface DateFieldRootProps extends PrimitiveProps, FormFieldProps {
id?: string
/** The reading direction of the date field when applicable. <br> If omitted, inherits globally from `ConfigProvider` or assumes LTR (left-to-right) reading mode. */
dir?: Direction
calendar?: Calendar
}
export type DateFieldRootEmits = {
Expand Down Expand Up @@ -92,6 +93,7 @@ const props = withDefaults(defineProps<DateFieldRootProps>(), {
readonly: false,
placeholder: undefined,
isDateUnavailable: undefined,
calendar: () => new GregorianCalendar(),
})
const emits = defineEmits<DateFieldRootEmits>()
defineSlots<{
Expand Down Expand Up @@ -128,6 +130,7 @@ const defaultDate = getDefaultDate({
granularity: granularity.value,
defaultValue: modelValue.value,
locale: props.locale,
calendar: props.calendar,
})
const placeholder = useVModel(props, 'placeholder', emits, {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/DatePicker/DatePickerRoot.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import type { DateValue } from '@internationalized/date'
import { type DateValue, GregorianCalendar } from '@internationalized/date'
import type { Ref } from 'vue'
import { computed, ref, toRefs, watch } from 'vue'
Expand Down Expand Up @@ -77,6 +77,7 @@ const props = withDefaults(defineProps<DatePickerRootProps>(), {
locale: 'en',
isDateDisabled: undefined,
isDateUnavailable: undefined,
calendar: () => new GregorianCalendar(),
})
const emits = defineEmits<DatePickerRootEmits & PopoverRootEmits>()
const {
Expand Down Expand Up @@ -117,6 +118,7 @@ const defaultDate = computed(() => getDefaultDate({
granularity: props.granularity,
defaultValue: modelValue.value,
locale: props.locale,
calendar: props.calendar,
}))
const placeholder = useVModel(props, 'placeholder', emits, {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/DateRangeField/DateRangeFieldRoot.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import type { DateValue } from '@internationalized/date'
import { type Calendar, type DateValue, GregorianCalendar } from '@internationalized/date'
import type { Ref } from 'vue'
import type { PrimitiveProps } from '@/Primitive'
Expand Down Expand Up @@ -77,6 +77,7 @@ export interface DateRangeFieldRootProps extends PrimitiveProps, FormFieldProps
id?: string
/** The reading direction of the date field when applicable. <br> If omitted, inherits globally from `ConfigProvider` or assumes LTR (left-to-right) reading mode. */
dir?: Direction
calendar?: Calendar
}
export type DateRangeFieldRootEmits = {
Expand Down Expand Up @@ -106,6 +107,7 @@ const props = withDefaults(defineProps<DateRangeFieldRootProps>(), {
readonly: false,
placeholder: undefined,
isDateUnavailable: undefined,
calendar: () => new GregorianCalendar(),
})
const emits = defineEmits<DateRangeFieldRootEmits>()
const { disabled, readonly, isDateUnavailable: propsIsDateUnavailable, dir: propDir, locale: propLocale } = toRefs(props)
Expand All @@ -131,6 +133,7 @@ const defaultDate = getDefaultDate({
granularity: props.granularity,
defaultValue: modelValue.value?.start,
locale: props.locale,
calendar: props.calendar,
})
const placeholder = useVModel(props, 'placeholder', emits, {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/DateRangePicker/DateRangePickerRoot.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import type { DateValue } from '@internationalized/date'
import { type DateValue, GregorianCalendar } from '@internationalized/date'
import type { Ref } from 'vue'
import { createContext, useDirection } from '@/shared'
Expand Down Expand Up @@ -83,6 +83,7 @@ const props = withDefaults(defineProps<DateRangePickerRootProps>(), {
isDateDisabled: undefined,
isDateUnavailable: undefined,
allowNonContiguousRanges: false,
calendar: () => new GregorianCalendar(),
})
const emits = defineEmits<DateRangePickerRootEmits & PopoverRootEmits>()
const {
Expand Down Expand Up @@ -123,6 +124,7 @@ const defaultDate = getDefaultDate({
granularity: props.granularity,
defaultValue: modelValue.value?.start,
locale: props.locale,
calendar: props.calendar,
})
const placeholder = useVModel(props, 'placeholder', emits, {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/RangeCalendar/RangeCalendarRoot.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { type DateValue, isEqualDay } from '@internationalized/date'
import { type Calendar, type DateValue, GregorianCalendar, isEqualDay } from '@internationalized/date'
import type { Ref } from 'vue'
import type { PrimitiveProps } from '@/Primitive'
Expand Down Expand Up @@ -97,6 +97,7 @@ export interface RangeCalendarRootProps extends PrimitiveProps {
nextPage?: (placeholder: DateValue) => DateValue
/** A function that returns the previous page of the calendar. It receives the current placeholder as an argument inside the component. */
prevPage?: (placeholder: DateValue) => DateValue
calendar?: Calendar
}
export type RangeCalendarRootEmits = {
Expand Down Expand Up @@ -133,6 +134,7 @@ const props = withDefaults(defineProps<RangeCalendarRootProps>(), {
isDateDisabled: undefined,
isDateUnavailable: undefined,
allowNonContiguousRanges: false,
calendar: () => new GregorianCalendar(),
})
const emits = defineEmits<RangeCalendarRootEmits>()
Expand Down Expand Up @@ -197,6 +199,7 @@ const defaultDate = getDefaultDate({
defaultPlaceholder: props.placeholder,
defaultValue: currentModelValue.value.start,
locale: props.locale,
calendar: props.calendar,
})
const startValue = ref(currentModelValue.value.start) as Ref<DateValue | undefined>
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/shared/date/comparators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Implementation ported from https://github.com/melt-ui/melt-ui/blob/develop/src/lib/internal/helpers/date/utils.ts
*/

import { CalendarDate, CalendarDateTime, DateFormatter, type DateValue, Time, type ZonedDateTime, createCalendar, toCalendar } from '@internationalized/date'
import { type Calendar, CalendarDate, CalendarDateTime, type DateValue, Time, type ZonedDateTime, toCalendar } from '@internationalized/date'

export type TimeValue = Time | CalendarDateTime | ZonedDateTime

Expand All @@ -14,6 +14,7 @@ type GetDefaultDateProps = {
defaultPlaceholder?: DateValue | undefined
granularity?: Granularity
locale?: string
calendar: Calendar
}

/**
Expand All @@ -27,7 +28,7 @@ type GetDefaultDateProps = {
*
*/
export function getDefaultDate(props: GetDefaultDateProps): DateValue {
const { defaultValue, defaultPlaceholder, granularity = 'day', locale = 'en' } = props
const { defaultValue, defaultPlaceholder, granularity = 'day', calendar } = props

if (Array.isArray(defaultValue) && defaultValue.length)
return defaultValue.at(-1)!.copy()
Expand All @@ -44,9 +45,6 @@ export function getDefaultDate(props: GetDefaultDateProps): DateValue {
const day = date.getDate()
const calendarDateTimeGranularities = ['hour', 'minute', 'second']

const defaultFormatter = new DateFormatter(locale)
const calendar = createCalendar(defaultFormatter.resolvedOptions().calendar)

if (calendarDateTimeGranularities.includes(granularity ?? 'day'))
return toCalendar(new CalendarDateTime(year, month, day, 0, 0, 0), calendar)

Expand Down

0 comments on commit 98438ab

Please sign in to comment.