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.
- 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.
- 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.
- Render Current Step Content: Dynamically render the content associated with the currently active step. This content will include input fields for user interaction.
- Implement Navigation Controls: Add "Next" and "Previous" buttons for navigating between steps.
- 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.
- Handle Boundaries: Disable the "Previous" button on the first step and change the "Next" button's text to "Submit" on the final step.
- 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.