64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
"use client"
|
|
|
|
import { format } from "date-fns";
|
|
import { Calendar as CalendarIcon } from "lucide-react";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Calendar } from "@/components/ui/calendar";
|
|
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
|
import { useLocale, useTranslations } from "next-intl";
|
|
import { forwardRef } from "react";
|
|
import { localeMap } from "@/types";
|
|
import { enUS } from "date-fns/locale";
|
|
|
|
interface BirthdatePickerProps {
|
|
value?: string;
|
|
onChange?: (value: string) => void;
|
|
disabled?: boolean;
|
|
"aria-invalid"?: boolean;
|
|
}
|
|
|
|
export const BirthdatePicker = forwardRef<HTMLButtonElement, BirthdatePickerProps>(
|
|
({ value, onChange, disabled, "aria-invalid": ariaInvalid }, ref) => {
|
|
const t = useTranslations("MePage.items.profile.form.birthdate");
|
|
const date = value ? new Date(value) : undefined;
|
|
|
|
const handleSelect = (selected: Date | undefined) => {
|
|
if (selected && onChange) {
|
|
onChange(format(selected, "yyyy-MM-dd"));
|
|
}
|
|
};
|
|
|
|
const locale = useLocale();
|
|
const dateLocale = localeMap[locale as keyof typeof localeMap] ?? enUS;
|
|
|
|
return (
|
|
<Popover>
|
|
<PopoverTrigger asChild>
|
|
<Button
|
|
ref={ref}
|
|
type="button"
|
|
variant="outline"
|
|
disabled={disabled}
|
|
aria-invalid={ariaInvalid}
|
|
data-empty={!date}
|
|
className="data-[empty=true]:text-muted-foreground w-full justify-start text-left font-normal"
|
|
>
|
|
<CalendarIcon className="mr-2 h-4 w-4" />
|
|
{date ? format(date, "PPP", { locale: dateLocale }) : <span>{t("pick")}</span>}
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="w-auto p-0">
|
|
<Calendar
|
|
mode="single"
|
|
selected={date}
|
|
onSelect={handleSelect}
|
|
captionLayout="dropdown"
|
|
disabled={{ after: new Date() }}
|
|
/>
|
|
</PopoverContent>
|
|
</Popover>
|
|
);
|
|
}
|
|
);
|
|
|
|
BirthdatePicker.displayName = "BirthdatePicker"; |