'use client' import { useState, useRef, useEffect } from 'react' import { DateRange, type Range, Calendar } from 'react-date-range' import { Icon } from '../../../atoms' import { format } from 'date-fns' import { es } from 'date-fns/locale' import { getGlobalStyle } from '../../../../helpers' import styles from './styles.module.css' interface InputDateProps { date: Date id?: string keySelection?: string label?: string maxDate?: Date minDate?: Date placeholder?: string style?: React.CSSProperties value?: Date withRange?: boolean disabled?: boolean showClearButton?: boolean onChange: (date: Date) => void onCleanValue?: () => void onSelect?: (ranges: Range[]) => void } export const InputDate: React.FC = ({ id = 'input-date', placeholder, value, label, date = null, style, showClearButton = false, keySelection, minDate, maxDate, withRange = false, disabled = false, onChange = () => { }, onCleanValue = () => { } }) => { const [open, setOpen] = useState(false) const componentRef = useRef(null) const handleOpen = (): void => { setOpen(!open) } const [selectionRange, setSelectionRange] = useState({ startDate: new Date(), endDate: new Date(), key: keySelection ?? 'selection' }) const handleSelect = (ranges: { selection: { startDate: Date, endDate: Date, key: string } }): void => { setSelectionRange(ranges.selection) onChange(ranges.selection.startDate) } const onChangeCalendar = (date: Date): void => { onChange(date) } // Function to handle clicks outside the component const handleClickOutside = (event: MouseEvent): void => { if ((componentRef.current != null) && !componentRef.current.contains(event.target as Node)) { setOpen(false) } } useEffect(() => { document.addEventListener('mousedown', handleClickOutside) return () => { document.removeEventListener('mousedown', handleClickOutside) } }, []) const formattedDate = format(new Date(date ?? new Date()), 'dd MMMM yyyy', { locale: es }) return (
{formattedDate} {showClearButton && }
{(!withRange && open) && console.log(focusedRange)} fixedHeight color={getGlobalStyle('--color-icons-primary')} onChange={onChangeCalendar} /> } {(open && withRange) && console.log(focusedRange)} rangeColors={[getGlobalStyle('--color-icons-primary')]} minDate={minDate} maxDate={maxDate} editableDateInputs={false} direction='vertical' /> }
) }