Flow elements
Form
Learn how the form element defines a step, its default values, and how it is rendered.
Usage
The form element defines a step in the multi-step form. The flow pauses at it until the user submits the step, and the submitted values are added to the flow values.
// src/app.tsx
import { useCallback } from "react";
import { Formity, type s, type Flow, type OnReturn } from "@formity/react";
import { AboutMeForm } from "./components/about-me-form";
type Schema = {
render: React.ReactNode;
struct: [
s.Form<{ name: string; surname: string; age: number }>,
s.Return<{ name: string; surname: string; age: number }>,
];
inputs: Record<never, never>;
params: Record<never, never>;
};
const flow: Flow<Schema> = [
{
form: {
fields: () => ({
name: ["", []],
surname: ["", []],
age: [20, []],
}),
render: ({ fields, next }) => <AboutMeForm fields={fields} next={next} />,
},
},
{
return: ({ name, surname, age }) => ({ name, surname, age }),
},
];
export default function App() {
const onReturn = useCallback<OnReturn<Schema>>((output) => {
// {
// name: string;
// surname: string;
// age: number;
// }
console.log(output);
}, []);
return <Formity<Schema> flow={flow} onReturn={onReturn} />;
}
The step is rendered by the AboutMeForm component, which receives the default values in fields and calls next with its values when the form is submitted.
// src/components/about-me-form.tsx
import { useState } from "react";
import type { Next } from "@formity/react";
type Values = { name: string; surname: string; age: number };
type AboutMeFormProps = {
fields: Values;
next: Next<Values>;
};
export function AboutMeForm({ fields, next }: AboutMeFormProps) {
const [name, setName] = useState(fields.name);
const [surname, setSurname] = useState(fields.surname);
const [age, setAge] = useState(fields.age);
return (
<form
onSubmit={(event) => {
event.preventDefault();
next({ name, surname, age });
}}
>
<input value={name} onChange={(event) => setName(event.target.value)} />
<input
value={surname}
onChange={(event) => setSurname(event.target.value)}
/>
<input
type="number"
value={age}
onChange={(event) => setAge(Number(event.target.value))}
/>
<button type="submit">Next</button>
</form>
);
}
In the Schema, each form element has an s.Form entry in struct. Its type must match the fields returned by fields and the values passed to next.
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.
Fields
The fields function receives the flow values, which are the inputs and the values collected so far from previous form and variables elements in the flow.
It returns a default value and an array for each field. When the user returns to the step, each entered value is kept unless its array has changed. Leave the arrays empty unless the form is in a loop.
fields: () => ({
name: ["", []],
surname: ["", []],
age: [20, []],
}),
Render
The render function renders the form step. It receives an object with the following properties:
fields: The default values of the form.values: The flow values.params: The values passed with theparamsprop.next(values): Receives the form values and goes to the next step.back(values): Receives the form values and goes to the previous step.jump(id, values): Receives a jump element'sidand the form values, and goes to it.getState(values): Receives the form values and returns the state of the multi-step form.setState(state): Receives a state and replaces the state of the multi-step form with it.
render: ({ fields, next }) => (
<AboutMeForm fields={fields} next={next} />
),
To pass these functions as props, type them with Next, Back, Jump, GetState, and SetState.
import type { Next, Back, Jump } from "@formity/react";
type Values = { name: string; surname: string; age: number };
type AboutMeFormProps = {
fields: Values;
next: Next<Values>;
back: Back<Values>;
jump: Jump<Values>;
};