-
-
Notifications
You must be signed in to change notification settings - Fork 500
Expand file tree
/
Copy pathDays.tsx
More file actions
98 lines (89 loc) · 2.46 KB
/
Days.tsx
File metadata and controls
98 lines (89 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"use client";
import { twMerge } from "../../../helpers/tailwind-merge";
import { useDatePickerContext } from "../DatepickerContext";
import {
addDays,
getFirstDayOfTheMonth,
getFormattedDate,
getWeekDays,
isDateEqual,
isDateInRange,
isDateToday,
isMonthEqual,
Views,
} from "../helpers";
export interface DatepickerViewsDaysTheme {
header: {
base: string;
title: string;
};
items: {
base: string;
item: {
base: string;
selected: string;
disabled: string;
today: string;
outside: string;
};
};
}
export function DatepickerViewsDays() {
const {
theme: rootTheme,
weekStart,
minDate,
maxDate,
filterDate,
viewDate,
selectedDate,
changeSelectedDate,
language,
} = useDatePickerContext();
const theme = rootTheme.views.days;
const weekDays = getWeekDays(language, weekStart);
const startDate = getFirstDayOfTheMonth(viewDate, weekStart);
return (
<>
<div className={theme.header.base}>
{weekDays.map((day, index) => (
<span key={index} className={theme.header.title}>
{day}
</span>
))}
</div>
<div className={theme.items.base}>
{[...Array(42)].map((_date, index) => {
const currentDate = addDays(startDate, index);
const day = getFormattedDate(language, currentDate, { day: "numeric" });
const isSelected = selectedDate && isDateEqual(selectedDate, currentDate);
const isDisabled =
!isDateInRange(currentDate, minDate, maxDate) || (filterDate && !filterDate(currentDate, Views.Days));
const isToday = isDateToday(currentDate);
const isOutOfMonth = !isMonthEqual(currentDate, viewDate)
return (
<button
disabled={isDisabled}
key={index}
type="button"
className={twMerge(
theme.items.item.base,
isOutOfMonth && theme.items.item.outside,
isToday && theme.items.item.today,
isSelected && theme.items.item.selected,
isDisabled && theme.items.item.disabled,
)}
onClick={() => {
if (isDisabled) return;
changeSelectedDate(currentDate, true);
}}
>
{day}
</button>
);
})}
</div>
</>
);
}
DatepickerViewsDays.displayName = "DatepickerViewsDays";