"use client" import * as React from "react" import { CalendarIcon, XIcon } from "lucide-react" import { Calendar, type CalendarProps } from "@/components/calendar/calendar" import { Button } from "@/components/ui/button" import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover" import { cn } from "@/lib/utils" import { parseDateKey } from "./date-utils" export type DatePickerLabels = CalendarProps["labels"] & { placeholder?: string selected?: string } export type DatePickerProps = Omit< CalendarProps, "mode" | "range" | "onRangeChange" | "labels" > & { placeholder?: string labels?: DatePickerLabels disabled?: boolean clearable?: boolean triggerVariant?: "default" | "compact" formatValue?: (value: string) => React.ReactNode triggerClassName?: string contentClassName?: string } function defaultFormatValue(value: string) { const date = parseDateKey(value) if (!date) return value return new Intl.DateTimeFormat("en-US", { dateStyle: "medium" }).format(date) } function DatePicker({ value, onValueChange, placeholder, labels, disabled = false, clearable = true, triggerVariant = "default", formatValue = defaultFormatValue, triggerClassName, contentClassName, className, ...calendarProps }: DatePickerProps) { const [open, setOpen] = React.useState(false) const hasValue = Boolean(value) const handleSelect = (nextValue: string) => { onValueChange?.(nextValue) setOpen(false) } const clearValue = () => { onValueChange?.("") setOpen(false) } const handleClear = (event: React.MouseEvent) => { event.preventDefault() event.stopPropagation() clearValue() } return (
} > {hasValue ? formatValue(String(value)) : placeholder ?? labels?.placeholder ?? "Select date"} {clearable && hasValue ? ( { if (event.key === "Enter" || event.key === " ") { event.preventDefault() clearValue() } }} > ) : null}
) } export { DatePicker }