Getting started
Introduction
Define your entire multi-step form as a single, programmable flow.
What is Formity?
Formity is a React library for building multi-step forms as programmable flows that can branch, loop, jump, and track variables across steps, giving you full control over the form's behavior.
Fully type-safe and compatible with React Hook Form, Formik, TanStack Form, and any other form library. It works for onboarding flows, lead capture forms, job applications, and more.
Formity only controls the flow between steps. Each step is a React component of your own, so fields, validation, and styling stay in your hands.
Installation
Install version 3 of the package, which works with React 16 or later.
npm install @formity/react
How it works
The core of Formity is the flow, an array of elements that each define a piece of the multi-step form's behavior. By combining them, you can build any multi-step form you can imagine.
The example below asks whether the user is a software developer and, only if they are, asks for their favorite programming language. At the end, it returns both answers.
// src/app.tsx
import { useCallback } from "react";
import { Formity, type s, type Flow, type OnReturn } from "@formity/react";
import { SoftwareDeveloperForm } from "./components/software-developer-form";
import { LanguageForm } from "./components/language-form";
type Schema = {
render: React.ReactNode;
struct: [
s.Form<{ softwareDeveloper: string }>,
s.Variables<{ language: string | null }>,
s.Condition<{
then: [s.Form<{ language: string }>];
else: [];
}>,
s.Return<{ softwareDeveloper: string; language: string | null }>,
];
inputs: Record<never, never>;
params: Record<never, never>;
};
const flow: Flow<Schema> = [
{
form: {
fields: () => ({ softwareDeveloper: ["yes", []] }),
render: ({ fields, next }) => (
<SoftwareDeveloperForm fields={fields} next={next} />
),
},
},
{
variables: () => ({ language: null }),
},
{
condition: {
if: ({ softwareDeveloper }) => softwareDeveloper === "yes",
then: [
{
form: {
fields: () => ({ language: ["", []] }),
render: ({ fields, back, next }) => (
<LanguageForm fields={fields} back={back} next={next} />
),
},
},
],
else: [],
},
},
{
return: ({ softwareDeveloper, language }) => ({
softwareDeveloper,
language,
}),
},
];
export default function App() {
const onReturn = useCallback<OnReturn<Schema>>((output) => {
// {
// softwareDeveloper: string;
// language: string | null;
// }
console.log(output);
}, []);
return <Formity<Schema> flow={flow} onReturn={onReturn} />;
}
These are the pieces involved:
- Flow: Defines the structure and behavior of the multi-step form. Its elements run in order, and the flow pauses at each
formelement until the user submits that step. - Flow values: An object made available to each element as the flow progresses. It contains the values from the form and variables elements encountered so far.
- Schema: The type of the flow. Its
structproperty lists the same elements as the flow, which keeps the flow values and the returned values type-safe. - Formity: The component that renders the multi-step form from the flow. When the steps need to render something other than a
ReactNode, use theuseFormityhook instead. - onReturn: A callback function that is called when the form is completed. It receives the values returned by the
returnelement, which is where you would submit them.
Form steps
In a form element, the fields function gives each field a default value and an array. Values the user already entered are kept unless the array changes.
The render function renders the step with a component that you write. The component receives the default values in fields and moves through the flow by calling next or back with its values.
// src/components/language-form.tsx
import { useState } from "react";
import type { Back, Next } from "@formity/react";
type Values = { language: string };
type LanguageFormProps = {
fields: Values;
back: Back<Values>;
next: Next<Values>;
};
export function LanguageForm({ fields, back, next }: LanguageFormProps) {
const [language, setLanguage] = useState(fields.language);
return (
<form
onSubmit={(event) => {
event.preventDefault();
next({ language });
}}
>
<input
value={language}
onChange={(event) => setLanguage(event.target.value)}
/>
<button type="button" onClick={() => back({ language })}>
Back
</button>
<button type="submit">Next</button>
</form>
);
}
This example uses React state to stay simple. In practice a form library takes care of the state and the validation of each step. See how it is done with React Hook Form, Formik, and TanStack Form.
Flow elements
Each element is an object with a single key that tells which element it is.
form: Shows a step and adds its values to the flow values.return: Ends the form and passes values toonReturn.variables: Adds computed values to the flow values.condition: Branches intothenorelsedepending onif.switch: Branches into the firstcasethat is true, or intodefault.jump: Marks a form that can be jumped to from anywhere in the flow.loop: Repeats the elements indowhilewhileis true.yield: Passes values toonYieldwhen the user moves between steps.module: Includes a nested flow that can be defined in another file.
Next steps
Follow the Tutorial to build a multi-step form with conditional logic from scratch.