0.3.2 • Published 3 years ago

step-wizard-react-hook v0.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Step Wizard for React Hook Form

This is an attempt to create a form that validates as the user goes through each step. For now, it's most useful if you want to split the form but still have validation.

Install

yarn add step-wizard-react-hook react-hook-form @hookform/resolvers yup

Code Example

import React from 'react';

import { StepWizardWrapper, StepWizardTab } from 'step-wizard-react-hook';

const MyStepWizard = StepWizardWrapper(<TabComponent />);

export const App = () => {
    return (
        <MyStepWizard onSubmit={(data) => console.log(data)}>
            <StepWizardTab name="Step 1">
                <p>Step 1</p>
            </StepWizardTab>

            <StepWizardTab name="Step 2">
                <p>Step 2</p>
            </StepWizardTab>

            <StepWizardTab name="Step 3">
                <p>Step 3</p>
            </StepWizardTab>
        </MyStepWizard>
    );
};

StepWizardWrapper

StepWizardWrapper needs to be called with the layout component inside so that it becomes StepWizardProvider. The Provider has the following properties and extends react-hook-form UseFormProps.

KeyTypeRequiredDescription
onSubmitSubmitHandler<FormValues>trueOn submit event for the last step.
aditionalData{ [key: string]: any }falseAny additional data you want to provide in case you don't want to use another Context.
childrenReact.ReactNodetrueTabs indicating each step of the form.

StepWizardTab

This component is used to help the lib gather important information regarding each step.

KeyTypeRequiredDescription
idstringfalseCreate a unique identifier if needed.
namestring or React.ReactNodetrueName for the layout tab bar.
validationSchemaanyfalseYup validation schema for the step.
childrenReact.ReactNodetrueTab UI component for the form step.

useStepWizard

This is the step wizard context containing the following data.

KeyTypeDescription
formRefReact.RefObject<HTMLFormElement>Reference to the wrapper form
tabsStepWizardTabProps[]List of all children tabs props.
stepnumberCurrent step.
currentTabStepWizardTabPropsCurrent step tab.
aditionalData{ [key: string]: any }Additional Data passed through Provider.
gotoStep(newStep?: number) => voidGo to a specific step.
nextStep() => voidGo to the next step.
previousStep() => voidGo to the previous step.