|
1
|
"use client" |
|
2
|
|
|
3
|
import { CalendarDate } from "@internationalized/date" |
|
4
|
import * as React from "react" |
|
5
|
import { useCalendarCell } from "react-aria" |
|
6
|
import { CalendarState } from "react-stately" |
|
7
|
|
|
8
|
import { clx } from "@/utils/clx" |
|
9
|
|
|
10
|
interface CalendarCellProps { |
|
11
|
date: CalendarDate |
|
12
|
state: CalendarState |
|
13
|
} |
|
14
|
|
|
15
|
const CalendarCell = ({ state, date }: CalendarCellProps) => { |
|
16
|
const ref = React.useRef(null) |
|
17
|
const { |
|
18
|
cellProps, |
|
19
|
buttonProps, |
|
20
|
isSelected, |
|
21
|
isOutsideVisibleRange, |
|
22
|
isDisabled, |
|
23
|
isUnavailable, |
|
24
|
formattedDate, |
|
25
|
} = useCalendarCell({ date }, state, ref) |
|
26
|
|
|
27
|
const isToday = getIsToday(date) |
|
28
|
|
|
29
|
return ( |
|
30
|
<td {...cellProps} className="p-1"> |
|
31
|
<div |
|
32
|
{...buttonProps} |
|
33
|
ref={ref} |
|
34
|
hidden={isOutsideVisibleRange} |
|
35
|
className={clx( |
|
36
|
"bg-ui-bg-base txt-compact-small relative flex size-8 items-center justify-center rounded-md outline-none transition-fg border border-transparent", |
|
37
|
"hover:bg-ui-bg-base-hover", |
|
38
|
"focus-visible:shadow-borders-focus focus-visible:border-ui-border-interactive", |
|
39
|
{ |
|
40
|
"!bg-ui-bg-interactive !text-ui-fg-on-color": isSelected, |
|
41
|
"hidden": isOutsideVisibleRange, |
|
42
|
"text-ui-fg-muted hover:!bg-ui-bg-base cursor-default": isDisabled || isUnavailable, |
|
43
|
} |
|
44
|
)} |
|
45
|
> |
|
46
|
{formattedDate} |
|
47
|
{isToday && ( |
|
48
|
<div |
|
49
|
role="none" |
|
50
|
className={clx( |
|
51
|
"bg-ui-bg-interactive absolute bottom-[3px] left-1/2 size-[3px] -translate-x-1/2 rounded-full transition-fg", |
|
52
|
{ |
|
53
|
"bg-ui-fg-on-color": isSelected, |
|
54
|
} |
|
55
|
)} |
|
56
|
/> |
|
57
|
)} |
|
58
|
</div> |
|
59
|
</td> |
|
60
|
) |
|
61
|
} |
|
62
|
|
|
63
|
/** |
|
64
|
* Check if the date is today. The CalendarDate is using a 1-based index for the month. |
|
65
|
* @returns Whether the CalendarDate is today. |
|
66
|
*/ |
|
67
|
function getIsToday(date: CalendarDate) { |
|
68
|
const today = new Date() |
|
69
|
return ( |
|
70
|
[date.year, date.month, date.day].join("-") === |
|
71
|
[today.getFullYear(), today.getMonth() + 1, today.getDate()].join("-") |
|
72
|
) |
|
73
|
} |
|
74
|
|
|
75
|
export { CalendarCell } |