react-fatless-form
Version:
A lightweight React form package designed for simplicity that simplifies form handling and validation without unnecessary complexity or bloat.
52 lines (51 loc) • 2.08 kB
TypeScript
import { useForm } from "../hooks/useForm";
/**
* Handles form submissions with validation, submission status tracking,
* and optional user feedback via toast notifications.
*
* This utility:
* - Validates the form using the provided resolver function
* - Tracks submission status (`"submitting"`, `"success"`, `"error"`)
* - Displays optional feedback via `feedbackManager` (e.g., toast)
* - Allows developers to suppress feedback manually or customize messages
*
* @template T - Form value type.
* @template R - Response type from the submission handler.
*
* @param {ReturnType<typeof useForm<T>>} form - The form instance managing state and validation
* @param {(values: T) => Partial<Record<keyof T, string>>} resolver - Validation function that returns errors object
* @param {(values: T) => Promise<R>} onSubmit - Async submission handler
* @param {(result: R) => void} [onSuccess] - Optional success callback
* @param {Object} [feedbackConfig] - Optional feedback configuration
* @param {string} [feedbackConfig.successMessage] - Fallback success message
* @param {string} [feedbackConfig.errorMessage] - Fallback error message
* @param {boolean} [feedbackConfig.showFeedback] - Toggle feedback visibility
*
* @returns {Promise<void>}
*
* @example - Using with Yup
* import { yupResolver } from "react-fatless-form";
*
* await handleSubmit(
* form,
* yupResolver(schema),
* async (values) => { ... }
* );
*
* @example - Using with Zod
* const zodResolver = (values) => {
* try {
* schema.parse(values);
* return {};
* } catch (error) {
* return error.flatten().fieldErrors;
* }
* };
*
* await handleSubmit(form, zodResolver, ...);
*/
export declare function handleSubmit<T extends Record<string, any>, R = void>(form: ReturnType<typeof useForm<T>>, resolver: (values: T) => Partial<Record<keyof T, string>>, onSubmit: (values: T) => Promise<R>, onSuccess?: (result: R) => void, feedbackConfig?: {
successMessage?: string;
errorMessage?: string;
showFeedback?: boolean;
}): Promise<void>;