Flow elements
Variables
Learn how the variables element adds values to the flow values.
Usage
The variables element defines variables that 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.Variables<{ fullName: string }>,
s.Return<{ fullName: 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} />,
},
},
{
variables: ({ name, surname }) => ({
fullName: `${name} ${surname}`,
}),
},
{
return: ({ fullName, age }) => ({ fullName, age }),
},
];
export default function App() {
const onReturn = useCallback<OnReturn<Schema>>((output) => {
// {
// fullName: string;
// age: number;
// }
console.log(output);
}, []);
return <Formity<Schema> flow={flow} onReturn={onReturn} />;
}
The variables function receives the flow values, which are the inputs and the values from previous form and variables elements. It returns the variables to add for the elements that follow.
In the Schema, each variables element has an s.Variables entry in struct. Its type must match the object returned by variables.
The variables function runs every time the flow reaches it, so it must be synchronous and free of side effects. If the flow needs fetched data, fetch it beforehand and pass it as inputs.
Replacing values
The variables are merged into the flow values. If a variable has the same name as an existing value, it replaces that value for the elements that follow.
// src/app.tsx
import { useCallback } from "react";
import { Formity, type s, type Flow, type OnReturn } from "@formity/react";
import { EmailForm } from "./components/email-form";
type Schema = {
render: React.ReactNode;
struct: [
s.Form<{ email: string }>,
s.Variables<{ email: string }>,
s.Return<{ email: string }>,
];
inputs: Record<never, never>;
params: Record<never, never>;
};
const flow: Flow<Schema> = [
{
form: {
fields: () => ({ email: ["", []] }),
render: ({ fields, next }) => <EmailForm fields={fields} next={next} />,
},
},
{
variables: ({ email }) => ({ email: email.trim().toLowerCase() }),
},
{
return: ({ email }) => ({ email }),
},
];
export default function App() {
const onReturn = useCallback<OnReturn<Schema>>((output) => {
// {
// email: string;
// }
console.log(output);
}, []);
return <Formity<Schema> flow={flow} onReturn={onReturn} />;
}
Skipped forms
A form inside a condition, switch or loop may be skipped, so its values aren't available outside it. To use them in a return, define them first with a variables element, which the form replaces when reached.
// src/app.tsx
import { useCallback } from "react";
import { Formity, type s, type Flow, type OnReturn } from "@formity/react";
import { WorkingForm } from "./components/working-form";
import { CompanyForm } from "./components/company-form";
type Schema = {
render: React.ReactNode;
struct: [
s.Form<{ working: boolean }>,
s.Variables<{ company: string | null }>,
s.Condition<{
then: [s.Form<{ company: string }>];
else: [];
}>,
s.Return<{ working: boolean; company: string | null }>,
];
inputs: Record<never, never>;
params: Record<never, never>;
};
const flow: Flow<Schema> = [
{
form: {
fields: () => ({ working: [true, []] }),
render: ({ fields, next }) => <WorkingForm fields={fields} next={next} />,
},
},
{
variables: () => ({ company: null }),
},
{
condition: {
if: ({ working }) => working,
then: [
{
form: {
fields: () => ({ company: ["", []] }),
render: ({ fields, back, next }) => (
<CompanyForm fields={fields} back={back} next={next} />
),
},
},
],
else: [],
},
},
{
return: ({ working, company }) => ({ working, company }),
},
];
export default function App() {
const onReturn = useCallback<OnReturn<Schema>>((output) => {
// {
// working: boolean;
// company: string | null;
// }
console.log(output);
}, []);
return <Formity<Schema> flow={flow} onReturn={onReturn} />;
}
The same applies to a variables element inside a condition, switch or loop. Its variables can only be used inside it, so define them before it if later elements need them.
Variables in loops
In a loop, variables hold what changes between iterations. They are defined before the loop, and a variables element at the end of do replaces them with the values for the next iteration.
// src/app.tsx
import { useCallback } from "react";
import { Formity, type s, type Flow, type OnReturn } from "@formity/react";
import { GuestForm } from "./components/guest-form";
type Schema = {
render: React.ReactNode;
struct: [
s.Variables<{ i: number; guests: string[] }>,
s.Loop<
[s.Form<{ guest: string }>, s.Variables<{ i: number; guests: string[] }>]
>,
s.Return<{ guests: string[] }>,
];
inputs: Record<never, never>;
params: Record<never, never>;
};
const flow: Flow<Schema> = [
{
variables: () => ({ i: 0, guests: [] }),
},
{
loop: {
while: ({ i }) => i < 3,
do: [
{
form: {
fields: ({ i }) => ({ guest: ["", [i]] }),
render: ({ fields, values, back, next }) => (
<GuestForm
key={values.i}
fields={fields}
back={back}
next={next}
/>
),
},
},
{
variables: ({ i, guests, guest }) => ({
i: i + 1,
guests: [...guests, guest],
}),
},
],
},
},
{
return: ({ guests }) => ({ guests }),
},
];
export default function App() {
const onReturn = useCallback<OnReturn<Schema>>((output) => {
// {
// guests: string[];
// }
console.log(output);
}, []);
return <Formity<Schema> flow={flow} onReturn={onReturn} />;
}
Return new values instead of mutating the existing ones, as in [...guests, guest], so that navigating back restores the values of the previous iteration.