|
1
|
"use client" |
|
2
|
|
|
3
|
import { |
|
4
|
CalendarDate, |
|
5
|
createCalendar, |
|
6
|
getLocalTimeZone |
|
7
|
} from "@internationalized/date" |
|
8
|
import { TriangleLeftMini, TriangleRightMini } from "@medusajs/icons" |
|
9
|
import * as React from "react" |
|
10
|
import { |
|
11
|
DateValue, |
|
12
|
useCalendar, |
|
13
|
useLocale, |
|
14
|
type CalendarProps as BaseCalendarProps, |
|
15
|
} from "react-aria" |
|
16
|
import { useCalendarState } from "react-stately" |
|
17
|
|
|
18
|
import { createCalendarDate, getDefaultCalendarDate, updateCalendarDate } from "@/utils/calendar" |
|
19
|
|
|
20
|
import { CalendarButton } from "./calendar-button" |
|
21
|
import { CalendarGrid } from "./calendar-grid" |
|
22
|
|
|
23
|
interface CalendarValueProps { |
|
24
|
/** |
|
25
|
* The currently selected date. |
|
26
|
*/ |
|
27
|
value?: Date | null |
|
28
|
/** |
|
29
|
* The date that is selected when the calendar first mounts (uncontrolled). |
|
30
|
*/ |
|
31
|
defaultValue?: Date | null |
|
32
|
/** |
|
33
|
* A function that is triggered when the selected date changes. |
|
34
|
*/ |
|
35
|
onChange?: (value: Date | null) => void |
|
36
|
/** |
|
37
|
* A function that determines whether a date is unavailable for selection. |
|
38
|
*/ |
|
39
|
isDateUnavailable?: (date: Date) => boolean |
|
40
|
/** |
|
41
|
* The minimum date that can be selected. |
|
42
|
*/ |
|
43
|
minValue?: Date |
|
44
|
/** |
|
45
|
* The maximum date that can be selected. |
|
46
|
*/ |
|
47
|
maxValue?: Date |
|
48
|
} |
|
49
|
|
|
50
|
interface CalendarProps |
|
51
|
extends Omit<BaseCalendarProps<CalendarDate>, keyof CalendarValueProps>, |
|
52
|
CalendarValueProps {} |
|
53
|
|
|
54
|
/** |
|
55
|
* Calendar component used to select a date. |
|
56
|
* Its props are based on [React Aria Calendar](https://react-spectrum.adobe.com/react-aria/Calendar.html#calendar-1). |
|
57
|
* |
|
58
|
* @excludeExternal |
|
59
|
*/ |
|
60
|
const Calendar = (props: CalendarProps) => { |
|
61
|
const [value, setValue] = React.useState<CalendarDate | null | undefined>( |
|
62
|
() => getDefaultCalendarDate(props.value, props.defaultValue) |
|
63
|
) |
|
64
|
|
|
65
|
const { locale } = useLocale() |
|
66
|
const _props = React.useMemo(() => convertProps(props, setValue), [props]) |
|
67
|
|
|
68
|
const state = useCalendarState({ |
|
69
|
..._props, |
|
70
|
value, |
|
71
|
locale, |
|
72
|
createCalendar, |
|
73
|
}) |
|
74
|
|
|
75
|
React.useEffect(() => { |
|
76
|
setValue(props.value ? updateCalendarDate(value, props.value) : null) |
|
77
|
}, [props.value]) |
|
78
|
|
|
79
|
const { calendarProps, prevButtonProps, nextButtonProps, title } = |
|
80
|
useCalendar({ value, ..._props }, state) |
|
81
|
|
|
82
|
return ( |
|
83
|
<div {...calendarProps} className="flex flex-col gap-y-2"> |
|
84
|
<div className="bg-ui-bg-field border-base grid grid-cols-[28px_1fr_28px] items-center gap-1 rounded-md border p-0.5"> |
|
85
|
<CalendarButton {...prevButtonProps}> |
|
86
|
<TriangleLeftMini /> |
|
87
|
</CalendarButton> |
|
88
|
<div className="flex items-center justify-center"> |
|
89
|
<h2 className="txt-compact-small-plus">{title}</h2> |
|
90
|
</div> |
|
91
|
<CalendarButton {...nextButtonProps}> |
|
92
|
<TriangleRightMini /> |
|
93
|
</CalendarButton> |
|
94
|
</div> |
|
95
|
<CalendarGrid state={state} /> |
|
96
|
</div> |
|
97
|
) |
|
98
|
} |
|
99
|
|
|
100
|
function convertProps( |
|
101
|
props: CalendarProps, |
|
102
|
setValue: React.Dispatch< |
|
103
|
React.SetStateAction<CalendarDate | null | undefined> |
|
104
|
> |
|
105
|
): BaseCalendarProps<CalendarDate> { |
|
106
|
const { |
|
107
|
minValue, |
|
108
|
maxValue, |
|
109
|
isDateUnavailable: _isDateUnavailable, |
|
110
|
onChange: _onChange, |
|
111
|
value: __value__, |
|
112
|
defaultValue: __defaultValue__, |
|
113
|
...rest |
|
114
|
} = props |
|
115
|
|
|
116
|
const onChange = (value: CalendarDate | null) => { |
|
117
|
setValue(value) |
|
118
|
_onChange?.(value ? value.toDate(getLocalTimeZone()) : null) |
|
119
|
} |
|
120
|
|
|
121
|
const isDateUnavailable = (date: DateValue) => { |
|
122
|
const _date = date.toDate(getLocalTimeZone()) |
|
123
|
|
|
124
|
return _isDateUnavailable ? _isDateUnavailable(_date) : false |
|
125
|
} |
|
126
|
|
|
127
|
return { |
|
128
|
...rest, |
|
129
|
onChange, |
|
130
|
isDateUnavailable, |
|
131
|
minValue: minValue ? createCalendarDate(minValue) : minValue, |
|
132
|
maxValue: maxValue ? createCalendarDate(maxValue) : maxValue, |
|
133
|
} |
|
134
|
} |
|
135
|
|
|
136
|
export { Calendar } |