seti-ramesesv1
Version:
Reusable components and context for Next.js apps
34 lines (31 loc) • 1.42 kB
JavaScript
import { jsx } from 'react/jsx-runtime';
import { useState } from 'react';
import { Text } from './Text.js';
const isValidDate = (dateString) => {
if (!dateString)
return false;
const date = new Date(dateString);
return !isNaN(date.getTime());
};
const getToday = () => {
const today = new Date();
return today.toISOString().split("T")[0];
};
const DateField = ({ value, onChange, onBlur, required = false, validateOnBlur = true, helperText, min = "1900-01-01", max, ...props }) => {
const [internalValue, setInternalValue] = useState("");
const [touched, setTouched] = useState(false);
const inputValue = value ?? internalValue ?? "";
const hasError = (required && inputValue.trim() === "") || (inputValue.trim() !== "" && !isValidDate(inputValue));
const shouldShowError = touched && hasError;
const handleChange = (e) => {
const newValue = e.target.value;
onChange ? onChange(e) : setInternalValue(newValue);
};
const handleBlur = (e) => {
setTouched(true);
onBlur?.(e);
};
return (jsx(Text, { ...props, type: "date", value: inputValue, onChange: handleChange, onBlur: validateOnBlur ? handleBlur : onBlur, error: shouldShowError, helperText: shouldShowError ? "Please enter a valid date." : helperText, min: min, max: max ?? getToday() }));
};
export { DateField as default };
//# sourceMappingURL=Date.js.map