import { useState } from "react"; const validatePhoneNumber = (phone) => { // Regex for exactly 10 digits const phoneRegex = /^\d{10}$/; return phoneRegex.test(phone); }; export const validateForm = (formData, validationRules, setErrMsg) => { const errors = {}; Object.keys(validationRules).forEach((field) => { const rules = validationRules[field]; const value = formData[field]; if ( rules.required && (!value || (typeof value === "string" && value.trim() === "")) ) { errors[field] = "Enter This Field"; } else if (rules.email && !validateEmail(value)) { errors[field] = "Invalid Email"; } else if (rules.custom && !rules.custom(value)) { errors[field] = rules.customMessage || "Invalid value"; } else if (rules.fileType && value && value instanceof File) { // Validate file type if (!rules.fileType.includes(value.type)) { errors[field] = "Invalid file type"; } // Validate file size if (rules.fileSize && value.size > rules.fileSize) { errors[field] = "File is too large"; } } else if (rules.phoneNumber && !validatePhoneNumber(value)) { // Validate phone number errors[field] = "Invalid phone number. Must be exactly 11 digits."; } }); setErrMsg(errors); return Object.keys(errors).length === 0; // Return true if no errors }; const validateEmail = (email) => { const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailPattern.test(email); }; export const FormField = ({ label, type, name, value, onChange, error, options, className, onSubmit, accept, placeholder, required, checked, ref, style, disabled, }) => { const [showPassword, setShowPassword] = useState(false); const handleTogglePasswordVisibility = () => { setShowPassword(!showPassword); }; return ( <>
{label && ( )} {type === "select" ? ( ) : type === "selectbyname" ? ( ) : type === "file" ? ( ) : type === "password" ? (
) : type === "email" ? ( ) : type === "text" ? ( ) : type === "textarea" ? ( ) : type === "date" ? ( ) : type === "datetime" ? ( ) : type === "tel" ? ( ) : type === "radio" ? (
{options?.map((option, index) => (
))}
) : type === "checkbox" ? ( ) : ( )} {error && (

{error}

)}
{onSubmit && ( )}
); }; // export const XLFormat = (data) => { // // Create a new workbook and add a worksheet // const wb = XLSX.utils.book_new(); // const ws = XLSX.utils.json_to_sheet(data); // // Add the worksheet to the workbook // XLSX.utils.book_append_sheet(wb, ws, "Sheet1"); // // Write the workbook and trigger a download // XLSX.writeFile(wb, "Applicants Data.xlsx"); // }; // export const downloadExcel = (data) => { // if (!data || typeof data !== 'object') { // console.error("Invalid data: Data should be an object."); // return; // } // try { // // Convert the object to an array of objects // const formattedData = Object.keys(data).map(key => { // return { [key]: data[key] }; // Convert object to an array of key-value pairs // }); // // Now we have an array of objects, each object has a key-value pair from the original object // const ws = XLSX.utils.json_to_sheet(formattedData); // Convert JSON to worksheet // const wb = XLSX.utils.book_new(); // Create a new workbook // XLSX.utils.book_append_sheet(wb, ws, 'Events'); // Append worksheet to workbook // // Create a Blob with the Excel file and trigger download // XLSX.writeFile(wb, 'event_data.xlsx'); // } catch (error) { // console.error("Error during Excel download:", error); // } // }; // export const useSetState = (initialState) => { // const [state, setState] = useState(initialState); // const newSetState = (newState) => { // setState((prevState) => ({ ...prevState, ...newState })); // }; // return [state, newSetState]; // }; // export const Dropdown = (arr, label) => { // const array = arr?.map((item) => ({ value: item?.id, label: item[label] })); // return array; // };