Skip to content
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

[Feature Request]: Form Groups #419

Open
crutchcorn opened this issue Aug 30, 2023 · 4 comments
Open

[Feature Request]: Form Groups #419

crutchcorn opened this issue Aug 30, 2023 · 4 comments

Comments

@crutchcorn
Copy link
Member

Description

When building a form stepper, like so:

image

It's common for each step to have its own form. However, this complicates the form submission and validation process by requiring you to add complex logic.

Ideally, it would be nice to have a FormGroup where you could validate the group, but not the form itself - submit the value and move on to the next step.

Describe the solution you'd like

What if there was something like this psuedo-API?

import { useState } from "react";
import { useForm } from "@tanstack/react-form";
// Import might change
import { za } from "@tanstack/zod-form-adapter";

const StepperForm = () => {
  const [step, setStep] = useState(0);
  const form = useForm({ onSubmit: () => {} });

  return (
    <form.Provider>
      <form {...form.getFormProps()}>
        <div>
          {step === 1 && (
            <form.FormGroup
              onGroupSubmit={() => {
                setStep(step + 1);
              }}
              preserveState={true}
            >
              <form.Field
                name="step1.name"
                onSubmit={za(z.string().thing())}
                onGroupSubmit={za(z.string().thing())}
              >
                <Input />
              </form.Field>
            </form.FormGroup>
          )}

          {step === 2 && (
            <form.FormGroup onGroupSubmit={submit} preserveState={true}>
              <form.Field
                name="step2.name"
                onSubmit={za(z.string().thing())}
                onGroupSubmit={za(z.string().thing())}
              >
                <Input />
              </form.Field>
            </form.FormGroup>
          )}
        </div>
      </form>
    </form.Provider>
  );
};
@Christian24
Copy link
Contributor

Just wanted to leave some additional context: We sometimes have quite complex forms that have a lot of fields that are only visible on a certain condition, say for example a billing and a delivery address, they are basically the same, but one is only used under the condition that the two are not the same. So FormGroups can be a great way to structure a form as well.

@timothyac
Copy link

+1

At AWS, a common pattern we implement is a “Wizard” pattern which guides a user through a multi-step form.

Example: https://cloudscape.design/examples/react/wizard.html

@francesconi
Copy link

I'm currently working on a multi-step form, similar to a wizard. The proposed form.FormGroup component would simplify the code a lot.

@crutchcorn
Copy link
Member Author

@timothyac we're not quite ready for this feature yet as part of our 1.x release, but we'll work on it soon after!

That said, I'd love to help the AWS team figure out how to integrate Cloudscape with TanStack Form when it's ready. If y'all need any help with anything, let me know (even via DMs - they're open)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants