'use strict'; var react = require('react'); // src/useForm.ts // src/utils/addValueToObject.ts function addValueToObject(object, key, value) { for (const [mainKey, _] of Object.entries(object)) { if (mainKey === key) { object[mainKey] = value; return; } } object[key] = value; } // src/utils/zodValidation.ts function zodValidation(schema, inputs, errors) { const result = schema && schema.safeParse(inputs); if (result && !result.success) { result.error.issues.map((zodErrors) => { const errorKey = zodErrors.path[0].toString(); const errorValue = zodErrors.message; addValueToObject(errors, errorKey, errorValue); }); } } // src/useForm.ts function useForm({ defaultValues, validation, handleSubmit, schema }) { const [isSubmitting, setIsSubmitting] = react.useState(false); const [inputs, setInputs] = react.useState(defaultValues); const [errors, setErrors] = react.useState(null); const validationValues = (inputs2) => { const errors2 = {}; schema && zodValidation(schema, inputs2, errors2); validation && validation(inputs2, errors2); const checkErrors = Object.values(errors2).some((error) => error.length > 0); const hasErrors = checkErrors ? errors2 : null; setErrors(hasErrors); return hasErrors; }; return { errors, inputs, isSubmitting, handleChange: (ev) => { const name = ev.currentTarget.name; const value = typeof inputs[name] === "string" || typeof inputs[name] === "number" ? ev.currentTarget.value : ev.currentTarget.checked; setInputs({ ...inputs, [name]: typeof inputs[name] === "number" ? Number(value) : value }); }, onSubmit: (ev) => { ev.preventDefault(); setIsSubmitting(true); const isErrors = validationValues(inputs); if (!isErrors) { handleSubmit(inputs, ev); } setIsSubmitting(false); } }; } exports.useForm = useForm;