0.1.1 • Published 7 years ago

react-sequence-wrapper v0.1.1

Weekly downloads
34
License
MIT
Repository
github
Last release
7 years ago

react-sequence-wrapper

a higher order component decorator providing state management for components that use a sequence of steps, like wizards and other multiple-step user interfaces.

npm

Installation

npm install react-sequence-wrapper --save-dev

Usage

Here's an example of using sequence decorator with the steps option to wrap a component and provide it with props to implement a basic wizard with forward and backwards navigation:

Edit react-sequence-wrapper@0.1.1

import React, { Component } from "react";
import { Label, Button } from "react-bootstrap";
import sequence from "react-sequence-wrapper";

@sequence({
  steps: ["Greet", "Compliment", "Leave"]
})
export default class WizardForm extends Component {
  render() {
    const stepComponents = [
      <div>hello world!</div>,
      <div>nice shirt!</div>,
      <div>goodbye!</div>
    ];

    const {
      currentStepIndex,
      currentStep,
      nextStep,
      prevStep,
      setStepIndex,
      setStep,
      isFirstStep,
      isLastStep,
      steps
    } = this.props; //all from @sequence
    return (
      <div>
        <div id="header">
          <h2>
            <Label bsStyle="success">
              {currentStepIndex + 1}. {steps[currentStepIndex]}
            </Label>
          </h2>
        </div>

        <div id="step">
          {stepComponents[currentStepIndex]}
        </div>

        <div id="basic-navigation">
          <Button onClick={prevStep} disabled={isFirstStep}>
            Back
          </Button>
          <Button bsStyle="primary" onClick={nextStep} disabled={isLastStep}>
            Next
          </Button>
        </div>

        <div id="steps-navigation">
          <h5>Steps Navigator</h5>

          {steps.map((step, index) => (
            <Button
              onClick={() => setStepIndex(index)}
              disabled={currentStepIndex == index}
              bsStyle={currentStepIndex == index ? "success" : "default"}
            >
              {index + 1}.{step}
            </Button>
          ))}
        </div>
      </div>
    );
  }
}

Dev Dependencies

License

MIT