Abstract onboarding wizard into reusable FormWizard#663
Conversation
Extract the ~400-line OnboardingForm wizard into a reusable form.Wizard and form.WizardStep component system that integrates with TanStack Form's formComponents registry. This enables multi-step wizards for onboarding, club match, club creation, and event creation forms. Key changes: - Create FormWizard/ directory with types, context, and components - Register Wizard and WizardStep as formComponents in form.ts - Fix validation to only block forward navigation (not backward) - Auto-advance to finish step after successful form submission - Rewrite OnboardingForm from ~400 lines to ~260 lines - Delete OnboardingFormStep (content moved inline to WizardStep children) Closes #588
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
@Isoscelestial, Hey Isaac wanted to follow up on this, can you check it when you get the chance? |
Isoscelestial
left a comment
There was a problem hiding this comment.
I did a quick look through, I like how it's looking so far! After you fix these things, I'll do another more in-depth review.
| type WizardStepObjectBase = { | ||
| label: string; | ||
| hidden?: boolean; | ||
| }; | ||
|
|
||
| export type WizardStepObject<Fields> = WizardStepObjectBase & | ||
| ( | ||
| | { | ||
| id: string | number; | ||
| variant?: 'body'; | ||
| fields: (keyof Fields)[]; | ||
| } | ||
| | { | ||
| id?: never; | ||
| variant: 'start' | 'finish'; | ||
| fields?: never; | ||
| } | ||
| ); |
There was a problem hiding this comment.
You replaced these (now unused) props with StepConfig. However, these unused props had type safety for fields and ensuring the start and finish variants don't have fields. Could you try to use WizardStepObject again, to add type safety?
There was a problem hiding this comment.
Never mind, you can remove these unused types now
Co-authored-by: Isoscelestial <isoscelestial@gmail.com>
Moved startStep and finishStep from Wizard props into WizardStep children using startStep and finishStep boolean props, so the API is consistent with how body steps are declared. Restored type safety on StepConfig using a discriminated union so start and finish variants cannot have fields. Fixed the Continue button on the finish screen not working, and made the wizard wait for the API call to finish before advancing to the finish step. Fixed date picker dark mode background from neutral-900 to neutral-800.
Isoscelestial
left a comment
There was a problem hiding this comment.
Looking good, love to see it functional! Sorry that I'm requesting a lot of changes...
| disabled={ | ||
| index - 1 > activeStep.index || isOnFinishStep | ||
| } |
There was a problem hiding this comment.
Can you add an or condition of (!currentFieldsValid && index >= activeStep.index)? This disables the stepper and prevents users from continuing if there is an error
| disabled={ | |
| index - 1 > activeStep.index || isOnFinishStep | |
| } | |
| disabled={ | |
| index - 1 > activeStep.index || | |
| isOnFinishStep || | |
| (!currentFieldsValid && index >= activeStep.index) | |
| } |
There was a problem hiding this comment.
I think it would also be nice to update the condition index - 1 > activeStep.index so that instead of using activeStep.index, it uses another variable called completedSteps that keeps track of what steps a user has completed. In other words, completedSteps is incremented whenever activeStep is incremented, but it never gets decremented (it will reset when the page reloads). That way, when a user goes back, they can immediately jump back forward, but the stepper still prevents users from jumping to the end.
| setActiveStep({ | ||
| index: steps.length - 1, | ||
| previous: activeStep.index, | ||
| }); |
There was a problem hiding this comment.
For reference, this is technically bad practice because it could lead to race conditions (you're using a state in its own setter). You don't have to fix it here since I'm about to suggest a change that will replace it, but this would be the preferred way:
setActiveStep((prev) => ({
index: prev.index - 1,
previous: prev.index,
}));| const shouldAutoAdvance = autoAdvanceOnSubmit ?? hasFinish; | ||
|
|
||
| // Step navigation state | ||
| const [activeStep, setActiveStep] = useState<ActiveStep>({ |
There was a problem hiding this comment.
There's a lot of repeated code in this file since activeStep.previous is always set to the previous value of activeStep.index. Can you add a helper function that sets active step that handles setting activeStep.previous automatically, or abstract this into a usePrevious hook? Make sure to use a functional update for setting the state to avoid race conditions, as I mentioned in my previous comment
There was a problem hiding this comment.
Can we also rename this state to something like actionStepState? Then we can do this:
const [activeStepState, setActiveStepState] = useState<ActiveStep>({ index: 0, previous: undefined});
const activeStep = activeStepState.index
const previousStep = activeStepState.previousThen update all the calls to activeStep.index to activeStep, and activeStep.previous to previousStep
|
|
||
| const hasStart = steps[0]?.variant === 'start'; | ||
| const hasFinish = steps[steps.length - 1]?.variant === 'finish'; | ||
| const shouldAutoAdvance = autoAdvanceOnSubmit ?? hasFinish; |
There was a problem hiding this comment.
Nitpicking, but this variable name is imprecise. It implies that the form auto advances on every step, but it only auto advances on submission. Probably rename to shouldAutoAdvanceOnSubmit (or instead add a comment explaining this)
| * ```tsx | ||
| * <form.Wizard onComplete={() => router.push('/')}> | ||
| * <form.WizardStep startStep> | ||
| * <Intro /> | ||
| * </form.WizardStep> | ||
| * <form.WizardStep label="Name" fields={['firstName', 'lastName']}> | ||
| * ...form fields... | ||
| * </form.WizardStep> | ||
| * <form.WizardStep finishStep> | ||
| * <Done /> | ||
| * </form.WizardStep> | ||
| * </form.Wizard> | ||
| * ``` | ||
| */ |
There was a problem hiding this comment.
Nitpicking again (sorry 😭), but use the @example tag from JSDoc. Also tweaked the example a bit
| * ```tsx | |
| * <form.Wizard onComplete={() => router.push('/')}> | |
| * <form.WizardStep startStep> | |
| * <Intro /> | |
| * </form.WizardStep> | |
| * <form.WizardStep label="Name" fields={['firstName', 'lastName']}> | |
| * ...form fields... | |
| * </form.WizardStep> | |
| * <form.WizardStep finishStep> | |
| * <Done /> | |
| * </form.WizardStep> | |
| * </form.Wizard> | |
| * ``` | |
| */ | |
| * @example | |
| * const form = useAppForm({...}) | |
| * | |
| * <form.Wizard onComplete={() => router.push('/')}> | |
| * <form.WizardStep startStep> | |
| * ... | |
| * </form.WizardStep> | |
| * <form.WizardStep label="Name" fields={['firstName', 'lastName']}> | |
| * ...form fields... | |
| * </form.WizardStep> | |
| * <form.WizardStep finishStep> | |
| * ... | |
| * </form.WizardStep> | |
| * </form.Wizard> | |
| */ |
| @@ -121,280 +76,193 @@ export default function OnboardingForm({ | |||
| validators: { onChange: accountOnboardingSchema }, | |||
There was a problem hiding this comment.
Change this to use the onSubmit validation event instead. This serves as a last-resort for all of the other validations that I'm assuming you implemented from my previous suggestion.
| validators: { onChange: accountOnboardingSchema }, | |
| validators: { onSubmit: accountOnboardingSchema }, |
To explain why I said earlier we can't use the onSubmit validation event for fields is because that'd mean we'd need to use form.validateField(field, 'submit'); And annoyingly, this will call this form-level onSubmit validator that we just added here (there's a logical reason for this, but it's a headache). So it will validate all the fields before we want it to.
To sum up everything, TanStack Form is missing features we need. Fortunately, they just started working on it in TanStack/form#2128. Looking forward to this, but for now we'll use this workaround (we'd probably want to refactor FormWizard again whenever this feature is released)
There was a problem hiding this comment.
Don't make this change until you've made the changes here: #663 (comment). Since you've already committed this particular change, you'll need to go ahead and work on that thread I linked.
| } | ||
| }, [hasFinish, steps.length]); | ||
|
|
||
| const handleNext = (event: MouseEvent<HTMLButtonElement>) => { |
There was a problem hiding this comment.
This shares a lot of duplicated code with the goNext function. Can you call goNext() here somehow?
| goNext: () => void; | ||
| goBack: () => void; | ||
| goToStep: (index: number) => void; | ||
| goToFinish: () => void; |
There was a problem hiding this comment.
Might be useful to return the new activeStep instead of void for goNext, goBack, and goToFinish? Not necessary, just thinking ahead
| }; | ||
|
|
||
| export type WizardContextType = { | ||
| activeStep: ActiveStep; |
There was a problem hiding this comment.
We should split the activeStep object to activeStep: number, previousStep: number | undefined. In conjunction with last one of my earlier suggestions, we could then remove the unused ActionStep type
| type WizardStepObjectBase = { | ||
| label: string; | ||
| hidden?: boolean; | ||
| }; | ||
|
|
||
| export type WizardStepObject<Fields> = WizardStepObjectBase & | ||
| ( | ||
| | { | ||
| id: string | number; | ||
| variant?: 'body'; | ||
| fields: (keyof Fields)[]; | ||
| } | ||
| | { | ||
| id?: never; | ||
| variant: 'start' | 'finish'; | ||
| fields?: never; | ||
| } | ||
| ); |
There was a problem hiding this comment.
Never mind, you can remove these unused types now
|
Adding a note here to copy these changes over to Notebook after merge. |
|
Resolved all FormWizard-related threads since we're taking a different approach: fixing the two real bugs directly in What's fixed (on
Left unresolved: The thread about changing The FormWizard abstraction can be revisited when a second wizard consumer actually exists (club creation, event creation, etc.), at which point the real API surface will be clearer. |
Co-authored-by: Isoscelestial <isoscyoung@gmail.com>
You already made the full abstraction. At this point, we are just fixing bugs and code quality issues. I am unresolving all the threads so that they can be fixed. If you'd like help with some of the threads, I'd be more than happy to tackle some of them!
Not sure where this branch name came from. If your AI tends to hallucinate, I ask that you at the very least double check your code and comments before sending them to be reviewed.
I'd like these changes to be made now, as we've figured out a solution and are just waiting for the code to be written. Related PR: UTDNebula/utd-notebook#183
As we discussed earlier, we are planning to have a "second wizard consumer" by using this abstracted system with the club matching form, the club creation form, and event creation form. |
Summary
form.Wizardandform.WizardStepcomponents that any form can use<form.WizardStep>How it works
The wizard registers as a TanStack Form
formComponent, so consumers use it asform.Wizard/form.WizardStep:Each step declares its
fieldsarray, which the wizard uses for per-step validation. The wizard handles the MUI Stepper, slide transitions, dynamic height, and navigation buttons automatically.Files changed
src/components/form/FormWizard/src/utils/form.tssrc/components/getting-started/OnboardingForm.tsxsrc/components/getting-started/OnboardingFormStep.tsxsrc/components/form/FormWizard.tsxCloses #588