AniUI Academy

Build a multi-step form wizard

mediumState & Data+220 XP

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.
Asked atStripeShopifyAtlassianLinkedIn

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>
  );
}

5 tests · +220 XP

Discussion

Sign in to post
Sign in to join the conversation.Sign in

No comments yet. Be the first to say something.