Spaces:
Runtime error
Runtime error
File size: 1,364 Bytes
2e3220b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import { InputHTMLAttributes, useState } from 'react';
import { useHolidays } from '../hooks/useHolidays';
interface DateInputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'type' | 'onChange'> {
value: string;
onChange: (newDate: string) => void;
/** If true, allow selecting holiday dates anyway (default false). */
allowHolidays?: boolean;
}
export default function DateInput({ value, onChange, allowHolidays = false, className = '', ...rest }: DateInputProps) {
const holidays = useHolidays();
const [warn, setWarn] = useState<string | null>(null);
const handle = (e: React.ChangeEvent<HTMLInputElement>) => {
const v = e.target.value;
setWarn(null);
if (v && !allowHolidays && holidays.has(v)) {
setWarn('Esta fecha es feriado y no puede seleccionarse.');
// revert: do NOT call onChange, so the input stays at the previous value
e.target.value = value;
return;
}
onChange(v);
};
const isHoliday = !!value && holidays.has(value);
const errClass = warn || (!allowHolidays && isHoliday) ? 'border-red-300' : '';
return (
<div>
<input
type="date"
value={value}
onChange={handle}
className={`${className} ${errClass}`.trim()}
{...rest}
/>
{warn && <p className="text-xs text-red-600 mt-1">{warn}</p>}
</div>
);
}
|