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

feat: reusable multistep form #181

Merged
merged 3 commits into from
Mar 12, 2024

Conversation

Aashish-Upadhyay-101
Copy link
Contributor

@Aashish-Upadhyay-101 Aashish-Upadhyay-101 commented Mar 9, 2024

This is an example of how to use this component.
@dahal, ready for review !

fixes : #179

const schema1 = z.object({
  name: z.string().min(1),
  email: z.string().email(),
  address: z.string().min(1),
});

type formField1 = z.infer<typeof schema1>;

const ExampleModal = () => {
  const [open, setOpen] = useState(false);      

  const steps: stepsType[] = [
    {
      id: 1,
      title: "step 1",
      component: form1, // a react FC component for each step, please do not write a <form>...</form>, just begin with <div> or others.
      fields: ["name"],
    },
    {
      id: 2,
      title: "step 2",
      component: form2,
      fields: ["email"],
    },
    {
      id: 3,
      title: "step 3",
      component: form3,
      fields: ["address"],
    },
  ];

  const onSubmit: SubmitHandler<formField1> = (data) => {
    // submit handler
  };

  return (
    <MultiStepFormModal
      steps={steps}
      title="New Modal"
      subtitle="Create new modal..."
      trigger={<Button>Open Modal</Button>}
      dialogProps={{
        open,
        onOpenChange: (val) => {
          setOpen(val);
        },
      }}
      schema={schema1}
      onSubmit={onSubmit}
    />
  );
};

export default ExampleModal;

const form1 = () => {
  const {
    register,
    formState: { errors },
  } = useFormContext<formField1>();
  return (
    <div>
      <Input
        type="text"
        placeholder="name"
        {...register("name", {
          required: { value: true, message: "Name is required" },
        })}
        id="name"
      />
      {errors.name && <p className="text-red-500">{errors.name?.message}</p>}
    </div>
  );
};

const form2 = () => {
  const {
    register,
    formState: { errors },
  } = useFormContext<formField1>();
  return (
    <div>
      <Input
        type="email"
        placeholder="email"
        {...register("email", {
          required: { value: true, message: "Email is required" },
        })}
        id="email"
      />
      {errors.email && <p className="text-red-500">{errors.email?.message}</p>}
    </div>
  );
};

const form3 = () => {
  const {
    register,
    formState: { errors },
  } = useFormContext<formField1>();
  return (
    <div>
      <Input
        type="text"
        placeholder="address"
        {...register("address", {
          required: { value: true, message: "Address is required" },
        })}
        id="address"
      />
      {errors.address && (
        <p className="text-red-500">{errors.address?.message}</p>
      )}
    </div>
  );
};

Copy link

github-actions bot commented Mar 9, 2024

Thank you for following the naming conventions for pull request titles! 🙏

Copy link
Contributor

@dahal dahal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks promising, let's go with it. Good job @Aashish-Upadhyay-101

@dahal dahal merged commit 20a7fff into captableinc:main Mar 12, 2024
2 checks passed
@Aashish-Upadhyay-101 Aashish-Upadhyay-101 deleted the multistep-form branch March 20, 2024 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create a re-usable modal with multi-step form
2 participants