Giter Site home page Giter Site logo

steps-wizard's Introduction

React Steps Wizard

Description

Easy navigation management between react components, usually used for wizards with BACK and NEXT buttons

Installation Steps:

  1. download and install nodeJS, if you don't have it already.
  2. open a terminal and run: npm i steps-wizard (in your project directory).

A usage example of the wizard:

CodeSandbox usage example

import { useStepsWizard, StepsWizard, Step } from "steps-wizard";

type SomeCompProps = {
  label: string;
  nextStepName?: string;
  loadAsync?: boolean
};

const SomeComp = ({ label, nextStepName,  loadAsync = false}: SomeCompProps) => {
  return (
    <>
    { loadAsync ? <ContentAsync label={label}/> :
                <Content label={label}/> }
      <Footer nextStepName={nextStepName}/>
    </>
  );
};

const sleep = (ms:number) => new Promise((resolve)=>{setTimeout(()=>resolve(),ms)})

const Content = ({label}: {label: string}) => <span> {label} </span>
  
const ContentAsync = ({label}: {label: string}) => {
  const [data, setData] = React.useState();
  const {
    meta
  } = useStepsWizard();
  if(!data){
    meta.setStepLoading(true);
    sleep(1000).then(()=>{
      setData('someData');
      meta.setStepLoading(false);
    })
  }

  return meta.isStepLoading ? <div>Loading Content...</div> : <span> {label} </span>
}

const Footer = ({nextStepName}: {nextStepName?: string}) => {
  const {
    setNextStep,
    setPreviouseStep,
    getHasPreviousStep,
    meta
  } = useStepsWizard();

  if(meta.isStepLoading) return <div>Loading Footer...</div>
  return (
      <div>
        {getHasPreviousStep() && <span onClick={setPreviouseStep}>{`<`}</span>}
      {nextStepName && (<span onClick={() => setNextStep(nextStepName)}>{`>`}</span>)}
      </div>
      )
}

export function App() {
  return (
    <div className="App">
      <h1>wizard usage</h1>
      <StepsWizard startStepName="one">
        <Step
          name="one"
          component={<SomeComp label="one" nextStepName="two" />}
        />
        <Step
          name="two"
          component={<SomeComp label="two" nextStepName="three" loadAsync />}
        />
        <Step name="three" component={<SomeComp label="three" />} />
      </StepsWizard>
    </div>
  );
}

const rootElement = document.getElementById("root");
render(<App />, rootElement);

steps-wizard's People

Contributors

dudiharush avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.