import * as React from 'react'; import { useState } from 'react'; import { ChevronLeft, ChevronRight, Calendar as CalendarIcon, } from 'lucide-react'; import { Button } from '@/figma-make/components/base/button'; import { Popover, PopoverContent, PopoverTrigger, } from '@/figma-make/components/base/popover'; import { Input } from '@/figma-make/components/base/input'; import { Label } from '@/figma-make/components/base/label'; interface DatePickerProps { date: string; onDateChange: (date: string) => void; disabled?: boolean; placeholder?: string; formatDisplay?: (date: string) => string; hasError?: boolean; } export function DatePicker({ date, onDateChange, disabled = false, placeholder = 'Select date', formatDisplay, hasError = false, }: DatePickerProps) { const [open, setOpen] = useState(false); const [currentMonth, setCurrentMonth] = useState(() => { const d = date ? new Date(date) : new Date(); return { year: d.getFullYear(), month: d.getMonth() }; }); const defaultFormatDisplay = (dateStr: string) => { if (!dateStr) return placeholder; const d = new Date(dateStr + 'T00:00:00'); return d.toLocaleDateString('id-ID', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', }); }; // const formatDateInput = (dateStr: string) => { // if (!dateStr) return ''; // const d = new Date(dateStr + 'T00:00:00'); // return d.toLocaleDateString('en-GB', { // day: '2-digit', // month: '2-digit', // year: 'numeric', // }); // }; const displayFormatter = formatDisplay || defaultFormatDisplay; const navigateMonth = (direction: 'prev' | 'next') => { const newDate = new Date( currentMonth.year, currentMonth.month + (direction === 'next' ? 1 : -1) ); setCurrentMonth({ year: newDate.getFullYear(), month: newDate.getMonth() }); }; const handleDateSelect = (dateStr: string) => { onDateChange(dateStr); setOpen(false); }; const handleManualInput = (value: string) => { onDateChange(value); setOpen(false); }; const renderCalendar = () => { const { year, month } = currentMonth; const firstDay = new Date(year, month, 1).getDay(); const adjustedFirstDay = firstDay === 0 ? 6 : firstDay - 1; // Monday = 0 const daysInMonth = new Date(year, month + 1, 0).getDate(); const monthName = new Date(year, month).toLocaleDateString('en-US', { month: 'long', }); const days = []; // Empty cells before first day for (let i = 0; i < adjustedFirstDay; i++) { days.push(
); } // Days of the month for (let day = 1; day <= daysInMonth; day++) { const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`; const isSelected = dateStr === date; const isToday = dateStr === new Date().toISOString().split('T')[0]; days.push( ); } return (
{monthName} {year}
{['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'].map((day) => (
{day}
))}
{days}
); }; return ( {renderCalendar()}
handleManualInput(e.target.value)} className='text-sm border-gray-200' />
); }