1.0.0 • Published 3 years ago

react-step-and-mark v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
3 years ago

React Step And Mark

A headless & type-safe multi-step UI builder.

Overview

React Step provides;

  • TypeScript support
  • A global state
  • Headless structure
  • Out-of-box form handler methods

Installation

Using npm:

$ npm i react-step-and-mark

Usage

Example:

<Steps>
	<Step title="My First Step" component={Step1} />
	<Step title="My Second Step" component={Step2} />
	<Step title="My Third Step" component={Step3} />
</Steps>

Config Object

Steps component accepts an optional config object for configuring the common navigation component or components that you'd like render before or after the Step components. These components are rendered along with every step component. Here is an example:

const Navigation = (props) => {
    return (
        <div>
            <button onClick={props.prev}>Previous</button>
            <button onClick={props.next}>Next</button>
        </div>
    );
}

const Before = (props) => {
	return <span>This component will be rendered before the Step components in every step</span>
}

const After = (props) => {
	return <span>This component will be rendered after the Step components in every step</span>
}

const config = {
	before: Before, // a React component with special props provided automatically
	after: After, // a React component with special props provided automatically
	navigation: {
		component: Navigation, // a React component with special props provided automatically
		location: "before" // or after
	}
}

<Steps config={config}>
	<Step title="My First Step" component={Step1} />
	<Step title="My Second Step" component={Step2} />
	<Step title="My Third Step" component={Step3} />
</Steps>

Documentation

Wrapper Components

Steps and Step are the only two wrapper components for creating your multi-step component.

ComponentDescription
<Steps />Wrapper component for Step components. Accepts a config object for rendering a common navigation component.
<Step />title: takes a title for the step, which can be accessed in props object of the step component. component: takes a React component that you would like to show in that step. beforeStepChange: takes a callback function to run right before the current step changes.

Step Component props

The React component that is passed to each Step wrapper component will be injected with the following props:

PropertyTypeDescription
props.ordernumberOrder number of the current step component
props.titlestringTitle of the current step component
props.progressnumberProgress of the current step, value between 0 and 1
props.nextfunctionFunction to move to the next step
props.prevfunctionFunction to move to the previous step
props.jumpfunction<step>Function to jump to the given step
props.isFirstfunctionFunction to check if the step is the first
props.isLastfunctionFunction to check if the step is the last
props.hasPrevfunctionFunction to check if the step has any previous step
props.hasNextfunctionFunction to check if the step has any next step
props.allStepsArray<{order, title}>Array of all available steps' title and order number
props.stateobjectCombined state value of all steps
props.setStatefunction<key, value>Function to set/update state by key
props.getStatefunction<key, defaultValue>Function to retrieve a state value by key
props.handleChangefunction<event>onChange event handler for form elements

Navigation Component props

PropertyTypeDescription
props.sizenumberTotal number of steps
props.currentstringCurrent step number
props.progressnumberProgress of the current step, value between 0 and 1
props.nextfunctionFunction to move to the next step
props.prevfunctionFunction to move to the previous step
props.jumpfunction<step>Function to jump to the given step
props.allStepsArray<{order, title}>Array of all available steps' title and order number
props.stateobjectCombined state value of all steps
props.setStatefunction<key, value>Function to set/update state by key
props.getStatefunction<key, defaultValue>Function to retrieve a state value by key
props.handleChangefunction<event>onChange event handler for form elements

Config Object

before

It accepts a function that returns some JSX.Element. This component's props object is automatically populated with the Steps component's state (see: NavigationComponentProps).

after

It accepts a function that returns some JSX.Element. This component's props object is automatically populated with the Steps component's state (see: NavigationComponentProps).

navigation

PropertyTypeDescription
component() => JSX.ElementThis component's props object is automatically populated with the Steps component's state (see: NavigationComponentProps).
location (optional)"before" || "after"Location of the navigation component.

Prop types of the step components for React / TypeScript

When developing your step components, you can utilize StepComponentProps type for your step component props.

Example:

import React from "react";
import { StepComponentProps } from "react-step-builder";

const Step1 = (props: StepComponentProps) => {
	return (
		<div>
			<input
				name="fname"
				value={props.getState("fname", "")}
				onChange={props.handleChange}
			/>
			<input
				name="lname"
				value={props.getState("lname", "")}
				onChange={props.handleChange}
			/>
		</div>
	);
};

export default Step1;

Example Navigation, Before, and After Components

If you'd like to add a persistent components to be shown on before or after every step, you may utilize NavigationComponentProps type for your custom Navigation, Before, or After components. Here is an example:

const Navigation = (props: NavigationComponentProps) => {
	return (
		<div>
			<button onClick={props.prev}>Previous</button>
			<button onClick={props.next}>Next</button>
		</div>
	);
};

const Before = (props: NavigationComponentProps) => {
	return (
		<span>This component will be rendered before the Step components</span>
	);
};

const After = (props: NavigationComponentProps) => {
	return <span>This component will be rendered after the Step components</span>;
};