Frontend School

Build a Multi-Step Form Wizard (Stepper)

Machine Coding
medium

Build a Multi-Step Form Wizard (Stepper)

Medium
React
Frontend
UI
State Management
Accessibility
Forms

Overview

Modern web applications often require users to input complex information, which can be overwhelming if presented as a single, long form. A multi-step form, commonly known as a "Stepper" or "Wizard," breaks down this process into manageable, sequential steps, significantly improving user experience and reducing cognitive load. For instance, an online checkout process or a new user onboarding flow are prime examples where a stepper wizard is invaluable.

What you need to do / Task

Your task is to build a Stepper React component that orchestrates a multi-step form. This component should manage its own state for navigation and validation, rendering different content for each step. You will be provided with a configuration array defining the steps.

  1. Implement the Stepper Component: Create a Stepper component that takes an array of step configurations as a prop. This component should manage the active step's index internally.
  2. Render Step Navigation: Display a visual representation of all steps (e.g., numbered titles) at the top, clearly indicating the active step and any completed steps.
  3. Render Current Step Content: Dynamically render the content associated with the currently active step. This content will include input fields for user interaction.
  4. Implement Navigation Controls: Add "Next" and "Previous" buttons for navigating between steps.
  5. Validation Logic: Implement a mechanism for each step's content to report its validity status back to the Stepper component. The "Next" button should be disabled if the current step's content is invalid.
  6. Handle Boundaries: Disable the "Previous" button on the first step and change the "Next" button's text to "Submit" on the final step.
  7. Form Data Management: The Stepper should collect and manage the form data from all steps. On "Submit," log the collected form data to the console.

UI/UX expectations

  • Layout: The stepper navigation (step titles) should appear above the current step's content, followed by the navigation buttons.
  • Visual Cues: The active step should be visually distinct (e.g., different background/text color). Completed steps (steps before the active one) should also have a distinct visual style.
  • Interactions:
    • Clicking "Next" advances to the next step if the current step is valid.
    • Clicking "Previous" moves to the prior step.
    • Buttons should be appropriately disabled based on navigation boundaries (first/last step) and validation status.
  • Accessibility: Use semantic HTML (e.g., role="tablist", role="tab", aria-controls, aria-selected, aria-current) for the step navigation to ensure keyboard accessibility and screen reader compatibility. Ensure inputs have associated labels.

Acceptance criteria

  • The Stepper component renders all defined steps in a navigation header.
  • Only the content of the currently active step is displayed.
  • Clicking "Next" advances the step index, displaying the subsequent step's content.
  • Clicking "Previous" decrements the step index, displaying the prior step's content.
  • The "Previous" button is disabled when on the first step.
  • The "Next" button's text changes to "Submit" when on the last step.
  • The "Next" button is disabled if the currently displayed step's content is incomplete or invalid (e.g., an input field is empty, or a required radio option is not selected).
  • The active step in the navigation header is clearly highlighted.
  • Completed steps in the navigation header are visually distinguishable from unvisited steps.
  • The solution incorporates basic accessibility attributes for navigation and form elements.

Constraints

Constraints & assumptions

  • You should use React with TypeScript.
  • No external UI component libraries (e.g., Material UI, Ant Design) are allowed; build components from scratch.
  • Focus on functional components and React Hooks for state management.
  • The provided stepsConfig array defines the structure of the steps.
  • Simple Validation: For form inputs, a basic validation check (e.g., checking if a text input is non-empty, or if a radio button group has a selection) is sufficient.
  • The problem focuses on the client-side UI and state management; no backend integration is required.

Out of scope

  • Complex form validation schemas or external validation libraries (e.g., Yup, Zod).
  • Advanced styling, animations, or transitions beyond basic visual cues.
  • Persisting form data across page reloads or to a server.
  • Deep linking or URL routing for steps.

What interviewers evaluate

  • Component Design: How well the Stepper component is structured, its reusability, and separation of concerns.
  • State Management: Effective use of React state and hooks to manage stepper navigation, form data, and validation status.
  • UI/UX and Accessibility: Correct implementation of visual cues, interactive elements, and adherence to basic accessibility standards.
  • Code Quality: Readability, maintainability, type safety, and efficient use of React patterns.

Loading editor...