UNPKG

@hitachivantara/uikit-react-core

Version:
78 lines (77 loc) 3.45 kB
//#region src/BaseInput/validations.ts var isTypeValidationIgnored = (type) => { return type === "text" || type === "password"; }; /** * Checks whether any integrated validation, native or not, is active. */ var hasBuiltInValidations = (required, inputType, minCharQuantity, maxCharQuantity, validation, inputProps) => required || !isTypeValidationIgnored(inputType) || minCharQuantity != null || maxCharQuantity != null || validation != null || inputProps?.required != null || inputProps?.minLength != null || inputProps?.maxLength != null || inputProps?.min != null || inputProps?.max != null || inputProps?.type && !isTypeValidationIgnored(inputProps.type) || inputProps?.pattern != null; /** Returns the form element's validation state based in the validity state of the input. */ var computeValidationState = (inputValidity, isEmptyValue) => { if (inputValidity.valid && isEmptyValue) return "standBy"; return inputValidity.valid ? "valid" : "invalid"; }; /** * Returns a error message based in the validity state of the input. * * Only one error message is returned even if multiple validations fail. * Also, only required, minCharQuantity, maxCharQuantity and validationType have specific error messages. * * For further customization both status and statusMessage should be controlled and * set using the onBlur callback that receives both the value and the input validity object. */ var computeValidationMessage = (inputValidity, errorMessages) => { if (inputValidity.valid) return ""; if (inputValidity.valueMissing) return errorMessages.requiredError; if (inputValidity.tooLong) return errorMessages.maxCharError; if (inputValidity.tooShort) return errorMessages.minCharError; if (inputValidity.typeMismatch) return errorMessages.typeMismatchError; return errorMessages.error; }; /** * Returns a object describing the validity state of the input. * * It implements the native browser's ValidityState interface: * https://developer.mozilla.org/en-US/docs/Web/API/ValidityState */ var validateInput = (input, required, minCharQuantity, maxCharQuantity, validation) => { const inputValidity = { ...input?.validity, valid: input?.validity?.valid ?? true }; const value = input?.value; if (!value) { if (required) { inputValidity.valueMissing = true; inputValidity.valid = false; } } else { if (minCharQuantity !== null && value.length < minCharQuantity) { inputValidity.tooShort = true; inputValidity.valid = false; } if (maxCharQuantity !== null && value.length > maxCharQuantity) { inputValidity.tooLong = true; inputValidity.valid = false; } if (validation != null && !validation(value)) { inputValidity.customError = true; inputValidity.valid = false; } } return inputValidity; }; var DEFAULT_ERROR_MESSAGES = { /** The value when a validation fails. */ error: "Invalid value", /** The message that appears when there are too many characters. */ maxCharError: "The value is too long", /** The message that appears when there are too few characters. */ minCharError: "The value is too short", /** The message that appears when the input is empty and required. */ requiredError: "The value is required", /** The message that appears when the input is value is incompatible with the expected type. */ typeMismatchError: "Invalid value" }; //#endregion export { DEFAULT_ERROR_MESSAGES, computeValidationMessage, computeValidationState, hasBuiltInValidations, validateInput };