npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-sweet-wizard

v1.1.0

Published

Your module to handle wizard steps in ReactJS applications easier

Downloads

45

Readme

React Sweet Wizard

Greenkeeper badge npm

npm version npm downloads

Coverage Status Build Status

Your module to handle with Form Wizards in ReactJS applications easier.

Why you shoud use React Sweet Wizard

Well, there are several selling points, actually!

  • 🤘 Full Typescript support: It embraces Typescript for real! It accepts interfaces and types as generics for their data structures and more!
  • 🏆 Better Developer Experience (DX): It enforces some conventions in order to make sure we got you covered! Issues with things such as specific components that needs to be used, which component is incorrect and context that needs to be used as wrapper will be raised straightaway on development environment.
  • 🎯 Covers different usage and scenarios: It covers simple usage, but also covers scenarios when you have steps being added dinamically, specific validation steps that needs to be added, data structures that should be passed on, etc. The sky is the limit here!
  • 🤩 No external dependencies: And that's exactly what you read. This is a zero-dependencies package, so you can integrate with Material-UI, Chakra-UI or any other Design System you might be using - including your own. Feel free to integrate with your workflow as you wish
  • 🗜 Small bundle: All of that in only 1.58KB

Install

You can get it on NPM installing react-sweet-wizard module as a project dependency.

npm install react-sweet-wizard --save
# or
yarn add react-sweet-wizard

You can also use the standalone UMD build by including dist/react-sweet-wizard.js in your page. If you do this you'll also need to include the dependencies. For example:

<script src="https://unpkg.com/react@<package-version></package-version>/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@<package-version>/dist/react-dom.js"></script>
<script src="https://unpkg.com/react-sweet-wizard/dist/umd/react-sweet-wizard.js"></script>

Setup

You'll need to import WizardProvider and add it into the root component of your application. So that you can create your wizard steps by using Steps, Step and control them via useWizardContext hook.

import {
  useWizardContext,
  WizardProvider,
  Steps,
  Step,
} from 'react-sweet-wizard';

const Progress = () => {
  const { activeStepIndex, steps } = useWizardContext();

  return (
    <div>
      State {activeStepIndex + 1} of {steps.length}
    </div>
  );
};

const WizardSteps = () => (
  <Steps>
    <Step key="0" id="0">
      <div>
        <p>step 1</p>
      </div>
    </Step>
    <Step key="1" id="1">
      <div>
        <p>step 2</p>
      </div>
    </Step>
  </Steps>
);

const App = () => (
  <WizardProvider>
    <Progress />
    <WizardSteps />
  </WizardProvider>
);

export default App;

Demo

Try out the demo on Stackblitz!

React Sweet Wizard Playground!

Run the tests

$ npm test

Run the build

$ npm run build

Run the bundlesize check

$ npm run bundlesize

Run the code lint

$ npm run lint

useWizardContext

This package exposes a hook with a few different methods which you can interact.

Using Generics for data structure steps

The hook accepts a generic as data structure. If you don't pass anything, it will use DefaultWizardStepProps as default one. Otherwise, it will use the one you defined.

To define a new interface and pass as a generic you should extends from DefaultWizardStepProps

import { useWizardContext, DefaultWizardStepProps } from 'react-sweet-wizard'
...
// Creating a data structure interface
// To be used for steps
interface MyWizardSteps extends DefaultWizardStepProps {
  name: string;
}
...
// Yay! You can use generics to enforce the data structure
// used for steps
const ctx = useWizardContext<MyWizardSteps>();
// ✨ where the magic happens ✨

Read-only values

  • steps: You can check how many steps are available on the wizard. Interesting to be used if you're adding dynamic nodes, as an example
  • activeStepIndex: A number referencing which step is visible on your wizard
  • isFirstStep: A boolean that can be used to check if the current step is the first one on your wizard
  • isLastStep: A boolean that can be used to check if the current step is the last one on your wizard

setSteps(steps: T[])

Sets a new value for steps to be used and accessed across components. It covers scenarios such as steps with complex data structures. It will use the defined Generic interface for it. Otherwise, it follows DefaultWizardStepProps interface.


import { useWizardContext, DefaultWizardStepProps } from 'react-sweet-wizard'

// Creating a data structure interface
// To be used for steps
interface MyWizardSteps extends DefaultWizardStepProps {
  name: string;
}
...
// Yay! You can use generics to enforce the data structure
// used for steps
const { setSteps } = useWizardContext<MyWizardSteps>();
// Updating the mapped steps
setSteps([
  { id: 0, name: 'first'},
  { id: 1, name: 'second'},
])
...

getActiveStep()

Returns a data structure value for the current step to be used and accessed across components. It covers scenarios such as steps with complex data structures.

It will use the defined Generic interface for it. Otherwise, it follows DefaultWizardStepProps interface.

import { useWizardContext } from 'react-sweet-wizard'
...
// Yay! You can use generics to enforce the data structure
// used for steps
const { getActiveStep, onNext } = useWizardContext();
// Adding 2 steps
setSteps([
  { id: 0 },
  { id: 1 },
])
// Returns the first step since the wizard is following the common flow
//   { id: 0 }
getActiveStep()
// Moving wizard to the second step
onNext()
// Returns the second step
//   { id: 1 }
getActiveStep()
...

onNext(cb?: () => void)

Moves the wizard to the next step. It also accepts a callback, in case you need to manage any specific handler. E.G.

import { useWizardContext } from 'react-sweet-wizard'
...
// Yay! You can use generics to enforce the data structure
// used for steps
const { setSteps, onNext } = useWizardContext();
...
// Adding 2 steps
setSteps([
  { id: 0},
  { id: 1},
  { id: 2},
])
// Moving wizard to the second step
onNext()
onNext(() => {
  alert('And here we go! 🚀');
  // You can add a specific validation here!
})

onPrevious()

Moves the wizard to the previous step.

import { useWizardContext } from 'react-sweet-wizard'
...
// Yay! You can use generics to enforce the data structure
// used for steps
const { setSteps, onPrevious } = useWizardContext();
...
// Adding 2 steps
setSteps([
  { id: 0},
  { id: 1},
])
// Moving wizard to the second step
onNext()
// Moving wizard back to the first step
onPrevious()

goTo(id: number | string)

Moves the wizard to a specific step. Interesting if you have some advanced scenario

import { useWizardContext, DefaultWizardStepProps } from 'react-sweet-wizard'

// Creating a data structure interface
// To be used for steps
interface MyWizardSteps extends DefaultWizardStepProps {
  name: string;
}
...
// Yay! You can use generics to enforce the data structure
// used for steps
const { setSteps, goTo, getActiveStep } = useWizardContext<MyWizardSteps>();
...
// Updating the mapped steps
setSteps([
  { id: 0, name: 'first'},
  { id: 1, name: 'second'},
])
// Moving wizard to second step
goTo(1)
//
const step = getActiveStep()
// Output
//   { id: 1, name: 'second'}
// It's using `MyWizardSteps` interface, so don't worry
console.log(step)

Publish

this project is using np package to publish, which makes things straightforward. EX: np <patch|minor|major>

For more details, please check np package on npmjs.com

Author

Wilson Mendes (willmendesneto)