Multi-Step Form Wizard
Break long forms into manageable steps
Overview
The multi-step form wizard allows you to break long forms into manageable steps, improving the user experience by only displaying one step at a time. It's powered by a lightweight Stimulus.js controller and integrates seamlessly with simple_form
and Tailwind CSS in Lightning Rails.
Installation & Setup
Follow these steps to implement a form wizard in your Lightning Rails project.
1. Create the Form View
Wrap your form in a <div data-controller="wizard">
and split it into multiple steps using data-wizard-target="step"
on each section.
2. Create the Stimulus Controller
Add this Stimulus controller in app/javascript/controllers/wizard_controller.js
:
Make sure the controllers are registered in application.js
:
Usage
Each step is wrapped in a
div
withdata-wizard-target="step"
.Use
class="hidden"
(Tailwind utility) to hide inactive steps.Navigation is handled via
button_tag
withdata-action
for Stimulus event bindings.next_step
andprevious_step
indicate the index of the step to show/hide.
Best Practices
Keep each step concise and focused.
Validate inputs before allowing the user to proceed (add validations or checks in the controller if needed).
Use consistent styling with Tailwind to provide visual cues for step transitions.
Troubleshooting
Issue: Nothing happens when clicking "Next" or "Previous". Solution:
Ensure the Stimulus controller is properly registered and compiled.
Verify that
data-controller="wizard"
anddata-wizard-target="step"
are correctly placed.Make sure the buttons have the correct
data-action
,next_step
, orprevious_step
.
Issue: Steps aren't hiding/showing properly. Solution:
Confirm that
class="hidden"
is being toggled correctly.Check that
stepTargets
correspond to the order of steps in your HTML.
Let me know on Slack if you'd like an enhanced version with progress indicators or form validation!
Last updated
Was this helpful?