Docs
useMultiStepForm

Manage multi-step forms with the useMultiStepForm hook.

Loading...

Installation

pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-multi-step-form

Usage

"use client"
 
import { useMultiStepForm } from "@/hooks/use-multi-step-form"
 
export function Component() {
  const {
    currentStep,
    totalSteps,
    isFirstStep,
    isLastStep,
    next,
    previous,
    goTo,
    progress,
  } = useMultiStepForm({ totalSteps: 3 })
 
  return (
    <div>
      <p>
        Step {currentStep + 1} of {totalSteps}
      </p>
 
      <div>
        {currentStep === 0 && <Step1 />}
        {currentStep === 1 && <Step2 />}
        {currentStep === 2 && <Step3 />}
      </div>
 
      <div className="mt-4 flex justify-between">
        <button onClick={previous} disabled={isFirstStep}>
          Back
        </button>
        <button onClick={isLastStep ? () => alert("Submit") : next}>
          {isLastStep ? "Submit" : "Next"}
        </button>
      </div>
 
      <progress value={progress} max={1} className="mt-2 w-full" />
    </div>
  )
}

API Reference

Parameters

NameTypeDescriptionDefault ValueOptional
initialStepnumberThe initial step to start from.0Yes
totalStepsnumberThe total number of steps in the form.1No
onStepChange(step: number) => voidCallback function called when the step changes.() => {}Yes
allowLoopingbooleanWhether to allow looping back to the first step after the last step.falseYes

Return Values

NameTypeDescription
currentStepnumberThe current step index (0-based).
totalStepsnumberThe total number of steps in the form.
isFirstStepbooleanWhether the current step is the first step.
isLastStepbooleanWhether the current step is the last step.
next() => voidFunction to go to the next step.
previous() => voidFunction to go to the previous step.
goTo(step: number) => voidFunction to go to a specific step.
progressnumberThe progress of the form as a value between 0 and 1.
canGoNextbooleanWhether the next step can be accessed.
canGoPreviousbooleanWhether the previous step can be accessed.