1.0.1 β€’ Published 8 months ago

react-stepper-context v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

React Stepper Context

npm downloads license

A lightweight and flexible React Context for managing multi-step flows in React and React Native.
Whether you're building multi-step wizards, onboarding flows, or checkout processes, React Stepper Context provides a customizable and easy-to-use framework.


πŸš€ Features

  • Customizable Layouts: Fully control the design of your stepper and navigation components.
  • Built for React & React Native: Works seamlessly across platforms.
  • Flexible Step Management: Control locking, navigation, and form state at each step.
  • Lightweight API: Leverages React Context and hooks for clean and reusable logic.

Common Use Cases:

  • Onboarding flows
  • Multi-step forms
  • Checkout processes

πŸ“¦ Installation

Install the package via npm or yarn:

npm install react-stepper-context

or

yarn add react-stepper-context

πŸ›  Usage

React Stepper Context allows you to define steps, manage navigation, and handle state within a multi-step flow. Here’s a simple example:

Example Setup

import { ReactStepperContext } from 'react-stepper-context';

const steps = [
  {
    key: 'step1',
    component: <Step1 />,
    title: 'Step 1',
  },
  {
    key: 'step2',
    component: <Step2 />,
    title: 'Step 2',
    locked: true,
  },
];

function App() {
  return (
    <ReactStepperContext steps={steps}>
      {(currentStepComponent) => (
        <div className="wrapper">
          <Sidebar />
          {currentStepComponent}
        </div>
      )}
    </ReactStepperContext>
  );
}

Step Component Example

Each step manages its own form values and navigation logic using the useReactStepperContext hook.

Just import and use the useReactStepperContext hook, it will provide you with all the necessary functions and values.

import React from 'react';
import { useReactStepperContext } from 'react-stepper-context';

export const Step1: React.FC = () => {
  const { setCurrentStepValues, goToNextStep, goToPreviousStep } = useReactStepperContext(); 

  const handleSubmit = () => {
    setCurrentStepValues({ key: 'value' });
    goToNextStep();
  };

  return (
    <div>
      <h2>Step 1</h2>
      <button onClick={goToPreviousStep}>Previous</button>
      <button onClick={handleSubmit}>Next</button>
    </div>
  );
};

Sidebar Example

The sidebar dynamically displays the steps and their statuses, enabling navigation between steps.

import { useReactStepperContext } from 'react-stepper-context';

export const Sidebar = () => {
  const { steps, currentStepKey, goToStep } = useReactStepperContext(); 

  return (
    <div>
      {steps.map((step, index) => (
        <button key={step.key} onClick={() => goToStep(index)} disabled={step.locked}>
          {step.title} - {step.status}
        </button>
      ))}
    </div>
  );
};

πŸ“– API Reference

Enums

StepStatus

Represents the status of a step in the stepper.

ValueDescription
IDLEThe step is idle.
IN_PROGRESSThe step is currently in progress.
SUCCESSThe step is successfully completed.
ERRORThe step encountered an error.
WARNINGThe step has a warning.

Types

Step

Defines a single step in the stepper.

PropertyTypeDescription
keystringUnique identifier for the step.
titlestringTitle of the step.
componentJSX.ElementComponent rendered for this step.
metadataRecord<string, any> (optional)Metadata related to the step.
statusStepStatusThe current status of the step.
lockedboolean (optional)Whether the step is locked.

ReactStepperContextProps

Props required to initialize the ReactStepperContext.

PropertyTypeDescription
children(currentStepComponent: JSX.Element) => JSX.ElementFunction that renders the current step component in the layout.
steps(Omit<Step, 'status'> & { status?: StepStatus })[]Array of steps to configure the flow.
initialStepIndexnumber (optional)Index of the initial step (default: 0).
initialStepValues{ [key: string | number]: any } (optional)Initial values for form state across steps.

ReactStepperContextValueType

Values and methods available via the useReactStepperContext hook.

PropertyTypeDescription
stepsStep[]Array of steps in the stepper.
currentStepIndexnumberThe index of the current step.
setCurrentStepIndex(index: number) => voidUpdates the current step index.
currentStepKeystringThe key of the current step.
totalStepsCountnumberTotal number of steps in the stepper.
isThisLastStepbooleanWhether the current step is the last one.
isThisFirstStepbooleanWhether the current step is the first one.
allStepsValues{ [key: string \| number]: any }Form values from all steps.
goToNextStep(currentStepStatus?: StepStatus, nextStepStatus?: StepStatus, unlockNextStep?: boolean) => voidMoves to the next step with optional parameters for step statuses and unlocking.
goToPreviousStep() => voidMoves to the previous step.
goToStep(index: number) => voidMoves to the specified step by index.
setCurrentStepValues(values: { [key: string \| number]: any }) => voidUpdates the form values for the current step.
getCurrentStepValues() => { [key: string \| number]: any }Retrieves the form values for the current step.
lockStep(key: string) => voidLocks a specific step by key.
unlockStep(key: string) => voidUnlocks a specific step by key.
unlockNextStep() => voidUnlocks the next step.
isStepLocked(key: string) => booleanChecks if a specific step is locked.
isNextStepLocked() => booleanChecks if the next step is locked.
isPreviousStepLocked() => booleanChecks if the previous step is locked.
setCurrentStepStatus(status: StepStatus) => voidUpdates the status of the current step.
stepsStateStep[]Current state of all steps.
areAllStepsSuccessStatusbooleanWhether all steps have the SUCCESS status.
isCurrentStepAlreadySubmittedbooleanWhether the current step has already been submitted.

Usage Hooks

useReactStepperContext

Provides access to the context values and methods listed above, allowing full control over the stepper flow.

import { useReactStepperContext } from 'react-stepper-context';

const { steps, currentStepIndex, goToNextStep, lockStep } = useReactStepperContext();

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

πŸ“ Contribution

Contributions are welcome! If you find bugs or have ideas for improvements, feel free to open an issue or a pull request.

1.0.1

8 months ago

1.0.0

8 months ago