Sub topic 2
Splitting input into steps
This chapter assumes you can write basic TypeScript, know how forms and form fields work, and understand application state at a simple level. You will learn how to decide where one step ends and the next begins. The code in later chapters builds on the Step type used here.
Why the split matters
A guided flow collects data one step at a time. The split you choose decides how much the user must think about at once, where validation runs, and how many branches you must maintain. A bad split is hard to fix later, because state, navigation, and tests all depend on it.
Good steps share three properties:
- One purpose. The user can state in a few words what the step asks for, such as "Tell us about your account."
- One validation boundary. Everything on the step can be checked together, and the user can fix any error without leaving the step.
- One decision point. The answer that chooses the next step is collected on the step that decides it.
Group inputs by meaning
Start from the full list of fields and group them by what they describe, not by how they look on screen. Email and password both belong to the account. Company name and tax ID both belong to the business.
Then check each group against these questions:
- Does the user already know all these answers, or must they look something up? Fields that need lookup, such as a tax ID, are better on their own step so the user can pause there.
- Do some fields depend on an earlier answer? Put them on a later step, so you can skip that step when it does not apply.
- Is the group small enough to finish without scrolling on a phone? If not, split it.
Order the steps
Place easy, low-risk questions first. A user who has answered two simple questions is more likely to finish. Put sensitive or effortful questions after the user has some momentum. Put any question that decides a branch before the steps it affects.
Always end with a review step. Review has no inputs of its own. It shows what was collected and offers submission. A later chapter covers it in detail.
Describe each step as data
Instead of writing one long form with hidden sections, describe each step as a small object. This is the first code example from the plan. Each step declares its own validation and the id of the step that follows.
type Data = Record<string, string>;
type Step = {
id: string;
validate(data: Data): string[];
next(data: Data): string | null;
};
const steps: Record<string, Step> = {
account: {
id: "account",
validate: (d) =>
/^\S+@\S+\.\S+$/.test(d.email ?? "") ? [] : ["Enter a valid email address."],
next: (d) => (d.accountType === "business" ? "company" : "review"),
},
company: {
id: "company",
validate: (d) => ((d.company ?? "").trim() ? [] : ["Enter your company name."]),
next: () => "review",
},
review: {
id: "review",
validate: () => [],
next: () => null,
},
};Three things are worth noticing:
validatereturns a list of messages. An empty list means the step is valid.nextreceives all collected data, so a step can branch on any earlier answer.nextreturnsnullon the last step, which signals that the flow has ended.
The branching lives in next, not in scattered if statements across the interface. To add a step, you add an entry and change one next function.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| One step per field | The flow feels long and users lose context | Group related fields |
| One step with every field | The user sees one long form, and errors are hard to locate | Split by meaning |
| Asking a branching question late | Earlier steps may collect data that is never used | Move the deciding question earlier |
| Repeating a question across steps | Answers can conflict | Collect each value once |
Check your split
Before moving on, take your own data-collection task and list its fields. Group them, name each group in a few words, and mark which groups depend on earlier answers. The result is your step list and the data you will put in steps.
Exercise
A flow collects name, email, a business flag, company name, and tax ID. Company name and tax ID apply only to businesses. Propose steps and say which step's next function holds the branch.
Show answer
Use three steps: "account" (name, email, business flag), "company" (company name, tax ID), and "review". The account step holds the branch, because its next returns "company" when the business flag is set and "review" otherwise. The business flag is collected on the account step because it decides the branch, and the company step is skipped for personal accounts.
Exercise
A step asks for a password and, on the same screen, an optional newsletter preference that has no connection to the account. Which of the three properties of a good step does this break, and what would you change?
Show answer
It breaks "one purpose". Move the newsletter preference to a separate step, or to the review step, so the account step only holds account credentials.