|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import * as Popover from '@radix-ui/react-popover'; |
| 4 | +import { format } from 'date-fns'; |
| 5 | +import { ko } from 'date-fns/locale'; |
| 6 | +import { CalendarIcon } from 'lucide-react'; |
| 7 | +import { useState } from 'react'; |
| 8 | +import { DayPicker } from 'react-day-picker'; |
| 9 | +import 'react-day-picker/dist/style.css'; |
| 10 | + |
| 11 | +interface DatePickerProps { |
| 12 | + date: Date | undefined; |
| 13 | + setDate: (date: Date | undefined) => void; |
| 14 | +} |
| 15 | + |
| 16 | +const DatePicker = ({ date, setDate }: DatePickerProps) => { |
| 17 | + const [isOpen, setIsOpen] = useState(false); |
| 18 | + |
| 19 | + const handleSelectDate = (selectedDate: Date | undefined) => { |
| 20 | + setDate(selectedDate); |
| 21 | + setIsOpen(false); // 날짜 고르면 팝오버 닫기 |
| 22 | + }; |
| 23 | + |
| 24 | + return ( |
| 25 | + <Popover.Root open={isOpen} onOpenChange={setIsOpen}> |
| 26 | + <Popover.Trigger asChild> |
| 27 | + <button |
| 28 | + className={`bg-background flex items-center gap-1.5 rounded-md border-2 px-2 py-1 text-xs text-gray-600 transition-colors hover:bg-gray-200 ${ |
| 29 | + date |
| 30 | + ? 'bg-background border-[#d5dcfb] text-gray-900 hover:bg-gray-50' |
| 31 | + : 'bg-surface border-gray-200 text-gray-400 hover:text-gray-600' |
| 32 | + } focus:outline-none`} |
| 33 | + > |
| 34 | + <CalendarIcon size={16} /> |
| 35 | + {date ? format(date, 'yyyy-MM-dd') : <span>마감일 선택</span>} |
| 36 | + </button> |
| 37 | + </Popover.Trigger> |
| 38 | + |
| 39 | + <Popover.Portal> |
| 40 | + <Popover.Content |
| 41 | + className="animate-in fade-in zoom-in-95 z-50 rounded-xl border border-gray-100 bg-white p-2 shadow-xl duration-200" |
| 42 | + align="start" // 버튼의 왼쪽 라인에 맞춰서 열림 |
| 43 | + sideOffset={5} // 버튼과 5px 간격 |
| 44 | + > |
| 45 | + <DayPicker |
| 46 | + mode="single" // 날짜 하나만 선택 |
| 47 | + selected={date} // 현재 선택된 날짜 표시 |
| 48 | + onSelect={handleSelectDate} // 선택 핸들러 |
| 49 | + locale={ko} // 한국어 달력 (월, 요일) |
| 50 | + // 커스텀 스타일 (Tailwind 색상 적용) |
| 51 | + modifiersClassNames={{ |
| 52 | + selected: |
| 53 | + 'bg-purple-600 text-white hover:bg-purple-500 rounded-full', // 선택된 날짜 색상 |
| 54 | + today: 'text-purple-600 font-bold', // 오늘 날짜 색상 |
| 55 | + }} |
| 56 | + // DayPicker 기본 스타일 오버라이드 (선택사항) |
| 57 | + styles={{ |
| 58 | + head_cell: { width: '40px', color: '#9ca3af' }, |
| 59 | + cell: { width: '40px' }, |
| 60 | + day: { margin: 'auto', borderRadius: '50%' }, |
| 61 | + }} |
| 62 | + /> |
| 63 | + </Popover.Content> |
| 64 | + </Popover.Portal> |
| 65 | + </Popover.Root> |
| 66 | + ); |
| 67 | +}; |
| 68 | + |
| 69 | +export default DatePicker; |
0 commit comments