Build a multi-step form wizard
Build a three-step onboarding wizard: Account (email, password), Profile (name, role), Review (read-back + Submit).
The Next button advances only when the current step is valid. The Back button goes to the previous step without re-validating. State is preserved when navigating back.
Submit on the Review step calls the local handler and shows a confirmation.
Examples
// Step 1 of 3: email + password
// Fill email="[email protected]", password="s3cret!"
// → Next enables; click → Step 2 of 3
// Step 2: name + role
// Click Back → Step 1 of 3, previous values intact.
// Fill name + role → Next → Step 3 of 3
// → review-email / review-name / review-role read back values.
// Submit → <p data-testid="done">Welcome, Priya!</p>Form state survives Back; Next's disabled flag is derived per step, not stored.
function isStepValid(step, form) {
if (step === 1)
return form.email.includes("@") && form.password.length >= 6;
if (step === 2)
return form.name.trim() !== "" && form.role.trim() !== "";
return true;
}
const valid = isStepValid(step, form);
// <button data-testid="next" disabled={!valid}>Next</button>Validity is derived during render — one source of truth, no stale-validation bugs when a field changes.
What it has to do
- An element `<p data-testid="step-indicator">Step N of 3</p>` always reflects the current step (N is 1-indexed).
- Step 1 renders `<input data-testid="email">` and `<input data-testid="password">`. Email must contain an "@" character; password must be ≥ 6 characters. Next is enabled only when both are valid.
- Step 2 renders `<input data-testid="name">` and `<input data-testid="role">`. Both must be non-empty.
- Step 3 renders `<p data-testid="review-email">`, `<p data-testid="review-name">`, `<p data-testid="review-role">` showing the committed values, and a `<button data-testid="submit">Submit</button>`.
- The Next button is `<button data-testid="next">Next</button>` with `disabled={!valid}`.
- The Back button is `<button data-testid="back">Back</button>`, hidden on step 1.
- On submit, show `<p data-testid="done">Welcome, {name}!</p>` and hide the form.
Notes
- Keep all form fields in a single object (`{ email, password, name, role }`) so Back preserves everything without per-step bookkeeping.
- Back should not re-validate — it just moves backward. Only forward motion gates on `valid`.
- In production, replace the inline validators with a schema (Zod, Yup) so the review step can reuse the exact same checks server-side before persisting.
- The regex-free `email.includes("@")` here is intentional — matching the full RFC is a huge yak shave; interviews expect a weak heuristic plus a note that server-side verification is the real check.
Stuck? The react course covers the ideas this problem is built on.
import { useState } from "react"; export default function App() { const [step, setStep] = useState(1); const [form, setForm] = useState({ email: "", password: "", name: "", role: "" }); const [submitted, setSubmitted] = useState(false); // TODO: compute validity per step; wire Next (disabled when invalid), // Back (hidden on step 1), and the review step. if (submitted) { return <p data-testid="done">Welcome, {form.name}!</p>; } return ( <div style={{ fontFamily: "sans-serif", padding: 16, maxWidth: 320 }}> <p data-testid="step-indicator">Step {step} of 3</p> {/* TODO: render each step's inputs */} <div style={{ marginTop: 16 }}> {step > 1 ? <button data-testid="back">Back</button> : null} {step < 3 ? <button data-testid="next">Next</button> : null} {step === 3 ? <button data-testid="submit">Submit</button> : null} </div> </div> ); }
- 1.An element `<p data-testid="step-indicator">Step N of 3</p>` always reflects the current step (N is 1-indexed).
- 2.Step 1 renders `<input data-testid="email">` and `<input data-testid="password">`. Email must contain an "@" character; password must be ≥ 6 characters. Next is enabled only when both are valid.
- 3.Step 2 renders `<input data-testid="name">` and `<input data-testid="role">`. Both must be non-empty.
- 4.Step 3 renders `<p data-testid="review-email">`, `<p data-testid="review-name">`, `<p data-testid="review-role">` showing the committed values, and a `<button data-testid="submit">Submit</button>`.
- 5.The Next button is `<button data-testid="next">Next</button>` with `disabled={!valid}`.
- 6.The Back button is `<button data-testid="back">Back</button>`, hidden on step 1.
- 7.On submit, show `<p data-testid="done">Welcome, {name}!</p>` and hide the form.
5 automated assertions run against your rendered component. The exact assertion names are hidden so you build to the behaviour, not to the test.
Discussion
Sign in to postNo comments yet. Be the first to say something.