Flow elements
Jump
Learn how the jump element defines a form that can be jumped to from anywhere in the flow.
Usage
The jump element defines a form that can be jumped to from anywhere in the flow.
TSX
// src/app.tsx
import { useCallback } from "react";
import { Formity, type s, type Flow, type OnReturn } from "@formity/react";
import { TopicsForm } from "./components/topics-form";
import { FrequencyForm } from "./components/frequency-form";
import { EmailForm } from "./components/email-form";
type Schema = {
render: React.ReactNode;
struct: [
s.Form<{ topics: string }>,
s.Form<{ frequency: string }>,
s.Jump<s.Form<{ email: string }>>,
s.Return<{
topics: string;
frequency: string;
email: string;
}>,
];
inputs: {
topics: string;
frequency: string;
};
params: Record<never, never>;
};
const flow: Flow<Schema> = [
{
form: {
fields: () => ({ topics: ["all", []] }),
render: ({ fields, next, jump }) => (
<TopicsForm fields={fields} next={next} jump={jump} />
),
},
},
{
form: {
fields: () => ({ frequency: ["weekly", []] }),
render: ({ fields, back, next }) => (
<FrequencyForm fields={fields} back={back} next={next} />
),
},
},
{
jump: {
id: "email",
at: {
form: {
fields: () => ({ email: ["", []] }),
render: ({ fields, back, next }) => (
<EmailForm fields={fields} back={back} next={next} />
),
},
},
},
},
{
return: ({ topics, frequency, email }) => ({
topics,
frequency,
email,
}),
},
];
export default function App() {
const onReturn = useCallback<OnReturn<Schema>>((output) => {
// {
// topics: string;
// frequency: string;
// email: string;
// }
console.log(output);
}, []);
return (
<Formity<Schema>
flow={flow}
inputs={{ topics: "all", frequency: "weekly" }}
onReturn={onReturn}
/>
);
}
The id identifies the jump element and at contains the form to jump to. The form is still a regular step, so next reaches it too.
In the Schema, each jump element has an s.Jump entry in struct, which wraps the s.Form entry of the form defined in at.
Jumping to a form
The render function of every form receives a jump function. It receives the id of a jump element and the form values, and goes to the form of that jump element.
TSX
// src/components/topics-form.tsx
import { useState } from "react";
import type { Next, Jump } from "@formity/react";
type Values = { topics: string };
type TopicsFormProps = {
fields: Values;
next: Next<Values>;
jump: Jump<Values>;
};
export function TopicsForm({ fields, next, jump }: TopicsFormProps) {
const [topics, setTopics] = useState(fields.topics);
return (
<form
onSubmit={(event) => {
event.preventDefault();
next({ topics });
}}
>
<select
value={topics}
onChange={(event) => setTopics(event.target.value)}
>
<option value="all">All topics</option>
<option value="product">Product updates</option>
<option value="tutorials">Tutorials</option>
</select>
<button type="submit">Next</button>
<button type="button" onClick={() => jump("email", { topics })}>
Skip to email
</button>
</form>
);
}
There are a few things to keep in mind:
- The jump element can be anywhere in the flow, before or after the current step.
- If no jump element has that
id, nothing happens and the current step stays. - Calling
backfrom the form that was jumped to returns to the step the jump was made from, unlesshistoryisfalse. - A jump element can be reached from anywhere, so previous elements may not have been visited and their values may be missing. Provide default values with
inputs, which always exist.