Skip to content

Commit

Permalink
feat: use @internationalized/date instead
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Feb 13, 2025
1 parent aaf3d0a commit 722dffe
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 20 deletions.
1 change: 0 additions & 1 deletion packages/core/src/i18n/useDateFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const dateFormatterCache = new Map<string, DateFormatter>();

function getFormatter(locale: string, options: Intl.DateTimeFormatOptions = {}) {
const cacheKey = locale + JSON.stringify(options);
console.log(cacheKey);
let formatter = dateFormatterCache.get(cacheKey);
if (!formatter) {
formatter = new DateFormatter(locale, options);
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/i18n/useLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { getConfig } from '../config';
import { getDirection } from './getDirection';
import { getWeekInfo } from './getWeekInfo';
import { Maybe, Reactivify } from '../types';
import { getCalendar } from './getCalendar';
import { Calendar } from '@internationalized/date';
import { Calendar, GregorianCalendar } from '@internationalized/date';
import { getTimeZone } from './getTimezone';

export type NumberLocaleExtension = `nu-${string}`;
Expand Down Expand Up @@ -49,7 +48,7 @@ export function useLocale(
const localeInstance = computed(() => new Intl.Locale(localeString.value));
const direction = computed(() => getDirection(localeInstance.value));
const weekInfo = computed(() => getWeekInfo(localeInstance.value));
const calendar = computed(() => getCalendar(localeInstance.value));
const calendar = computed(() => toValue(extensions.calendar) ?? (new GregorianCalendar() as Calendar));
const timeZone = computed(() => getTimeZone(localeInstance.value));
const locale = computed(() => localeInstance.value.toString());

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/useCalendar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface CalendarContext {
locale: Ref<string>;
weekInfo: Ref<WeekInfo>;
calendar: Ref<Calendar>;
timeZone: Ref<string>;
getSelectedDate: () => ZonedDateTime;
getMinDate: () => Maybe<ZonedDateTime>;
getMaxDate: () => Maybe<ZonedDateTime>;
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/useCalendar/useCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useLabel } from '../a11y';
import { useControlButtonProps } from '../helpers/useControlButtonProps';
import { CalendarContextKey, MONTHS_COLUMNS_COUNT, YEAR_CELLS_COUNT, YEARS_COLUMNS_COUNT } from './constants';
import { CalendarPanel, useCalendarPanel } from './useCalendarPanel';
import { Calendar, ZonedDateTime, fromDate, now } from '@internationalized/date';
import { Calendar, ZonedDateTime, now, toCalendar } from '@internationalized/date';

export interface CalendarProps {
/**
Expand Down Expand Up @@ -85,7 +85,7 @@ export function useCalendar(_props: Reactivify<CalendarProps, 'onDaySelected'> =
calendar: () => toValue(props.calendar),
});

const selectedDate = computed(() => toValue(props.currentDate) ?? now(toValue(timeZone)));
const selectedDate = computed(() => toValue(props.currentDate) ?? toCalendar(now(toValue(timeZone)), calendar.value));
const focusedDay = shallowRef<ZonedDateTime>();
const { isOpen } = usePopoverController(pickerEl, { disabled: props.disabled });

Expand All @@ -101,6 +101,7 @@ export function useCalendar(_props: Reactivify<CalendarProps, 'onDaySelected'> =
weekInfo,
locale,
calendar,
timeZone,
getSelectedDate: () => selectedDate.value,
getFocusedDate: getFocusedOrSelected,
setDate: (date: ZonedDateTime, panel?: CalendarPanelType) => {
Expand Down Expand Up @@ -175,7 +176,7 @@ export function useCalendar(_props: Reactivify<CalendarProps, 'onDaySelected'> =
}

if (!focusedDay.value) {
focusedDay.value = fromDate(selectedDate.value.toDate(), selectedDate.value.timeZone);
focusedDay.value = selectedDate.value.copy();
}

await nextTick();
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/useCalendar/useCalendarPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useDateFormatter } from '../i18n';
import { Reactivify } from '../types';
import { normalizeProps } from '../utils/common';
import { YEAR_CELLS_COUNT } from './constants';
import { now } from '@internationalized/date';
import { now, toCalendar } from '@internationalized/date';

export interface CalendarDayPanel {
type: 'day';
Expand Down Expand Up @@ -79,7 +79,7 @@ export function useCalendarPanel(_props: Reactivify<CalendarPanelProps>, context
}

function useCalendarDaysPanel(
{ weekInfo, getFocusedDate, getSelectedDate, locale, getMinDate, getMaxDate }: CalendarContext,
{ weekInfo, getFocusedDate, getSelectedDate, locale, timeZone, calendar, getMinDate, getMaxDate }: CalendarContext,
daysOfWeekFormat?: MaybeRefOrGetter<Intl.DateTimeFormatOptions['weekday']>,
) {
const dayFormatter = useDateFormatter(locale, () => ({ weekday: toValue(daysOfWeekFormat) ?? 'short' }));
Expand All @@ -98,7 +98,7 @@ function useCalendarDaysPanel(

// Always use 6 weeks (42 days) for consistent layout
const gridDays = 42;
const rightNow = now(focused.timeZone);
const rightNow = toCalendar(now(timeZone.value), calendar.value);
const minDate = getMinDate();
const maxDate = getMaxDate();

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/useDateTimeField/temporalPartial.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DateTimeSegmentType, TemporalPartial } from './types';
import { isObject } from '../../../shared/src';
import { Calendar, ZonedDateTime, fromDate, now } from '@internationalized/date';
import { Calendar, ZonedDateTime, now, toCalendar } from '@internationalized/date';

export function createTemporalPartial(calendar: Calendar, timeZone: string) {
const zonedDateTime = now(timeZone) as TemporalPartial;
const zonedDateTime = toCalendar(now(timeZone), calendar) as TemporalPartial;
zonedDateTime['~fw_temporal_partial'] = {};

return zonedDateTime;
Expand All @@ -13,7 +13,7 @@ export function toTemporalPartial(
value: ZonedDateTime | TemporalPartial,
setParts?: DateTimeSegmentType[],
): TemporalPartial {
const clone = fromDate(value.toDate(), value.timeZone) as TemporalPartial;
const clone = value.copy() as TemporalPartial;
clone['~fw_temporal_partial'] = isTemporalPartial(value) ? value['~fw_temporal_partial'] : {};
if (setParts) {
setParts.forEach(part => {
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/useDateTimeField/useDateTimeField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ export interface DateTimeFieldProps {
/**
* The minimum date to use for the field.
*/
minDate?: Date;
min?: Date;

/**
* The maximum date to use for the field.
*/
maxDate?: Date;
max?: Date;
}

export function useDateTimeField(_props: Reactivify<DateTimeFieldProps, 'schema'>) {
Expand All @@ -105,7 +105,7 @@ export function useDateTimeField(_props: Reactivify<DateTimeFieldProps, 'schema'
timeZone: timeZone,
locale: locale,
model: {
get: () => toValue(props.minDate),
get: () => toValue(props.min),
},
});

Expand All @@ -114,7 +114,7 @@ export function useDateTimeField(_props: Reactivify<DateTimeFieldProps, 'schema'
timeZone: timeZone,
locale: locale,
model: {
get: () => toValue(props.maxDate),
get: () => toValue(props.max),
},
});

Expand Down Expand Up @@ -160,6 +160,7 @@ export function useDateTimeField(_props: Reactivify<DateTimeFieldProps, 'schema'
const calendarProps: Reactivify<CalendarProps, 'onDaySelected'> = {
locale: () => locale.value,
currentDate: temporalValue,
calendar: calendar,
onDaySelected: onValueChange,
minDate: () => (isTemporalPartial(minDate.value) ? undefined : minDate.value),
maxDate: () => (isTemporalPartial(maxDate.value) ? undefined : maxDate.value),
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/useDateTimeField/useDateTimeSegmentGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from './constants';
import { NumberParserContext, useNumberParser } from '../i18n';
import { isTemporalPartial, isTemporalPartSet, toTemporalPartial } from './temporalPartial';
import { ZonedDateTime, DateFormatter, fromDate } from '@internationalized/date';
import { ZonedDateTime, DateFormatter } from '@internationalized/date';

export interface DateTimeSegmentRegistration {
id: string;
Expand Down Expand Up @@ -105,7 +105,7 @@ export function useDateTimeSegmentGroup({

function withAllPartsSet(value: ZonedDateTime) {
if (isTemporalPartial(value) && isAllPartsSet(value)) {
return fromDate(value.toDate(), value.timeZone); // clones the value and drops the partial flag
return value.copy(); // clones the value and drops the partial flag
}

return value;
Expand Down
5 changes: 4 additions & 1 deletion packages/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import DateField from '@/components/DateField.vue';
import { createCalendar } from '@internationalized/date';
const today = new Date();
const minDate = new Date('2025-01-01');
const maxDate = new Date('2025-01-31');
</script>
Expand All @@ -11,10 +12,12 @@ const maxDate = new Date('2025-01-31');
<DateField
name="date"
label="Date"
:value="today"
locale="ar"
:calendar="createCalendar('islamic-umalqura')"
:format-options="{
day: '2-digit',
month: '2-digit',
month: 'long',
year: 'numeric',
hour: '2-digit',
hour12: true,
Expand Down

0 comments on commit 722dffe

Please sign in to comment.