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-finite-state-machine

v1.1.0

Published

React finite state machine

Downloads

7

Readme

React Finite State Machine

Documentation in Russian

React component that implements the logic of the finite-state machine. Allows describing the component's state and the logic behind interstate transitions, transferring useful data between stages.

Prop Types

FSM Component Props

| Property | Type | Required? | Description | |:---|:---|:---:|:---| | meta | Meta | ✓ | Metdata. Description of stages. | | onFinish | OnFinish | | Hook activated after the transition to the final stage, finish() method call in the component. | | beforeTransition | BeforeTransition | | Hook activated before the transition to the preset stage. It's possible to interrupt the transition to the preset stage by returning false or Promise. | | afterTransition | AfterTransition | | Hook activated after the transition to the preset stage. | | Layout | React.ReactType | | General layout for all nodes of a finite state machine that receives information about the current step and children for rendering | | commonProps | any | | General parameters that will be sent to each node component of a finite state machine |

type StageName = string | number;

/**
 * History of interstage transitions
 */
interface IHistory {
	/** Number of records in history */
	recordsCount: number;

	/** Link to the first record's object in history */
	first: IStage | null;

	/** Link to the last record's object in history */
	last: IStage | null;

	/**
	 * Method of adding a new record to history
	 * @param {StageName} stageName Stage name
	 * @param {any} payload
	 * @returns {void}
	 */
	add: (stageName: StageName, payload: any) => void;
}

type Meta<Stage extends StageName = string> = {
	/** List of stages and corresponding components ('name-component' pair) */
	stages: {
		[name in Stage]: React.ElementType<StageComponentProps>;
	};

	/** Initial stage */
	initialStage: Stage;

	/** Final stage */
	finalStage: Stage;
};

type OnFinish: (transitionsHistory: IHistory) => void;

type BeforeTransition = (currentStage: StageName, nextStage: StageName, payload?: any) => boolean;

type AfterTransition = (currentStage: StageName, prevStage: StageName, payload?: any) => void;

type CommonProps = any;

type Layout = React.ElementType<LayoutComponentProps>;

Stage Component Props

| Property | Type | Description | |:---|:---|:---| | transition | Transition | Method of transition to the present stage. Can accept useful data for transferring to the next stage. | | toPrevious | ToPrevious | Method of returning to the preceding stage. Can accept useful data for transferring to the previous stage. | | finish | () => void | Calls onFinish hook. | | payload | IncomingPayload | Data transferred from the preceding stage. | | commonProps | any | General parameters that will be sent to each node component of a finite state machine |

type IncomingPayload = {} | void;

type OutGoingPayload = {} | void;

type ToPrevPayload = OutGoingPayload;

type Transition = (stageName: StageName, payload?: OutGoingPayload) => Promise<void>;

type ToPrevious = (payload?: ToPrevPayload) => Promise<void>;

type CommonProps = any;

Examples

import React from 'react';
import ReactDOM from 'react-dom';
import {FSM} from 'react-fsm';

const commonProps = {
	key: 'value'
};

const Layout = ({children, currentStage, currentStagePayload}) => (
	<div>
		{children}
	</div>
);

const Stage = {
	Stage1: 'Stage1',
	Stage2: 'Stage2',
	Stage3: 'Stage3'
};

const formsMeta = {
	stages: {
		[Stage.Stage1]: ({transition}) => (
			<div onClick={() => transition(Stage.Stage2)}>
				Stage 1
			</div>
		),
		[Stage.Stage2]: ({transition}) => (
			<div onClick={() => transition(Stage.Stage3)}>
				Stage 2
			</div>
		),
		[Stage.Stage3]: ({finish}) => (
			<div onClick={() => finish()}>
				Stage 3
			</div>
		)
	},
	initialStage: Stage.Stage1,
	finalStage: Stage.Stage3
};

const Form = () => (
	<FSM
		meta={formsMeta}
		afterTransition={(currentStage, prevStage, payload) => {
			console.log(
				`We've made a transition to the stage ${currentStage}`,
				`From stage ${prevStage}`,
				`Received payload: ${payload}`
			);
		}}
		beforeTransition={(currentStage, nextStage, payload) => {
			console.log(
				`Try to make a transition to the stage ${nextStage}`,
				`From stage ${prevStage}`,
				`With payload: ${payload}`
			);

			return !!payload.hasCats;
		}}
		onFinish={(transitionsHistory) => {
			console.log(`Finish. History: ${transitionsHistory}`);
		}}
		commonProps={commonProps}
		Layout={Layout}
	/>
);

ReactDOM.render(
  <Form />,
  document.getElementById('example')
);