UNPKG

@kubit-ui-web/react-components

Version:

Kubit React Components is a customizable, accessible library of React web components, designed to enhance your application's user experience

46 lines 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useInternalValidations = void 0; const react_1 = require("react"); const inputType_1 = require("../types/inputType"); const internalErrors_1 = require("../types/internalErrors"); const EMAIL_REGEXP = /^([A-Za-z0-9_\-.])+@([A-Za-z0-9_\-.])+\.([A-Za-z]{2,4})$/; // WARNING!!: Use this hook with caution, in general, validations shouldn't be handle internally const useInternalValidations = (type, minLength, onInternalErrors) => { const [internalErrors, setInternalErrors] = (0, react_1.useState)([]); const checkInternalValidations = (value) => { const errors = []; // Email validation if (value && type === inputType_1.InputTypeType.EMAIL && !EMAIL_REGEXP.test(value)) { errors.push(internalErrors_1.InternalErrorType.INVALID_EMAIL); } // Min length validation const minLengthValidation = () => { if (type === inputType_1.InputTypeType.TEXT && minLength && value.length < minLength) { errors.push(internalErrors_1.InternalErrorType.INVALID_MIN_LENGTH); } }; minLengthValidation(); setInternalErrors(errors); onInternalErrors?.(errors); }; const addInternalError = (internalErrorType) => { const errors = new Set(internalErrors); errors.add(internalErrorType); setInternalErrors(Array.from(errors)); onInternalErrors?.(Array.from(errors)); }; const removeInternalError = (internalErrorType) => { const errors = new Set(internalErrors); errors.forEach(error => { if (error === internalErrorType) { errors.delete(error); } }); setInternalErrors(Array.from(errors)); onInternalErrors?.(Array.from(errors)); }; return { internalErrors, checkInternalValidations, addInternalError, removeInternalError }; }; exports.useInternalValidations = useInternalValidations; //# sourceMappingURL=useInternalValidations.js.map