seti-ramesesv1
Version:
Reusable components and context for Next.js apps
25 lines (22 loc) • 1.2 kB
JavaScript
import { jsx } from 'react/jsx-runtime';
import { useState } from 'react';
import { Text } from '../ui/Text.js';
const isValidEmail = (value) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value.trim());
const Email = ({ value, onChange, onBlur, required = false, helperText, validateOnBlur = true, ...props }) => {
const [internalValue, setInternalValue] = useState("");
const [touched, setTouched] = useState(false);
const inputValue = value ?? internalValue ?? "";
const hasError = (required && inputValue.trim() === "") || (inputValue.trim() !== "" && !isValidEmail(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: "email", value: inputValue, onChange: handleChange, onBlur: validateOnBlur ? handleBlur : onBlur, error: shouldShowError, helperText: shouldShowError ? "Please enter a valid email address." : helperText, required: required }));
};
export { Email as default };
//# sourceMappingURL=Email.js.map