Skip to content

Commit 1564145

Browse files
committed
add: send custom caption to calendar component
1 parent d306f51 commit 1564145

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

components/ui/calendar.tsx

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import * as React from "react";
44

5-
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
5+
import { CaptionProps, DayPicker, useNavigation } from "react-day-picker";
6+
import {
7+
ChevronLeft,
8+
ChevronLeftIcon,
9+
ChevronRight,
10+
ChevronRightIcon,
11+
} from "lucide-react";
612
import {
713
Select,
814
SelectContent,
@@ -11,16 +17,34 @@ import {
1117
SelectValue,
1218
} from "./select";
1319
import { enUS, es } from "date-fns/locale";
20+
import { format, formatDate } from "date-fns";
1421

15-
import { DayPicker } from "react-day-picker";
1622
import { buttonVariants } from "./button";
1723
import { capitalizeFirstDateLetter } from "@/utils/date";
1824
import { cn } from "@/lib/utils";
19-
import { format } from "date-fns";
2025
import { useLocale } from "next-intl";
2126

2227
export type CalendarProps = React.ComponentProps<typeof DayPicker>;
23-
28+
function CustomCaptionComponent(props: CaptionProps) {
29+
const { goToMonth, nextMonth, previousMonth } = useNavigation();
30+
return (
31+
<div className='flex items-center justify-between '>
32+
<button
33+
disabled={!previousMonth}
34+
onClick={() => previousMonth && goToMonth(previousMonth)}
35+
>
36+
<ChevronLeft />
37+
</button>
38+
<h2 className='text-lg'>{formatDate(props.displayMonth, "MMM yyy")} </h2>
39+
<button
40+
disabled={!nextMonth}
41+
onClick={() => nextMonth && goToMonth(nextMonth)}
42+
>
43+
<ChevronRight />
44+
</button>
45+
</div>
46+
);
47+
}
2448
function Calendar({
2549
className,
2650
classNames,
@@ -101,6 +125,7 @@ function Calendar({
101125
...classNames,
102126
}}
103127
components={{
128+
Caption: CustomCaptionComponent,
104129
IconLeft: ({ ...props }) => <ChevronLeftIcon className='h-4 w-4' />,
105130
IconRight: ({ ...props }) => <ChevronRightIcon className='h-4 w-4' />,
106131
Dropdown: ({ ...props }) => (

components/ui/themed-calendar.tsx

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,10 @@
11
"use client";
22

3-
import { CaptionProps, useNavigation } from "react-day-picker";
4-
import { ChevronLeft, ChevronRight } from "lucide-react";
5-
63
import { Calendar as CalendarComponent } from "@/components/ui/calendar";
74
import { buttonVariants } from "./button";
85
import { cn } from "@/lib/utils";
9-
import { formatDate } from "date-fns";
106
import { useSettingsContext } from "@/contexts/SettingsContext";
117

12-
function CustomCaptionComponent(props: CaptionProps) {
13-
const { goToMonth, nextMonth, previousMonth } = useNavigation();
14-
return (
15-
<div className='flex items-center justify-between '>
16-
<button
17-
disabled={!previousMonth}
18-
onClick={() => previousMonth && goToMonth(previousMonth)}
19-
>
20-
<ChevronLeft />
21-
</button>
22-
<h2 className='text-lg'>{formatDate(props.displayMonth, "MMM yyy")} </h2>
23-
<button
24-
disabled={!nextMonth}
25-
onClick={() => nextMonth && goToMonth(nextMonth)}
26-
>
27-
<ChevronRight />
28-
</button>
29-
</div>
30-
);
31-
}
328
export function ThemedCalendar(
339
props: React.ComponentProps<typeof CalendarComponent>
3410
) {
@@ -58,9 +34,6 @@ export function ThemedCalendar(
5834
today: "text-green-500",
5935
selected: "bg-green-500 text-white hover:bg-green-500 hover:text-white",
6036
}}
61-
components={{
62-
Caption: CustomCaptionComponent,
63-
}}
6437
/>
6538
);
6639
}

0 commit comments

Comments
 (0)