import React, { useRef } from 'react'; import { Calendar } from 'lucide-react'; import { DateRange } from '../types'; interface DateRangePickerProps { dateRange: DateRange; onChange: (range: DateRange) => void; } export const DateRangePicker: React.FC = ({ dateRange, onChange }) => { const startRef = useRef(null); const endRef = useRef(null); const formatDateForInput = (date: Date) => { // Format to local YYYY-MM-DD to avoid timezone shifts const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; }; const formatShortDate = (date: Date) => { return date.toLocaleDateString('pt-BR', { day: '2-digit', month: '2-digit', year: '2-digit', timeZone: 'America/Sao_Paulo' }); }; const parseLocalDate = (value: string) => { // Split "YYYY-MM-DD" and create date in local timezone to avoid UTC midnight shift if (!value) return null; const [year, month, day] = value.split('-'); return new Date(parseInt(year), parseInt(month) - 1, parseInt(day)); }; const handleStartChange = (e: React.ChangeEvent) => { const newStart = parseLocalDate(e.target.value); if (newStart && !isNaN(newStart.getTime())) { onChange({ ...dateRange, start: newStart }); } }; const handleEndChange = (e: React.ChangeEvent) => { const newEnd = parseLocalDate(e.target.value); if (newEnd && !isNaN(newEnd.getTime())) { // Set to end of day to ensure the query includes the whole day newEnd.setHours(23, 59, 59, 999); onChange({ ...dateRange, end: newEnd }); } }; const openPicker = (ref: React.RefObject) => { if (ref.current) { try { if ('showPicker' in HTMLInputElement.prototype) { ref.current.showPicker(); } else { ref.current.focus(); } } catch (e) { ref.current.focus(); } } }; return (
{/* Start Date */}
openPicker(startRef)} > {formatShortDate(dateRange.start)}
até {/* End Date */}
openPicker(endRef)} > {formatShortDate(dateRange.end)}
); };