Skip to content

Commit 5b92195

Browse files
feat: add month/year dropdown to calendar component
1 parent 2f34ade commit 5b92195

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/components/ui/Calendar.tsx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ChevronLeftIcon, ChevronRightIcon } from "@radix-ui/react-icons";
2+
import { format } from "date-fns";
23
import * as React from "react";
3-
import { DayPicker } from "react-day-picker";
4+
import { DayPicker, DropdownProps } from "react-day-picker";
45
import { cn } from "#/lib/utils";
56
import { buttonVariants } from "#/components/ui/Button";
67

@@ -78,9 +79,31 @@ const DateInput = ({ value, onChange, className = "" }) => {
7879
const IconLeft = () => <ChevronLeftIcon className="h-4 w-4" />;
7980
const IconRight = () => <ChevronRightIcon className="h-4 w-4" />;
8081

82+
const CalendarDropdown = ({
83+
value,
84+
onChange,
85+
children,
86+
className,
87+
}: DropdownProps) => (
88+
<div className={className}>
89+
<select
90+
value={value}
91+
onChange={onChange}
92+
// eslint-disable-next-line react/forbid-dom-props
93+
style={{ appearance: "none", WebkitAppearance: "none" as never }}
94+
className="text-sm py-1 gap-0 font-medium bg-background rounded-md border-none hover:bg-foreground/10 cursor-pointer focus:outline-none focus:ring-1 focus:ring-ring"
95+
>
96+
{children}
97+
</select>
98+
</div>
99+
);
100+
81101
function Calendar({
82102
withTime = true,
83103
withDateInput = true,
104+
fromYear = 1900,
105+
toYear = 2100,
106+
captionLayout = "dropdown-buttons" as const,
84107
className,
85108
classNames,
86109
showOutsideDays = true,
@@ -207,13 +230,17 @@ function Calendar({
207230
showOutsideDays={showOutsideDays}
208231
selected={selected}
209232
onSelect={handleDaySelect}
233+
captionLayout={captionLayout}
234+
fromYear={fromYear}
235+
toYear={toYear}
210236
className={cn("p-3", className)}
211237
classNames={{
212238
months:
213239
"flex flex-col sm:flex-row space-y-4 sm:space-x-4 sm:space-y-0",
214240
month: "space-y-4",
215241
caption: "flex justify-center pt-1 relative items-center",
216-
caption_label: "text-sm font-medium",
242+
caption_label: "hidden",
243+
caption_dropdowns: "flex items-center gap-1",
217244
nav: "space-x-1 flex items-center",
218245
nav_button: cn(
219246
buttonVariants({ variant: "outline" }),
@@ -244,7 +271,9 @@ function Calendar({
244271
components={{
245272
IconLeft,
246273
IconRight,
274+
Dropdown: CalendarDropdown,
247275
}}
276+
formatters={{ formatMonthCaption: (date) => format(date, "MMM") }}
248277
footer={footerContent}
249278
{...props}
250279
/>

0 commit comments

Comments
 (0)