Files
co-lite/solvway-admin-portal/app/components/ui/date-picker.tsx
T
ngqileandClaude Opus 5.5 c908fbe9fc feat(admin-portal): port babyboo's date picker and calendar
Adds react-day-picker 10.0.1 (it brings date-fns). The portal bans native date
inputs, and the events and leads forms need a date field.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-27 00:23:56 +08:00

139 lines
4.9 KiB
TypeScript

"use client"
import * as React from "react"
import { CalendarIcon } from "lucide-react"
import type { Matcher } from "react-day-picker"
import { Button } from "@/app/components/ui/button"
import { Calendar } from "@/app/components/ui/calendar"
import { Input } from "@/app/components/ui/input"
import { Popover, PopoverAnchor, PopoverContent, PopoverTrigger } from "@/app/components/ui/popover"
import { dateToIso, formatDmy, isoToDate } from "@/app/lib/date-input"
import { cn } from "@/app/lib/utils"
import { useT } from "@/app/lib/i18n/MessagesProvider"
type DmyInputProps = Omit<React.ComponentProps<"input">, "value" | "onChange" | "type" | "readOnly"> & {
value: string
onOpen: () => void
}
const OPEN_KEYS = new Set(["Enter", " ", "ArrowDown"])
/** Read-only box showing DD/MM/YYYY; the calendar is the only way to change the value. */
function DmyInput({ value, onOpen, placeholder, className, ...props }: DmyInputProps) {
const t = useT()
// text-sm at every width: Input's mobile text-base cut "DD/MM/YYYY" in the 120px range boxes; read-only, so no iOS zoom.
return (
<Input
{...props}
type="text"
readOnly
inputMode="none"
aria-haspopup="dialog"
placeholder={placeholder ?? t("UiDatePlaceholder")}
value={formatDmy(value)}
className={cn("cursor-pointer text-sm", className)}
onClick={props.disabled ? undefined : onOpen}
onKeyDown={(e) => {
if (!props.disabled && OPEN_KEYS.has(e.key)) {
e.preventDefault()
onOpen()
}
}}
/>
)
}
/** Year-dropdown range and disabled days for a min/max pair ("yyyy-MM-dd"). */
function calendarBounds(min?: string, max?: string) {
const minDate = min ? isoToDate(min) : undefined
const maxDate = max ? isoToDate(max) : undefined
const disabled: Matcher[] = []
if (minDate) disabled.push({ before: minDate })
if (maxDate) disabled.push({ after: maxDate })
return {
startMonth: minDate ?? new Date(1920, 0),
endMonth: maxDate ?? new Date(new Date().getFullYear() + 10, 11),
disabled,
}
}
interface DatePickerProps {
value: string
onChange: (iso: string) => void
id?: string
"aria-label"?: string
"aria-labelledby"?: string
"aria-invalid"?: boolean
"aria-describedby"?: string
required?: boolean
disabled?: boolean
min?: string
max?: string
placeholder?: string
className?: string
}
function DatePicker({ value, onChange, required, min, max, className, ...inputProps }: DatePickerProps) {
const t = useT()
const [open, setOpen] = React.useState(false)
const anchorRef = React.useRef<HTMLDivElement>(null)
const selected = isoToDate(value)
const bounds = calendarBounds(min, max)
const label = inputProps["aria-label"]
const labelledby = inputProps["aria-labelledby"]
const buttonId = React.useId()
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverAnchor asChild>
<div ref={anchorRef} data-slot="date-picker" className={cn("relative h-9", className)}>
<DmyInput {...inputProps} required={required} value={value} onOpen={() => setOpen(true)} className="h-full pr-12" />
<PopoverTrigger asChild>
<Button
type="button"
variant="ghost"
size="icon"
disabled={inputProps.disabled}
id={buttonId}
aria-label={label ? t("UiOpenCalendarFor", { label }) : t("UiOpenCalendar")}
aria-labelledby={!label && labelledby ? `${buttonId} ${labelledby}` : undefined}
className="absolute top-0 right-0 h-full w-9"
>
<CalendarIcon />
</Button>
</PopoverTrigger>
</div>
</PopoverAnchor>
<PopoverContent className="w-auto p-3" align="start" onInteractOutside={(e) => {
// The boxes sit in PopoverAnchor, outside the content, so radix treats a click on them
// as outside and closes; keep it open (the box re-opens, the icon still toggles).
if (anchorRef.current?.contains(e.target as Node)) e.preventDefault()
}}>
<Calendar
mode="single"
selected={selected}
defaultMonth={selected ?? (bounds.endMonth < new Date() ? bounds.endMonth : undefined)}
startMonth={bounds.startMonth}
endMonth={bounds.endMonth}
disabled={bounds.disabled}
onSelect={(d) => {
if (!d) return // clicking the selected day again deselects; keep the value
onChange(dateToIso(d))
setOpen(false)
}}
/>
{!required ? (
<div className="flex justify-end border-t pt-2">
<Button type="button" variant="ghost" size="sm" onClick={() => { onChange(""); setOpen(false) }}>
{t("UiClear")}
</Button>
</div>
) : null}
</PopoverContent>
</Popover>
)
}
export { DatePicker, DmyInput, calendarBounds }
export type { DatePickerProps }