'use client'; import { cva, cx } from 'class-variance-authority'; import dayjs from 'dayjs'; import timezonePlugin from 'dayjs/plugin/timezone'; import utcPlugin from 'dayjs/plugin/utc'; import { ChangeEvent, ReactNode, useEffect, useRef, useState } from 'react'; import { DayPicker, DayPickerProps, DropdownProps } from 'react-day-picker'; import { ChevronLeftIcon, ChevronRightIcon } from '~/icons'; import { IconButton } from '../icon-button'; import { Select } from '../select'; import { Skeleton } from '../skeleton'; import { Tooltip } from '../tooltip'; import styles from './calendar.module.css'; // `timezone` plugin depends on `utc`. dayjs.extend(utcPlugin); dayjs.extend(timezonePlugin); interface OnDropdownOpen { onDropdownOpen?: VoidFunction; } export interface CalendarPropsExtended { showTooltip?: boolean; tooltipMessages?: Record; /* * Record keys are tz-aware `DD-MM-YYYY`; function form gets raw `day.date` * (consumer must apply `timeZone` themselves to stay consistent). */ dateInfo?: Record | ((date: Date) => ReactNode | null); loading?: boolean; timeZone?: string; } export type CalendarProps = DayPickerProps & OnDropdownOpen & CalendarPropsExtended; const root = cva(styles.calendarRoot); interface DropDownComponentProps extends DropdownProps, OnDropdownOpen { disabled?: boolean; } function DropDown({ options = [], value, onChange, onDropdownOpen, disabled }: DropDownComponentProps) { const [open, setOpen] = useState(false); /* * Mirror the callback into a ref so the effect depends only on `open`, * parents that re-create `onDropdownOpen` per render would otherwise cause * a re-fire on every parent render where `open` is true. */ const onDropdownOpenRef = useRef(onDropdownOpen); useEffect(() => { onDropdownOpenRef.current = onDropdownOpen; }); useEffect(() => { if (open) onDropdownOpenRef.current?.(); }, [open]); function handleChange(value: string) { if (onChange) { onChange({ target: { value } } as ChangeEvent); } } return ( ); } export const Calendar = function ({ className, classNames, showOutsideDays = true, onDropdownOpen, showTooltip = false, tooltipMessages = {}, dateInfo = {}, loading = false, timeZone, ...props }: CalendarProps) { return ( ( ), NextMonthButton: ({ children, ...props }) => ( ), Dropdown: (props: DropdownProps) => ( ), DayButton: props => { const { day, ...buttonProps } = props; /* * Format in the picker's zone so the key matches the rendered day * (otherwise UTC-day grids miss messages keyed at UTC midnight when * the browser is in a non-UTC zone). */ const dateKey = timeZone ? dayjs(day.date).tz(timeZone).format('DD-MM-YYYY') : dayjs(day.date).format('DD-MM-YYYY'); const message = tooltipMessages[dateKey]; const dateComponent = typeof dateInfo === 'function' ? dateInfo(day.date) : dateInfo[dateKey]; const hasDateInfo = Boolean(dateComponent); return ( {hasDateInfo && (
{dateComponent}
)} {buttonProps.children} } /> {message}
); }, MonthGrid: props => (
) }} classNames={{ caption_label: styles.captionLabel, button_previous: `${styles.navButton} ${styles.navButtonPrevious}`, button_next: `${styles.navButton} ${styles.navButtonNext}`, month_caption: styles.monthCaption, months: styles.months, nav: styles.nav, day: styles.day, today: styles.today, outside: styles.outside, week: styles.week, weekdays: styles.week, weekday: styles.weekday, disabled: styles.disabled, selected: styles.selected, day_button: styles.dayButton, range_middle: styles.rangeMiddle, range_end: styles.rangeEnd, range_start: styles.rangeStart, hidden: styles.hidden, dropdowns: styles.dropdowns, ...classNames }} className={root({ className })} data-slot='calendar' mode='single' {...props} /> ); }; Calendar.displayName = 'Calendar';