Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
smartstepper
Advanced tools
SmartStepper is a powerful React component that simplifies the creation of multi-step forms with state management, validation, and a flexible UI approach. It utilizes a state machine concept to manage step transitions, ensuring a smooth and controlled use
SmartStepper: A State Machine-Driven Multi-Step Form Component for React
Introduction
SmartStepper
is a powerful React component that simplifies the creation of multi-step forms with state management, validation, and a flexible UI approach. It utilizes a state machine concept to manage step transitions, ensuring a smooth and controlled user experience.
Key Features
useSmartStepper
Context:
useSmartStepper
hook for step components to access navigation methods:
navigateToNextStep
: Moves the stepper to the next step in the sequence.navigateToPreviousStep
: Moves the stepper to the previous step in the history.navigateToPreviousStepWithTargetStep
: Navigates to a specific step within the history stack.react-hook-form
for field-level validation within each step.Installation
npm install smartstepper
Basic Usage
Import necessary components:
import { SmartStepper, TSmartStepperSchema } from 'smartstepper';
import * as Yup from 'yup'; // Or your preferred schema library
import Step1 from './Step1';
import Step2 from './Step2';
// ... import other step components
Define your step schema:
const validationSchema = Yup.object().shape({
firstName: Yup.string().required('First name is required'),
lastName: Yup.string().required('Last name is required'),
});
const stepsSchema: TSmartStepperSchema<keyof InferType<typeof validationSchema>> = [
{
stepName: 'step1',
fieldsForValidation: ['firstName', 'lastName'],
component: <Step1 />,
},
// ... define other steps
];
Create your form component:
const MyForm = () => {
const onSubmit = (data) => {
// Handle form submission with validated data
console.log('Form submitted:', data);
};
return (
<SmartStepper
onSubmit={onSubmit}
resolver={yupResolver(validationSchema)} // Or your validation resolver
// ... other SmartStepper props
>
{stepsSchema.map((step) => (
<SmartStepper.Step key={step.stepName} stepName={step.stepName} fieldsForValidation={step.fieldsForValidation}>
{step.component}
</SmartStepper.Step>
))}
</SmartStepper>
);
};
export default MyForm;
Advanced Features
SmartStepper.Step
components within other step components. This enables you to organize complex forms with hierarchical relationships between steps.SmartStepper
automatically unregisters form fields when navigating to a previous step. This prevents issues with staleThe SmartStepper
component utilizes React Context to provide methods and form state to child components (steps) for interaction with the stepper functionality. Here's a breakdown of the context values and their relation to React Hook Form integration:
Context Values Exported by useSmartStepper
Hook:
navigateToNextStep
(function):
navigateToPreviousStep
(function):
navigateToPreviousStepWithTargetStep
(function):
registerStepperFields
(function):
useForm().register
from React Hook Form.getStepperFieldValues
(function):
useForm().getValues
from React Hook Form.setStepperFieldValues
(function):
useForm().setValue
from React Hook Form.stepperFieldResetter
(function):
useForm().reset
from React Hook Form.canNavigateToNextStep
(function):
fieldsForValidation
for that step.true
if validation passes or false
if there are errors.control
(object):
control
object directly provided by useForm
from React Hook Form.Important Notes:
useSmartStepper
are directly related to React Hook Form integration within SmartStepper
.SmartStepper
.By effectively utilizing these context values, step components can interact with the form state, trigger validation, navigate between steps, and manage field values seamlessly within the SmartStepper
framework. The integration with React Hook Form simplifies form validation and management, allowing you to focus on building clean and well-structured multi-step forms.
FAQs
smart-stepper is a powerful React component that simplifies the creation of multi-step forms with state management, validation, and a flexible UI approach. It utilizes a state machine concept to manage step transitions, ensuring a smooth and controlled us
We found that smartstepper demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.