Skip to content

Commit

Permalink
Merge pull request #112 from rumeshudash/form-in-onsubmit
Browse files Browse the repository at this point in the history
Refactor AutoForm callbacks to include form object, remove SubmitOptions type
  • Loading branch information
vantezzen authored Oct 16, 2024
2 parents 338f05a + 1424d20 commit 7d1f42b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
31 changes: 20 additions & 11 deletions src/components/ui/auto-form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
"use client";
import { Form } from "@/components/ui/form";
import React from "react";
import { DefaultValues, FormState, useForm } from "react-hook-form";
import {
DefaultValues,
FormState,
useForm,
UseFormReturn,
} from "react-hook-form";
import { z } from "zod";

import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { zodResolver } from "@hookform/resolvers/zod";

import AutoFormObject from "./fields/object";
import { Dependency, FieldConfig, SubmitOptions } from "./types";
import { Dependency, FieldConfig } from "./types";
import {
ZodObjectOrWrapped,
getDefaultValues,
getObjectFormSchema,
ZodObjectOrWrapped,
} from "./utils";

export function AutoFormSubmit({
Expand Down Expand Up @@ -45,11 +50,17 @@ function AutoForm<SchemaType extends ZodObjectOrWrapped>({
}: {
formSchema: SchemaType;
values?: Partial<z.infer<SchemaType>>;
onValuesChange?: (values: Partial<z.infer<SchemaType>>) => void;
onParsedValuesChange?: (values: Partial<z.infer<SchemaType>>) => void;
onValuesChange?: (
values: Partial<z.infer<SchemaType>>,
form: UseFormReturn<z.infer<SchemaType>>
) => void;
onParsedValuesChange?: (
values: Partial<z.infer<SchemaType>>,
form: UseFormReturn<z.infer<SchemaType>>
) => void;
onSubmit?: (
values: z.infer<SchemaType>,
options: SubmitOptions<z.infer<SchemaType>>
form: UseFormReturn<z.infer<SchemaType>>
) => void;
fieldConfig?: FieldConfig<z.infer<SchemaType>>;
children?:
Expand All @@ -71,18 +82,16 @@ function AutoForm<SchemaType extends ZodObjectOrWrapped>({
function onSubmit(values: z.infer<typeof formSchema>) {
const parsedValues = formSchema.safeParse(values);
if (parsedValues.success) {
onSubmitProp?.(parsedValues.data, {
setError: form.setError,
});
onSubmitProp?.(parsedValues.data, form);
}
}

React.useEffect(() => {
const subscription = form.watch((values) => {
onValuesChangeProp?.(values);
onValuesChangeProp?.(values, form);
const parsedValues = formSchema.safeParse(values);
if (parsedValues.success) {
onParsedValuesChange?.(parsedValues.data);
onParsedValuesChange?.(parsedValues.data, form);
}
});

Expand Down
10 changes: 1 addition & 9 deletions src/components/ui/auto-form/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ControllerRenderProps,
FieldValues,
UseFormSetError,
} from "react-hook-form";
import { ControllerRenderProps, FieldValues } from "react-hook-form";
import * as z from "zod";
import { INPUT_COMPONENTS } from "./config";

Expand Down Expand Up @@ -81,7 +77,3 @@ export type AutoFormInputComponentProps = {
zodItem: z.ZodAny;
className?: string;
};

export type SubmitOptions<SchemaType extends z.infer<z.ZodObject<any, any>>> = {
setError: UseFormSetError<SchemaType>;
};

0 comments on commit 7d1f42b

Please sign in to comment.