import { useState } from "react"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Button } from "@/components/ui/button"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { cn } from "@/lib/utils"; const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; /** Click the month/year straight to any month within a year, instead of * stepping one at a time - opens on the trigger's own year, not always the * current one, so jumping to a month a year ago doesn't first require * paging the popover back itself. */ export function MonthPicker({ value, onChange, maxDate, label, }: { value: Date; onChange: (date: Date) => void; /** No month after this one is selectable - typically "today". */ maxDate?: Date; label: string; }) { const [open, setOpen] = useState(false); const [year, setYear] = useState(value.getFullYear()); const maxYear = maxDate?.getFullYear(); const maxMonth = maxDate?.getMonth(); return ( { setOpen(o); if (o) setYear(value.getFullYear()); }} >
{year}
{MONTHS.map((m, i) => { const isFuture = maxYear != null && maxMonth != null && (year > maxYear || (year === maxYear && i > maxMonth)); const isSelected = year === value.getFullYear() && i === value.getMonth(); return ( ); })}
); }