137 lines
3.6 KiB
TypeScript
137 lines
3.6 KiB
TypeScript
"use client";
|
|
|
|
import dayjs from "dayjs";
|
|
import { useEffect, useState } from "react";
|
|
import { CalendarIcon } from "lucide-react";
|
|
|
|
import { Button } from "../button";
|
|
import { Calendar } from "../calendar";
|
|
import { Input } from "../input";
|
|
import { Popover, PopoverContent, PopoverTrigger } from "../popover";
|
|
|
|
import "./input-date-picker.css";
|
|
|
|
export interface InputDatePickerProps {
|
|
value?: Date;
|
|
defaultValue?: Date;
|
|
onChange?: (date: Date | undefined) => void;
|
|
placeholder?: string;
|
|
disabled?: boolean;
|
|
className?: string;
|
|
}
|
|
|
|
export function InputDatePicker({
|
|
value: controlledValue,
|
|
defaultValue,
|
|
onChange,
|
|
placeholder,
|
|
disabled,
|
|
className,
|
|
}: InputDatePickerProps) {
|
|
const isControlled = controlledValue !== undefined;
|
|
|
|
const [open, setOpen] = useState(false);
|
|
const [internalDate, setInternalDate] = useState<Date | undefined>(
|
|
defaultValue,
|
|
);
|
|
const [month, setMonth] = useState<Date | undefined>(
|
|
isControlled ? controlledValue : defaultValue,
|
|
);
|
|
|
|
// 실제로 사용할 date 값 (제어/비제어 모두 지원)
|
|
const date = isControlled ? controlledValue : internalDate;
|
|
|
|
// 표시할 문자열 값
|
|
const [displayValue, setDisplayValue] = useState(date);
|
|
|
|
// 제어 컴포넌트일 경우 외부 값 변경 감지
|
|
useEffect(() => {
|
|
if (isControlled) {
|
|
setDisplayValue(controlledValue);
|
|
setMonth(controlledValue);
|
|
}
|
|
}, [isControlled, controlledValue]);
|
|
|
|
// 내부 상태 변경 핸들러
|
|
const handleDateChange = (newDate: Date | undefined) => {
|
|
if (!isControlled) {
|
|
setInternalDate(newDate);
|
|
}
|
|
|
|
if (onChange) {
|
|
onChange(newDate);
|
|
}
|
|
|
|
setDisplayValue(newDate);
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col gap-3">
|
|
<div className="relative flex gap-2">
|
|
<Input
|
|
id="date"
|
|
type="date"
|
|
value={displayValue ? dayjs(displayValue).format("YYYY-MM-DD") : ""}
|
|
disabled={disabled}
|
|
className={`input-date-picker bg-background pr-10 ${className || ""}`}
|
|
onChange={(e) => {
|
|
const newDate = new Date(e.target.value);
|
|
setDisplayValue(newDate);
|
|
|
|
if (isValidDate(newDate)) {
|
|
handleDateChange(newDate);
|
|
setMonth(newDate);
|
|
}
|
|
}}
|
|
onKeyDown={(e) => {
|
|
if (e.key === "ArrowDown") {
|
|
e.preventDefault();
|
|
setOpen(true);
|
|
}
|
|
}}
|
|
placeholder={placeholder}
|
|
/>
|
|
<Popover open={open} onOpenChange={setOpen}>
|
|
<PopoverTrigger asChild>
|
|
<Button
|
|
id="date-picker"
|
|
variant="ghost"
|
|
color="white"
|
|
disabled={disabled}
|
|
className="absolute right-2 top-1/2 size-7 -translate-y-1/2 rounded-[0.4rem] border-none p-0"
|
|
>
|
|
<CalendarIcon className="!size-full" />
|
|
<span className="sr-only">Select date</span>
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent
|
|
className="w-auto overflow-hidden p-0"
|
|
align="end"
|
|
alignOffset={-8}
|
|
sideOffset={10}
|
|
>
|
|
<Calendar
|
|
mode="single"
|
|
selected={date}
|
|
captionLayout="dropdown"
|
|
month={month}
|
|
onMonthChange={setMonth}
|
|
onSelect={(newDate) => {
|
|
handleDateChange(newDate);
|
|
setOpen(false);
|
|
}}
|
|
/>
|
|
</PopoverContent>
|
|
</Popover>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function isValidDate(date: Date | undefined) {
|
|
if (!date) {
|
|
return false;
|
|
}
|
|
return !isNaN(date.getTime());
|
|
}
|