175 lines
6.3 KiB
TypeScript
175 lines
6.3 KiB
TypeScript
import * as React from "react"
|
|
import { ChevronDown, ChevronLeft, ChevronRight, ChevronUp } from "lucide-react"
|
|
import {
|
|
DayPicker,
|
|
getDefaultClassNames,
|
|
type DayButtonProps,
|
|
type DayPickerProps,
|
|
} from "react-day-picker"
|
|
import { zhCN } from "react-day-picker/locale"
|
|
|
|
import { Button } from "@/shared/ui/button"
|
|
import { cn } from "@/shared/ui/utils"
|
|
|
|
function Calendar({
|
|
className,
|
|
classNames,
|
|
showOutsideDays = true,
|
|
...props
|
|
}: DayPickerProps) {
|
|
const defaultClassNames = getDefaultClassNames()
|
|
|
|
return (
|
|
<DayPicker
|
|
className={cn(
|
|
"group/calendar w-fit bg-background p-3 [--cell-size:2.25rem]",
|
|
className,
|
|
)}
|
|
classNames={{
|
|
root: cn("w-fit", defaultClassNames.root),
|
|
months: cn(
|
|
"relative flex flex-col gap-4 md:flex-row",
|
|
defaultClassNames.months,
|
|
),
|
|
month: cn("flex w-full flex-col gap-4", defaultClassNames.month),
|
|
nav: cn(
|
|
"absolute inset-x-0 top-0 flex w-full items-center justify-between gap-1",
|
|
defaultClassNames.nav,
|
|
),
|
|
button_previous: cn(
|
|
"size-(--cell-size) p-0 select-none",
|
|
defaultClassNames.button_previous,
|
|
),
|
|
button_next: cn(
|
|
"size-(--cell-size) p-0 select-none",
|
|
defaultClassNames.button_next,
|
|
),
|
|
month_caption: cn(
|
|
"flex h-(--cell-size) w-full items-center justify-center px-(--cell-size)",
|
|
defaultClassNames.month_caption,
|
|
),
|
|
caption_label: cn(
|
|
"select-none text-sm font-medium",
|
|
defaultClassNames.caption_label,
|
|
),
|
|
dropdowns: cn(
|
|
"flex h-(--cell-size) w-full items-center justify-center gap-1.5 text-sm font-medium",
|
|
defaultClassNames.dropdowns,
|
|
),
|
|
dropdown_root: cn(
|
|
"relative rounded-md border border-input shadow-xs has-focus:border-ring has-focus:ring-3 has-focus:ring-ring/50",
|
|
defaultClassNames.dropdown_root,
|
|
),
|
|
dropdown: cn("absolute inset-0 opacity-0", defaultClassNames.dropdown),
|
|
month_grid: cn("w-full border-collapse", defaultClassNames.month_grid),
|
|
weekdays: cn("flex", defaultClassNames.weekdays),
|
|
weekday: cn(
|
|
"flex-1 rounded-md text-[0.8rem] font-normal text-muted-foreground select-none",
|
|
defaultClassNames.weekday,
|
|
),
|
|
week: cn("mt-2 flex w-full", defaultClassNames.week),
|
|
day: cn(
|
|
"group/day relative aspect-square h-full w-full rounded-md p-0 text-center select-none",
|
|
defaultClassNames.day,
|
|
),
|
|
day_button: cn(
|
|
"relative z-10 flex aspect-square size-auto w-full min-w-(--cell-size) flex-col gap-1 border-0 leading-none font-normal group-data-[focused=true]/day:relative group-data-[focused=true]/day:z-10 group-data-[focused=true]/day:border-ring group-data-[focused=true]/day:ring-[3px] group-data-[focused=true]/day:ring-ring/50 data-[range-end=true]:rounded-md data-[range-start=true]:rounded-md data-[range-middle=true]:rounded-none data-[selected-single=true]:bg-primary data-[selected-single=true]:text-primary-foreground dark:hover:text-foreground [&>span]:text-xs [&>span]:opacity-70",
|
|
defaultClassNames.day_button,
|
|
),
|
|
range_start: cn(
|
|
"day-range-start rounded-md bg-muted",
|
|
defaultClassNames.range_start,
|
|
),
|
|
range_middle: cn(
|
|
"rounded-none bg-muted",
|
|
defaultClassNames.range_middle,
|
|
),
|
|
range_end: cn(
|
|
"day-range-end rounded-md bg-muted",
|
|
defaultClassNames.range_end,
|
|
),
|
|
selected: cn("rounded-md bg-muted", defaultClassNames.selected),
|
|
today: cn(
|
|
"rounded-md bg-muted text-foreground data-[selected=true]:rounded-none",
|
|
defaultClassNames.today,
|
|
),
|
|
outside: cn(
|
|
"text-muted-foreground opacity-50 data-[selected=true]:text-muted-foreground data-[selected=true]:opacity-30",
|
|
defaultClassNames.outside,
|
|
),
|
|
disabled: cn(
|
|
"text-muted-foreground opacity-50",
|
|
defaultClassNames.disabled,
|
|
),
|
|
hidden: cn("invisible", defaultClassNames.hidden),
|
|
...classNames,
|
|
}}
|
|
components={{
|
|
Chevron: ({ className: chevronClassName, orientation, style }) => {
|
|
const Icon =
|
|
orientation === "left"
|
|
? ChevronLeft
|
|
: orientation === "right"
|
|
? ChevronRight
|
|
: orientation === "up"
|
|
? ChevronUp
|
|
: ChevronDown
|
|
return (
|
|
<Icon
|
|
aria-hidden="true"
|
|
className={cn("size-4", chevronClassName)}
|
|
style={style}
|
|
/>
|
|
)
|
|
},
|
|
DayButton: CalendarDayButton,
|
|
}}
|
|
locale={zhCN}
|
|
showOutsideDays={showOutsideDays}
|
|
{...props}
|
|
/>
|
|
)
|
|
}
|
|
|
|
function CalendarDayButton({
|
|
className,
|
|
day,
|
|
modifiers,
|
|
...props
|
|
}: DayButtonProps) {
|
|
const ref = React.useRef<HTMLButtonElement>(null)
|
|
const defaultClassNames = getDefaultClassNames()
|
|
|
|
React.useEffect(() => {
|
|
if (modifiers.focused) {
|
|
ref.current?.focus()
|
|
}
|
|
}, [modifiers.focused])
|
|
|
|
return (
|
|
<Button
|
|
ref={ref}
|
|
variant="ghost"
|
|
size="icon"
|
|
data-day={day.date.toLocaleDateString()}
|
|
data-selected-single={
|
|
modifiers.selected &&
|
|
!modifiers.range_start &&
|
|
!modifiers.range_end &&
|
|
!modifiers.range_middle
|
|
}
|
|
data-range-start={modifiers.range_start}
|
|
data-range-end={modifiers.range_end}
|
|
data-range-middle={modifiers.range_middle}
|
|
className={cn(
|
|
"relative z-10 flex aspect-square size-auto w-full min-w-(--cell-size) flex-col gap-1 border-0 leading-none font-normal group-data-[focused=true]/day:relative group-data-[focused=true]/day:z-10 group-data-[focused=true]/day:border-ring group-data-[focused=true]/day:ring-[3px] group-data-[focused=true]/day:ring-ring/50 data-[range-end=true]:rounded-md data-[range-start=true]:rounded-md data-[range-middle=true]:rounded-none data-[selected-single=true]:bg-primary data-[selected-single=true]:text-primary-foreground dark:hover:text-foreground [&>span]:text-xs [&>span]:opacity-70",
|
|
defaultClassNames.day_button,
|
|
className,
|
|
)}
|
|
{...props}
|
|
/>
|
|
)
|
|
}
|
|
|
|
export { Calendar }
|