ui-next-library
Version:
Biblioteca de componentes UI para Next.js
82 lines (78 loc) • 5.33 kB
JavaScript
"use client";
var _excluded = ["placeholder", "Icon", "iconClassName", "errorMessage", "onChange", "onBlur", "value", "className", "register", "registerOptions"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React, { useState, forwardRef, memo } from "react";
import { FaEnvelope } from "react-icons/fa";
import Text from "./Text";
var Email = /*#__PURE__*/forwardRef(function (_ref, ref) {
var _props$errors$props$n;
var _ref$placeholder = _ref.placeholder,
placeholder = _ref$placeholder === void 0 ? "ejemplo@correo.com" : _ref$placeholder,
_ref$Icon = _ref.Icon,
Icon = _ref$Icon === void 0 ? FaEnvelope : _ref$Icon,
_ref$iconClassName = _ref.iconClassName,
iconClassName = _ref$iconClassName === void 0 ? "text-gray-400" : _ref$iconClassName,
_ref$errorMessage = _ref.errorMessage,
errorMessage = _ref$errorMessage === void 0 ? "Correo electrónico inválido" : _ref$errorMessage,
onChange = _ref.onChange,
onBlur = _ref.onBlur,
value = _ref.value,
_ref$className = _ref.className,
className = _ref$className === void 0 ? "" : _ref$className,
_ref$register = _ref.register,
register = _ref$register === void 0 ? null : _ref$register,
_ref$registerOptions = _ref.registerOptions,
registerOptions = _ref$registerOptions === void 0 ? {} : _ref$registerOptions,
props = _objectWithoutProperties(_ref, _excluded);
var _useState = useState(true),
_useState2 = _slicedToArray(_useState, 2),
isValid = _useState2[0],
setIsValid = _useState2[1];
var _useState3 = useState(""),
_useState4 = _slicedToArray(_useState3, 2),
localErrorMessage = _useState4[0],
setLocalErrorMessage = _useState4[1];
var validateEmail = function validateEmail(email) {
var re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(String(email).toLowerCase());
};
var handleChange = function handleChange(e) {
var email = e.target.value;
if (email && !validateEmail(email)) {
setIsValid(false);
setLocalErrorMessage(errorMessage);
} else {
setIsValid(true);
setLocalErrorMessage("");
}
onChange && onChange(e);
};
// Manejar error cuando el campo es requerido
var hasError = !isValid || props.required && !value || props.errors && props.errors[props.name];
var displayErrorMessage = localErrorMessage || (props.required && !value ? "Este campo es obligatorio" : "") || props.errors && ((_props$errors$props$n = props.errors[props.name]) === null || _props$errors$props$n === void 0 ? void 0 : _props$errors$props$n.message);
return /*#__PURE__*/React.createElement(Text, _extends({}, props, {
ref: ref,
type: "email",
placeholder: placeholder,
Icon: Icon,
iconClassName: iconClassName,
className: className,
onChange: handleChange,
error: hasError,
errorMessage: displayErrorMessage,
register: register,
registerOptions: registerOptions
}));
});
// Nombramiento explícito para DevTools
Email.displayName = "Email";
// Memoización para evitar renderizados innecesarios
export default /*#__PURE__*/memo(Email);