"use client" import { parseDate } from "@internationalized/date" import { useState } from "react" import { DateInput, DatePicker, Group } from "react-aria-components" import { useController, useFormContext, useWatch } from "react-hook-form" import { useIntl } from "react-intl" import { dt } from "@/lib/dt" import Select from "@/components/TempDesignSystem/Select" import { rangeArray } from "@/utils/rangeArray" import { DateName } from "./date" import styles from "./date.module.css" import type { Key } from "react-aria-components" import type { DateProps } from "./date" export default function DateSelect({ name, registerOptions = {} }: DateProps) { const intl = useIntl() const currentValue = useWatch({ name }) const { control, setValue, trigger } = useFormContext() const { field } = useController({ control, name, rules: registerOptions, }) const currentYear = new Date().getFullYear() const months = rangeArray(1, 12).map((month) => ({ value: month, label: `${month}`, })) const years = rangeArray(1900, currentYear - 18) .reverse() .map((year) => ({ value: year, label: year.toString() })) // Ensure the user can't select a date that doesn't exist. const daysInMonth = dt(currentValue).daysInMonth() const days = rangeArray(1, daysInMonth).map((day) => ({ value: day, label: `${day}`, })) function createOnSelect(selector: DateName) { /** * Months are 0 index based and therefore we * must subtract by 1 to get the selected month */ return (select: Key) => { if (selector === DateName.month) { select = Number(select) - 1 } const newDate = dt(currentValue).set(selector, Number(select)) setValue(name, newDate.format("YYYY-MM-DD")) trigger(name) } } const dayLabel = intl.formatMessage({ id: "Day" }) const monthLabel = intl.formatMessage({ id: "Month" }) const yearLabel = intl.formatMessage({ id: "Year" }) let dateValue = null try { /** * parseDate throws when its not a valid * date, but we can't check isNan since * we recieve the date as "1999-01-01" */ dateValue = dt(currentValue).isValid() ? parseDate(currentValue) : null } catch (error) { console.warn("Known error for parse date in DateSelect: ", error) } return ( {(segment) => { switch (segment.type) { case "day": return (
) case "year": return (