-
Notifications
You must be signed in to change notification settings - Fork 6
feat(apollo-vertex): headless escape hatches for the form wizard [AGVSOL-4703] #950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
frankkluijtmans
wants to merge
2
commits into
main
Choose a base branch
from
frankkluijtmans/headless-wizard-form
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
281 changes: 281 additions & 0 deletions
281
apps/apollo-vertex/app/components/form-wizard/form-wizard-headless-demo.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,281 @@ | ||
| "use client"; | ||
|
|
||
| import { formOptions } from "@tanstack/react-form"; | ||
| import { useState } from "react"; | ||
| import { z } from "zod"; | ||
| import { FieldGroup } from "@/components/ui/field"; | ||
| import { withForm } from "@/components/ui/form"; | ||
| import { | ||
| FormWizard, | ||
| FormWizardNav, | ||
| FormWizardStep, | ||
| FormWizardSteps, | ||
| useFormWizard, | ||
| useFormWizardContext, | ||
| type WizardStepDef, | ||
| } from "@/components/ui/form-wizard"; | ||
| import { cn } from "@/lib/utils"; | ||
| import { LocaleProvider } from "@/registry/shell/shell-locale-provider"; | ||
|
|
||
| const accountSchema = z.object({ | ||
| fullName: z.string().min(2, "Enter your name."), | ||
| email: z.email("Enter a valid email."), | ||
| }); | ||
| const planSchema = z.object({ | ||
| tier: z.string().min(1), | ||
| }); | ||
| const reviewSchema = z.object({ | ||
| acceptTerms: z.boolean().refine((value) => value, { | ||
| message: "Accept the terms to continue.", | ||
| }), | ||
| }); | ||
|
|
||
| const wizardSchema = z.object({ | ||
| account: accountSchema, | ||
| plan: planSchema, | ||
| review: reviewSchema, | ||
| }); | ||
|
|
||
| type WizardValues = z.infer<typeof wizardSchema>; | ||
|
|
||
| const defaultValues = { | ||
| account: { fullName: "", email: "" }, | ||
| plan: { tier: "free" }, | ||
| review: { acceptTerms: false }, | ||
| }; | ||
|
|
||
| const wizardOpts = formOptions({ defaultValues }); | ||
|
|
||
| const tierOptions = [ | ||
| { label: "Free", value: "free" }, | ||
| { label: "Pro", value: "pro" }, | ||
| ]; | ||
|
|
||
| const steps: WizardStepDef<WizardValues>[] = [ | ||
| { id: "account", title: "Account" }, | ||
| { id: "plan", title: "Plan" }, | ||
| { id: "review", title: "Review" }, | ||
| ]; | ||
|
|
||
| // A custom nav rendered entirely by the consumer via the render-prop escape hatch. | ||
| function CustomNav() { | ||
| return ( | ||
| <FormWizardNav> | ||
| {({ back, isFirst, isLast, form }) => ( | ||
| <div className="mt-2 flex items-center justify-between"> | ||
| <button | ||
| type="button" | ||
| onClick={back} | ||
| disabled={isFirst} | ||
| className="text-sm font-medium text-muted-foreground underline-offset-4 hover:underline disabled:opacity-40" | ||
| > | ||
| Go back | ||
| </button> | ||
| <form.Subscribe selector={(state) => state.isSubmitting}> | ||
| {(isSubmitting) => ( | ||
| <button | ||
| type="submit" | ||
| disabled={isSubmitting} | ||
| className="rounded-full bg-foreground px-5 py-2 text-sm font-semibold text-background disabled:opacity-50" | ||
| > | ||
| {isLast ? "Create account" : "Continue"} | ||
| </button> | ||
| )} | ||
| </form.Subscribe> | ||
| </div> | ||
| )} | ||
| </FormWizardNav> | ||
| ); | ||
| } | ||
|
|
||
| const AccountStep = withForm({ | ||
| ...wizardOpts, | ||
| render: function Render({ form }) { | ||
| const { next } = useFormWizardContext(); | ||
| return ( | ||
| <form.FormGroup | ||
| name="account" | ||
| validators={{ onDynamic: accountSchema }} | ||
| onGroupSubmit={() => next()} | ||
| > | ||
| {(group) => ( | ||
| <form | ||
| onSubmit={(event) => { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| void group.handleSubmit(); | ||
| }} | ||
| > | ||
| <FieldGroup> | ||
| <form.AppField name="account.fullName"> | ||
| {(field) => ( | ||
| <field.TextField | ||
| label="Full name" | ||
| placeholder="Ada Lovelace" | ||
| /> | ||
| )} | ||
| </form.AppField> | ||
| <form.AppField name="account.email"> | ||
| {(field) => ( | ||
| <field.TextField | ||
| type="email" | ||
| label="Email" | ||
| placeholder="ada@example.com" | ||
| /> | ||
| )} | ||
| </form.AppField> | ||
| <CustomNav /> | ||
| </FieldGroup> | ||
| </form> | ||
| )} | ||
| </form.FormGroup> | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| const PlanStep = withForm({ | ||
| ...wizardOpts, | ||
| render: function Render({ form }) { | ||
| const { next } = useFormWizardContext(); | ||
| return ( | ||
| <form.FormGroup | ||
| name="plan" | ||
| validators={{ onDynamic: planSchema }} | ||
| onGroupSubmit={() => next()} | ||
| > | ||
| {(group) => ( | ||
| <form | ||
| onSubmit={(event) => { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| void group.handleSubmit(); | ||
| }} | ||
| > | ||
| <FieldGroup> | ||
| <form.AppField name="plan.tier"> | ||
| {(field) => ( | ||
| <field.RadioGroupField label="Plan" options={tierOptions} /> | ||
| )} | ||
| </form.AppField> | ||
| <CustomNav /> | ||
| </FieldGroup> | ||
| </form> | ||
| )} | ||
| </form.FormGroup> | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| const ReviewStep = withForm({ | ||
| ...wizardOpts, | ||
| render: function Render({ form }) { | ||
| const { next } = useFormWizardContext(); | ||
| return ( | ||
| <form.FormGroup | ||
| name="review" | ||
| validators={{ onDynamic: reviewSchema }} | ||
| onGroupSubmit={() => next()} | ||
| > | ||
| {(group) => ( | ||
| <form | ||
| onSubmit={(event) => { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| void group.handleSubmit(); | ||
| }} | ||
| > | ||
| <FieldGroup> | ||
| <form.AppField name="review.acceptTerms"> | ||
| {(field) => ( | ||
| <field.CheckboxField label="I accept the terms and conditions" /> | ||
| )} | ||
| </form.AppField> | ||
| <CustomNav /> | ||
| </FieldGroup> | ||
| </form> | ||
| )} | ||
| </form.FormGroup> | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| export function FormWizardHeadlessDemo() { | ||
| const [submitted, setSubmitted] = useState<WizardValues | null>(null); | ||
|
|
||
| const wizard = useFormWizard<WizardValues>({ | ||
| formOptions: wizardOpts, | ||
| schema: wizardSchema, | ||
| steps, | ||
| onSubmit: (values) => setSubmitted(values), | ||
| }); | ||
|
|
||
| return ( | ||
| <LocaleProvider> | ||
| <FormWizard wizard={wizard}> | ||
| {/* Custom progress rail built from the FormWizardSteps render prop. */} | ||
| <FormWizardSteps> | ||
| {({ steps: visibleSteps, stepIndex, goToStep }) => ( | ||
| <div className="flex flex-col gap-2"> | ||
| <div className="flex items-center gap-2"> | ||
| {visibleSteps.map((step, index) => { | ||
| const done = index < stepIndex; | ||
| const active = index === stepIndex; | ||
| return ( | ||
| <button | ||
| key={step.id} | ||
| type="button" | ||
| onClick={() => goToStep(step.id)} | ||
| disabled={!done} | ||
| className={cn( | ||
| "flex items-center gap-2 text-sm transition-colors", | ||
| active && "font-semibold text-foreground", | ||
| done && "text-foreground hover:opacity-80", | ||
| !active && !done && "text-muted-foreground", | ||
| )} | ||
| > | ||
| <span | ||
| className={cn( | ||
| "flex size-5 items-center justify-center rounded-full border text-xs", | ||
| active && | ||
| "border-foreground bg-foreground text-background", | ||
| done && | ||
| "border-foreground bg-foreground text-background", | ||
| !active && !done && "border-muted-foreground/40", | ||
| )} | ||
| > | ||
| {done ? "✓" : index + 1} | ||
| </span> | ||
| {step.title} | ||
| </button> | ||
| ); | ||
| })} | ||
| </div> | ||
| <div className="h-1 w-full overflow-hidden rounded-full bg-muted"> | ||
| <div | ||
| className="h-full rounded-full bg-foreground transition-all" | ||
| style={{ width: `${wizard.progress}%` }} | ||
| /> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </FormWizardSteps> | ||
|
|
||
| <FormWizardStep stepId="account"> | ||
| <AccountStep form={wizard.form} /> | ||
| </FormWizardStep> | ||
| <FormWizardStep stepId="plan"> | ||
| <PlanStep form={wizard.form} /> | ||
| </FormWizardStep> | ||
| <FormWizardStep stepId="review"> | ||
| <ReviewStep form={wizard.form} /> | ||
| </FormWizardStep> | ||
| </FormWizard> | ||
|
|
||
| {submitted ? ( | ||
| <pre className="mt-6 overflow-x-auto rounded-lg bg-muted p-4 text-sm text-muted-foreground"> | ||
| {JSON.stringify(submitted, null, 2)} | ||
| </pre> | ||
| ) : null} | ||
| </LocaleProvider> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.