0.5.0 • Published 1 year ago

whizflow v0.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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 / TypeProperty / FunctionType / SignatureDescription
WhizFlowMain component for managing the workflow.
workflowStep[]An array of Step objects defining the workflow. (Required)
onComplete(answers: Answers) => voidA 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)
stepStepThe current step object. (Render prop)
loadingbooleanWhether handleNext is running asyncly. (Render prop)
answersRecord<string, any>An object containing the answers for each question in the workflow. (Render prop)
setAnswers(updatedAnswers: Record<string, any>) => voidA function to update the answers object. (Render prop)
handleNext(submitterAnswers?: Record<string, any>) => voidA function to handle navigation to the next step in the workflow, allows the submitter to update the answers. (Render prop)
handlePrev() => voidA function to handle navigation to the previous step in the workflow. (Render prop)
renderQuestion(questionId: string) => React.ReactNodeA function to render the correct question type based on the provided dictionary. (Render prop)
StepAn object defining a single step in the workflow.
idstringA unique identifier for the step.
questionsQuestion[]An array of Question objects.
nextstring \| { 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?anyAn optional context object for the question to pass (to be used later when rendering).
QuestionAn object defining a single question within a step.
idstringA unique identifier for the question.
promptstringThe question's text.
optionsOption[]An optional option array for multi-select type of questions
inputTypestringThe question's input type, which corresponds to the key in the questionTypes prop passed to the WhizFlow component.
context?anyAn optional context object for the question to pass (to be used later when rendering).
OptionAnswer option for multi-select type of questions.
idstringA unique identifier for the option.
labelstringThe option's text.
valuestringValue 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.ReactNodeThe 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

0.5.0

1 year ago

0.4.2

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago