UNPKG

1.16 kBJavaScriptView Raw
1import React from "react";
2export function useSingle(props, dateLib) {
3 const { selected: initiallySelected, required, onSelect } = props;
4 const [selected, setSelected] = React.useState(initiallySelected);
5 const { isSameDay } = dateLib;
6 // Update the selected date if the `selected` value changes.
7 React.useEffect(() => {
8 setSelected(initiallySelected);
9 }, [initiallySelected]);
10 const isSelected = (compareDate) => {
11 return selected ? isSameDay(selected, compareDate) : false;
12 };
13 const select = (triggerDate, modifiers, e) => {
14 let newDate = triggerDate;
15 if (!required && selected && selected && isSameDay(triggerDate, selected)) {
16 // If the date is the same, clear the selection.
17 newDate = undefined;
18 }
19 setSelected(newDate);
20 if (required) {
21 onSelect?.(newDate, triggerDate, modifiers, e);
22 }
23 else {
24 onSelect?.(newDate, triggerDate, modifiers, e);
25 }
26 return newDate;
27 };
28 return {
29 selected,
30 select,
31 isSelected
32 };
33}
34//# sourceMappingURL=useSingle.js.map
\No newline at end of file