import React from "react"; import type { DateLib, DayPickerProps, Modifiers, PropsSingle, SelectHandler, SelectedValue, Selection } from "../types/index.js"; export type UseSingle = { select: SelectHandler; isSelected: (date: Date) => boolean; selected: SelectedValue; }; export function useSingle( props: DayPickerProps, dateLib: DateLib ): Selection { const { selected: initiallySelected, required, onSelect } = props as PropsSingle; const [selected, setSelected] = React.useState( initiallySelected ); const { isSameDay } = dateLib; // Update the selected date if the `selected` value changes. React.useEffect(() => { setSelected(initiallySelected); }, [initiallySelected]); const isSelected = (compareDate: Date) => { return selected ? isSameDay(selected, compareDate) : false; }; const select = ( triggerDate: Date, modifiers: Modifiers, e: React.MouseEvent | React.KeyboardEvent ) => { let newDate: Date | undefined = triggerDate; if (!required && selected && selected && isSameDay(triggerDate, selected)) { // If the date is the same, clear the selection. newDate = undefined; } setSelected(newDate); if (required) { onSelect?.(newDate as Date, triggerDate, modifiers, e); } else { onSelect?.(newDate, triggerDate, modifiers, e); } return newDate; }; return { selected, select, isSelected } as Selection; }