SmartStepper is a modern, customizable React stepper component for building multi-step forms, wizards, and onboarding flows. Integrates seamlessly with react-hook-form, supports Yup/Zod validation, and offers a flexible, config-driven API. Perfect for dynamic forms, onboarding, checkout flows, and more.
smartstepper
is a powerful React component integrated with react-hook-form
that simplifies the creation of multi-step forms. It provides robust state management, validation, and a flexible UI approach, ensuring a smooth and controlled user experience.
- Leverages a state machine to track the current step, enabling efficient navigation between steps.
- Ensures controlled and predictable transitions, preventing unexpected behavior.
- All step logic, navigation, validation, and views are defined in a single
config
object. - No need to define steps as children or schema arrays.
- Navigation functions (
next
andprevious
) receive the current form values as their argument and should return the name of the next or previous step.
orchestration: {
user: {
next: (data) => data.fullName ? 'address' : 'user'
},
address: {
next: () => 'confirm',
previous: (data) => data.fullName ? 'user' : 'address',
},
confirm: { previous: () => 'address' },
}
- Arguments:
next(data: FieldValues): StepName
previous(data: FieldValues): StepName
data
is the current form values object.
- Encourages separation between step logic and UI rendering.
- Step components define their content, while the stepper handles transitions and context.
- Allows for custom UI components within each step for a tailored user experience.
- Provides a
useSmartStepper
hook for step components to access navigation methods and form control:navigateToNextStep
: Moves to the next step in the sequence. Optionally accepts a target step name and unregister flag.navigateToPreviousStep
: Moves to the previous step in the history.registerStepperFields
,getStepperFieldValues
,setStepperFieldValues
,stepperFieldResetter
,canNavigateToNextStep
,control
for React Hook Form integration.
- Seamlessly integrates with
react-hook-form
for field-level validation within each step. - Utilizes Yup or Zod for defining validation rules.
- Triggers validation before advancing to the next step, ensuring data integrity.
- Automatically unregisters form fields when navigating to a previous step.
- Prevents stale data from persisting and affecting validation in subsequent steps.
- Maintains a clean form state and improves performance.
npm install smartstepper
Note: You must also install the following peer dependencies in your project:
react
react-hook-form
yup
(for Yup validation) and/orzod
(for Zod validation)
import {
SmartStepper,
useSmartStepper,
Controller,
type FieldValues,
type SmartStepperConfig,
} from 'smartstepper';
import * as yup from 'yup';
Each step is a React component that uses the useSmartStepper
hook and the Controller
component:
const UserInfoStep = () => {
const { navigateToNextStep, control } = useSmartStepper();
return (
<div>
<Controller
control={control}
name="fullName"
render={({ field }) => (
<input {...field} />
)}
/>
<button type="button" onClick={() => navigateToNextStep()}>Next</button>
</div>
);
};
const config: SmartStepperConfig<'user' | 'address' | 'confirm'> = {
start: 'user',
orchestration: {
user: {
next: (data) => data.fullName ? 'address' : 'user'
},
address: {
next: () => 'confirm',
previous: (data) => data.fullName ? 'user' : 'address',
},
confirm: { previous: () => 'address' },
},
validations: {
user: {
schema: yup.object({
fullName: yup.string().required('Full Name is required'),
email: yup.string().email().required('Email is required'),
}),
defaultValues: { fullName: '', email: '' },
},
address: {
schema: yup.object({
city: yup.string().required(),
zip: yup.string().required(),
}),
defaultValues: { city: '', zip: '' },
},
confirm: {
schema: yup.object(),
defaultValues: {},
},
},
views: {
user: { component: <UserInfoStep /> },
address: { component: <AddressStep /> },
confirm: { component: <ConfirmStep /> },
},
onSubmit: (data: FieldValues) => {
console.log('Final submission', data);
alert('Form submitted successfully!');
},
};
const MyMultiStepForm = () => <SmartStepper config={config} />;
export default MyMultiStepForm;
You can completely customize the UI of each step by creating your own step components that render the desired content. The stepper itself handles state management and navigation, allowing you to focus on building step-specific UI elements.
You can provide a wrapper
React element for each step in the views
config to wrap the step content (e.g., for cards or styling).
smartstepper
automatically unregisters form fields when navigating to a previous step. This prevents issues with stale data.
The SmartStepper
component utilizes React Context to provide methods and form state to step components for interaction with the stepper functionality. Here are the main context values and hooks:
Function/Hook | Description |
---|---|
navigateToNextStep |
Triggers navigation to the next step in the sequence. Optionally accepts a target step name and unregister flag. |
navigateToPreviousStep |
Triggers navigation to the previous step in the history stack. |
registerStepperFields |
React Hook Form integration: A wrapper around useForm().register for registering form fields. |
getStepperFieldValues |
React Hook Form integration: A wrapper around useForm().getValues for retrieving current field values. |
setStepperFieldValues |
React Hook Form integration: A wrapper around useForm().setValue for updating specific field values. |
stepperFieldResetter |
React Hook Form integration: A wrapper around useForm().reset for resetting the entire form state. |
canNavigateToNextStep |
React Hook Form integration: Triggers validation for the current step's fields, returns a Promise of validity. |
control |
React Hook Form integration: The control object provided by useForm , for use with Controller or useController . |
useSmartStepper |
Custom hook to access the SmartStepper context in your step components. |
useSmartStepperController |
Custom hook to create a react-hook-form controller bound to the SmartStepper's form control. |
By effectively utilizing these context values and hooks, step components can interact with the form state, trigger validation, navigate between steps, and manage field values seamlessly within the smartstepper
framework.
This project is licensed under the MIT License.