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

whizflow

v0.5.0

Published

WhizFlow is a lightweight, headless and extensible React library for building dynamic multi-step forms or troubleshooting workflows.

Downloads

65

Readme

WhizFlow

npm bundle size npm npm node-current

WhizFlow is a lightweight, headless and extensible React library for building dynamic multi-step forms or troubleshooting workflows.

Features

  • Headless: Gives you full control over the UI and styling.
  • Extensible: Allows custom question types and render implementations.
  • Flexible: Easily build complex workflows with conditional branching.
  • Agnostic: Use Formik or any other form/validation library that you want to handle the input.

Demo

Installation

npm install whizflow

or

yarn add whizflow

Usage

  1. Import WhizFlow and required types:
import WhizFlow from 'whizflow';
import { Step } from 'whizflow/dist/types';
  1. Define your workflow:
const workflow: Step[] = [
  {
    id: 'step1',
    questions: [
      {
        id: 'question1',
        prompt: 'What is your name?',
        inputType: 'text',
      },
    ],
    next: (answers) => 'step2',
  },
  {
    id: 'step2',
    questions: [
      {
        id: 'question2',
        prompt: 'What is your favorite color?',
        inputType: 'text',
      },
    ],
    next: (answers) => 'done',
  },
];
  1. Define your custom question types (optional):
const questionTypes = {
  text: (question, answers, setAnswers) => (
    <div key={question.id}>
      <label htmlFor={question.id}>{question.prompt}</label>
      <input
        id={question.id}
        type="text"
        value={answers[question.id] || ''}
        onChange={(e) =>
          setAnswers({ ...answers, [question.id]: e.target.value })
        }
      />
    </div>
  ),
  // Add more question types and their render functions here
};
  1. Use the WhizFlow component in your application:
const YourComponent = () => {
  return (
    <WhizFlow workflow={workflow} questionTypes={questionTypes}>
      {({ step, answers, setAnswers, handleNext, renderQuestion }) => (
        <div>
          {step.questions.map((question) => renderQuestion(question.id))}
          <button onClick={handleNext}>Next</button>
        </div>
      )}
    </WhizFlow>
  );
};

export default YourComponent;

Types

WhizFlow

WhizFlow is the main component for managing the workflow. It accepts the following props:

Props

  • workflow (required): An array of Step objects defining the workflow.
  • onComplete (optional): A callback function to be called when the workflow reaches the done step.
  • questionTypes (optional): An object with keys representing the question type and values as the corresponding render functions.

Render Props

  • step: The current step object.
  • answers: An object containing the answers for each question in the workflow.
  • loading: A boolean to mark when handleNext is waiting to resolve.
  • setAnswers: A function to update the answers object.
  • handleNext: A function to handle navigation to the next step in the workflow.
  • handlePrev: A function to handle navigation to the previous step.
  • renderQuestion: A function to render the correct question type based on the provided dictionary.

Step

A Step object defines a single step in the workflow and includes the following properties:

  • id: A unique identifier for the step.
  • questions: An array of Question objects.
  • next: A function that determines the next step in the workflow based on the current answers. It should return the next step's ID or done if the workflow is complete.

Question

A Question object defines a single question within a step and includes the following properties:

  • id: A unique identifier for the question.
  • prompt: The question's text.
  • inputType: The question's input type, which corresponds to the key in the questionTypes prop passed to the WhizFlow component.

QuestionTypes

An object with keys representing the question type and values as render functions. The render functions should take the question, answers, and a setAnswers function as arguments and return a React element. You can define custom question types and their implementations in the questionTypes object.

const questionTypes = {
  text: (question, answers, setAnswers) => (
    <div key={question.id}>
      <label htmlFor={question.id}>{question.prompt}</label>
      <input
        id={question.id}
        type="text"
        value={answers[question.id] || ''}
        onChange={(e) =>
          setAnswers({ ...answers, [question.id]: e.target.value })
        }
      />
    </div>
  ),
  // Add more question types and their render functions here
};

API

| Component / Type | Property / Function | Type / Signature | Description | | -- | -- | --------- | -------------- | | WhizFlow | | | Main component for managing the workflow. | | | workflow | Step[] | An array of Step objects defining the workflow. (Required) | | | onComplete | (answers: Answers) => void |A callback function to be called when the workflow reaches the done step. (Optional) | | | questionTypes | { [key: string]: QuestionRenderFunction } | An object with keys representing the question type and values as the corresponding render functions. (Optional) | | | step | Step | The current step object. (Render prop) | | | loading | boolean | Whether handleNext is running asyncly. (Render prop) | | | answers | Record<string, any> | An object containing the answers for each question in the workflow. (Render prop) | | | setAnswers | (updatedAnswers: Record<string, any>) => void | A function to update the answers object. (Render prop) | | | handleNext | (submitterAnswers?: Record<string, any>) => void | A function to handle navigation to the next step in the workflow, allows the submitter to update the answers. (Render prop) | | | handlePrev | () => void | A function to handle navigation to the previous step in the workflow. (Render prop) | | | renderQuestion | (questionId: string) => React.ReactNode | A function to render the correct question type based on the provided dictionary. (Render prop) | | Step | | | An object defining a single step in the workflow. | | | id | string | A unique identifier for the step. | | | questions | Question[] | An array of Question objects. | | | next | string \| { nextStepId: string; updatedAnswers?: Answers } ֿֿֿ\| Promise<string> \| Promise<{ nextStepId: string; updatedAnswers?: Answers }> | A function that determines the next step in the workflow based on the current answers. It should return the next step's ID or 'done' if the workflow is complete. | | | context? | any | An optional context object for the question to pass (to be used later when rendering). | | Question | | | An object defining a single question within a step. | | | id | string | A unique identifier for the question. | | | prompt | string | The question's text. | | | options | Option[] | An optional option array for multi-select type of questions | | | inputType | string | The question's input type, which corresponds to the key in the questionTypes prop passed to the WhizFlow component. | | | context? | any | An optional context object for the question to pass (to be used later when rendering). | | Option | | | Answer option for multi-select type of questions. | | | id | string | A unique identifier for the option. | | | label | string | The option's text. | | | value | string | Value for the option | | QuestionTypes | - | { [key: string]: QuestionRenderFunction } | An object with keys representing the question type and values as the corresponding render functions. (Optional) | | QuestionRenderFunction | - | (question: Question, answers: Record<string, any>, setAnswers: (updatedAnswers: Record<string, any>) => void) => React.ReactNode | The render functions should take the question, answers, and a setAnswers function as arguments and return a React element. You can define custom question types and their implementations in the questionTypes object. |

License

MIT © Itamar Bareket, MobiMatter LTD