Gathering detailed insights and metrics for react-zod-multistep-form
Gathering detailed insights and metrics for react-zod-multistep-form
Gathering detailed insights and metrics for react-zod-multistep-form
Gathering detailed insights and metrics for react-zod-multistep-form
A React hook for managing multi-step forms with Zod schema validation and react-hook-form integration. Simplifies navigation between form steps, handles form state, validation, and error management with ease.
npm install react-zod-multistep-form
Typescript
Module System
Node Version
NPM Version
TypeScript (71.2%)
JavaScript (28.8%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
10 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Oct 11, 2024
Latest Version
0.0.8
Package Id
react-zod-multistep-form@0.0.8
Unpacked Size
388.40 kB
Size
107.49 kB
File Count
7
NPM Version
10.9.0
Node Version
20.12.0
Published on
Oct 11, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
react-zod-multistep-form
is a lightweight React hook designed to simplify the
management of multi-step forms. It integrates react-hook-form
with Zod
schema validation to handle form state, validation, and error management across
multiple steps. This hook provides a seamless way to navigate between form steps
while maintaining robust validation and form control.
Zod
for schema-based validation at
each step to ensure type safety.react-hook-form
to handle form
state, registration, and validation.goToNextStep
and goToPreviousStep
functions to handle form step transitions while respecting validation.trigger
from react-hook-form
to
validate only the fields in the current step before allowing navigation to the
next step.1npm install react-zod-multistep-form
Here’s how to use the useMultiStepForm
hook in your application:
1import { z } from "zod"; 2 3const schema = z.object({ 4 name: z.string().min(2, "Name is required"), 5 age: z.number().min(18, "Must be at least 18"), 6}); 7 8type FormData = z.infer<typeof schema>;
1import { StepComponent } from "react-zod-multistep-form"; 2 3const NameStep: StepComponent<FormData> = ({ register, errors }) => ( 4 <div> 5 <label>Name</label> 6 <input {...register("name")} /> 7 {errors.name && <p>{errors.name.message}</p>} 8 </div> 9); 10 11const AgeStep: StepComponent<FormData> = ({ register, errors }) => ( 12 <div> 13 <label>Age</label> 14 <input type="number" {...register("age")} /> 15 {errors.age && <p>{errors.age.message}</p>} 16 </div> 17);
1const steps: Step<FormData>[] = [ 2 { component: NameStep, fields: ["name"] }, 3 { component: AgeStep, fields: ["age"] }, 4];
useMultiStepForm
in your form component1import useMultiStepForm from "react-zod-multistep-form"; 2 3const MultiStepForm = () => { 4 const { 5 CurrentStep, 6 goToNextStep, 7 goToPreviousStep, 8 isFirstStep, 9 isLastStep, 10 handleSubmit, 11 control, 12 errors, 13 register, 14 } = useMultiStepForm<FormData>({ 15 steps, 16 schema, 17 initialValues: { name: "", age: 0 }, 18 }); 19 20 const onSubmit = (data: FormData) => { 21 console.log(data); 22 }; 23 24 return ( 25 <form noValidate onSubmit={handleSubmit(onSubmit)}> 26 <CurrentStep control={control} register={register} errors={errors} /> 27 {!isFirstStep && ( 28 <button type="button" onClick={() => goToPreviousStep()}> 29 Back 30 </button> 31 )} 32 {!isLastStep && ( 33 <button type="button" onClick={() => goToNextStep()}> 34 Next 35 </button> 36 )} 37 {isLastStep && <button type="submit">Submit</button>} 38 </form> 39 ); 40};
trigger
for Field-Specific ValidationThe useMultiStepForm
hook leverages react-hook-form
's trigger
function to
validate only the fields present in the current step before moving to the next
one. This ensures that each form step only validates the fields it's responsible
for, enhancing performance and making the validation process more intuitive.
In the goToNextStep
function, the trigger
method is used with the list of
fields from the current step, which ensures that only the fields in that step
are validated before the form transitions to the next step. If validation fails,
the user is not allowed to move to the next step until the errors are resolved.
Example:
1const goToNextStep = useCallback(async () => { 2 const isValid = await trigger(steps[currentStepIndex].fields); // Only validate current step fields 3 if (!isValid) return; 4 5 setCurrentStepIndex((prevStep) => 6 prevStep < steps.length - 1 ? prevStep + 1 : prevStep 7 ); 8}, [currentStepIndex, steps, trigger]);
This approach ensures that each form step only validates the relevant fields, which makes the process more efficient and prevents unnecessary validation across the entire form.
1type FormNavButtonsProps = { 2 goToPreviousStep: () => void; 3 goToNextStep: () => Promise<void>; 4 isFirstStep: boolean; 5 isLastStep: boolean; 6}; 7 8export const FormNavButtons: React.FC<FormNavButtonsProps> = ({ 9 goToPreviousStep, 10 goToNextStep, 11 isFirstStep, 12 isLastStep, 13}) => { 14 return ( 15 <nav> 16 {!isFirstStep && ( 17 <button type="button" onClick={goToPreviousStep}> 18 Back 19 </button> 20 )} 21 {!isLastStep && ( 22 <button type="button" onClick={goToNextStep}> 23 Next 24 </button> 25 )} 26 {isLastStep && <button type="submit">Submit</button>} 27 </nav> 28 ); 29};
FormStepComponent
type to build
form components that handle form fields and errors.component
and fields
that correspond to the Zod schema.useMultiStepForm
hook: Manage form state, navigation, and
validation across steps.goToNextStep
and goToPreviousStep
functions to control navigation between steps.The useMultiStepForm
hook accepts the following parameters:
steps
: An array of form steps, each with a component
and a list of
fields
to validate.schema
: A Zod schema that defines the structure and validation rules for
your form data.initialValues
: The initial values for each form field.The hook also accepts a generic type that can be inferred from your Zod schema, allowing for type-safe form handling.
The hook returns an object containing the following properties:
CurrentStep
: The current form step component to render.currentStepIndex
: The index of the current step.setCurrentStepIndex
: A function to manually set the current step index.goToNextStep
: Function to move to the next step, ensuring the current
step's validation passes.goToPreviousStep
: Function to move to the previous step.isFirstStep
: Boolean indicating if the current step is the first step.isLastStep
: Boolean indicating if the current step is the last step.handleSubmit
: The form submission handler from react-hook-form
.control
: The control
object from react-hook-form
for managing form
fields.errors
: An object containing validation errors for each form field.register
: The register
function from react-hook-form
for registering
form fields.This project is licensed under the Apache License 2.0.
This package simplifies the process of building multi-step forms with Zod validation in React. If you encounter any issues or have suggestions for improvements, feel free to open an issue or contribute to the repository!
No vulnerabilities found.
No security vulnerabilities found.