1 | import React from "react";
|
2 | export function useSingle(props, dateLib) {
|
3 | const { selected: initiallySelected, required, onSelect } = props;
|
4 | const [selected, setSelected] = React.useState(initiallySelected);
|
5 | const { isSameDay } = dateLib;
|
6 |
|
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 |
|
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 |
|
\ | No newline at end of file |