|
1 | 1 | import { ChevronLeftIcon, ChevronRightIcon } from "@radix-ui/react-icons"; |
| 2 | +import { format } from "date-fns"; |
2 | 3 | import * as React from "react"; |
3 | | -import { DayPicker } from "react-day-picker"; |
| 4 | +import { DayPicker, DropdownProps } from "react-day-picker"; |
4 | 5 | import { cn } from "#/lib/utils"; |
5 | 6 | import { buttonVariants } from "#/components/ui/Button"; |
6 | 7 |
|
@@ -78,9 +79,31 @@ const DateInput = ({ value, onChange, className = "" }) => { |
78 | 79 | const IconLeft = () => <ChevronLeftIcon className="h-4 w-4" />; |
79 | 80 | const IconRight = () => <ChevronRightIcon className="h-4 w-4" />; |
80 | 81 |
|
| 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 | + |
81 | 101 | function Calendar({ |
82 | 102 | withTime = true, |
83 | 103 | withDateInput = true, |
| 104 | + fromYear = 1900, |
| 105 | + toYear = 2100, |
| 106 | + captionLayout = "dropdown-buttons" as const, |
84 | 107 | className, |
85 | 108 | classNames, |
86 | 109 | showOutsideDays = true, |
@@ -207,13 +230,17 @@ function Calendar({ |
207 | 230 | showOutsideDays={showOutsideDays} |
208 | 231 | selected={selected} |
209 | 232 | onSelect={handleDaySelect} |
| 233 | + captionLayout={captionLayout} |
| 234 | + fromYear={fromYear} |
| 235 | + toYear={toYear} |
210 | 236 | className={cn("p-3", className)} |
211 | 237 | classNames={{ |
212 | 238 | months: |
213 | 239 | "flex flex-col sm:flex-row space-y-4 sm:space-x-4 sm:space-y-0", |
214 | 240 | month: "space-y-4", |
215 | 241 | 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", |
217 | 244 | nav: "space-x-1 flex items-center", |
218 | 245 | nav_button: cn( |
219 | 246 | buttonVariants({ variant: "outline" }), |
@@ -244,7 +271,9 @@ function Calendar({ |
244 | 271 | components={{ |
245 | 272 | IconLeft, |
246 | 273 | IconRight, |
| 274 | + Dropdown: CalendarDropdown, |
247 | 275 | }} |
| 276 | + formatters={{ formatMonthCaption: (date) => format(date, "MMM") }} |
248 | 277 | footer={footerContent} |
249 | 278 | {...props} |
250 | 279 | /> |
|
0 commit comments