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-multistepper

v1.0.25

Published

Implement a step-by-step UI. Handling navigation and adding/removing steps.

Downloads

18

Readme

Multistepper

The purpose of this is to abstract our step-by-step form logic into a reuseable React/Redux component.

Actions/Reducers

steps.js

These actionCreators initiate the adding, removing and altering steps contained in an array.

There is also a submitSteps function that uses thunk to return a javascript Promise. This allows for async or sync handling of form submission.

stepCounter.js

These actionCreators implment an incremental counter as well as a function that will set to the counter to the specified value. This is used to indicate at what index of our steps array we are.

Selector

Selectors are useful in cases where we have to format our Redux state in a certain way for multiple React components.

The selectors we use here are getNextStep and getPreviousStep. These selectors return the index of the next or previous step, respectively.

Structure of our array of steps

The state handled by the steps.js actions and reducer is an array of objects. These objects can be structured however you want but the examples here and how we will implment it on GS...

[
	{
		component: (props) => (<p>Step 1</p>),
		conditional: (state) => state ? true : false
	},
	{
		component: ComponentThatWasImported,
		conditional: (state) => state ? true : false
	}
]

The only attribute that is tightly coupled to this structure is that conditional is a function that returns a boolean. conditional is used by the getNextStep and getPreviousStep selectors to determine if a step should be rendered or skipped.

And while it doesn't make much sense to use the Multistepper if your steps aren't going to do something with a component you technically could.

index.js

Let's start with an example of the use of our Multistepper.

import HelloWorld from 'hello-world.js';

class App extends Component {
  initialSteps = [
    {
      component: HelloWorld,
      conditional: () => true
    },
    {
      component: (props) => (<p>Step 2</p>),
      conditional: () => true
    }
  ]
  render() {
    return (
      <Multistepper initialSteps={this.initialSteps} >
        {
          (stepObject) => (
            stepObject ? <StepTemplate stepObject={stepObject} /> : <p>Loading...</p>
          )
        }
      </Multistepper>
    );
  }
}

We see here our Multistepper can take a prop of initialSteps which is an array of objects representing our steps. Also our Multistepper component expects to receive children in the form of a function.

Multistepper is a "Function as Child Component". For now, the only thing Multistepper does is figure out what the current step object is and passes that stepObject to its children. It is up to the user of the Multistepper component to determine what is done with the stepObject.

In our example we have a StepTemplate that takes the stepObject and expects it to have a component attribute that it will render amongst some other markup.

import React, { Component } from 'react';
import NextButton from '../buttons/next.js';

class StepTemplate extends Component {
  render() {
    let StepComponent = this.props.stepObject.component;

    return (
      <div className="App">
        <div className="App-intro">
          <StepComponent />
        </div>
        <NextButton buttonText="Next" />
      </div>
    );
  }
}

export default StepTemplate;

Navigation

In this example we aren't handling anything other than going to the next step who's conditional returns true. This is handled by the NextButton component.

import { getNextStep } from '../../multistepper/selector';
import { goToSpecificStep } from '../../multistepper/actions/stepCounter';

let mapStateToProps = function mapStateToProps(state) {
  return {
    nextStepIndex: getNextStep(state, state.stepCounter)
  }
};

let mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return bindActionCreators({
    goToSpecificStep
  }, dispatch);
};


class NextButton extends Component {
  handleClick = (event) => {
    event.preventDefault();
    this.props.goToSpecificStep(this.props.nextStepIndex);
  }
  render() {
    return (
      <button onClick={this.handleClick}>{this.props.buttonText}</button>
    );
  }
}

NextButton = connect(
  mapStateToProps,
  mapDispatchToProps
)(NextButton);

export default NextButton;