Skip to content

Commit

Permalink
Merge pull request #23 from Code-Forge-Net/22-formstateissubmitting-d…
Browse files Browse the repository at this point in the history
…oes-not-behave-as-we-could-expect

#22 - Is submitting change
  • Loading branch information
AlemTuzlak authored Sep 10, 2023
2 parents e6edd4e + 155fc3f commit 48a6cc6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/hook/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ vi.mock("@remix-run/react", () => ({
useSubmit: () => submitMock,
useActionData: () => ({}),
useFetcher: () => ({ submit: fetcherSubmitMock, data: {} }),
useNavigation: () => ({ state: "idle" }),
}));

describe("useRemixForm", () => {
Expand Down Expand Up @@ -56,7 +57,7 @@ describe("useRemixForm", () => {
onValid,
onInvalid,
},
})
}),
);

act(() => {
Expand All @@ -74,7 +75,7 @@ describe("useRemixForm", () => {
submitConfig: {
action: "/submit",
},
})
}),
);

act(() => {
Expand All @@ -99,7 +100,7 @@ describe("useRemixForm", () => {
submitConfig: {
action: "/submit",
},
})
}),
);

act(() => {
Expand All @@ -124,7 +125,7 @@ describe("RemixFormProvider", () => {
submitConfig: {
action: "/submit",
},
})
}),
);
const spy = vi.spyOn(result.current, "handleSubmit");

Expand All @@ -136,7 +137,7 @@ describe("RemixFormProvider", () => {
const { getByTestId } = render(
<RemixFormProvider {...result.current}>
<TestComponent />
</RemixFormProvider>
</RemixFormProvider>,
);

const form = getByTestId("test") as HTMLFormElement;
Expand Down
7 changes: 6 additions & 1 deletion src/hook/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
SubmitFunction,
useActionData,
useSubmit,
useNavigation,
} from "@remix-run/react";
import {
SubmitErrorHandler,
Expand Down Expand Up @@ -46,6 +47,10 @@ export const useRemixForm = <T extends FieldValues>({
const submit = fetcher?.submit ?? actionSubmit;
const data = fetcher?.data ?? actionData;
const methods = useForm<T>(formProps);
const navigation = useNavigation();
// Either it's submitted to an action or submitted to a fetcher (or neither)
const isSubmittingForm =
navigation.state !== "idle" || (fetcher && fetcher.state !== "idle");

// Submits the data to the server when form is valid
const onSubmit = (data: T) => {
Expand Down Expand Up @@ -96,7 +101,7 @@ export const useRemixForm = <T extends FieldValues>({
isDirty,
isSubmitSuccessful,
isSubmitted,
isSubmitting,
isSubmitting: isSubmittingForm || isSubmitting,
isValid,
isValidating,
touchedFields,
Expand Down
10 changes: 7 additions & 3 deletions src/testing-app/app/routes/_index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { json, type ActionArgs } from "@remix-run/node";
import { useFetcher } from "@remix-run/react";
import { getValidatedFormData, useRemixForm } from "remix-hook-form";
import { z } from "zod";

Expand All @@ -13,7 +14,8 @@ const resolver = zodResolver(schema);

export const action = async ({ request }: ActionArgs) => {
const { data } = await getValidatedFormData<FormData>(request, resolver);

await new Promise((resolve) => setTimeout(resolve, 2000));
return null;
// Make DB call or similar here.
//return json({ errors: { content: { message: "error" } } });
return {
Expand All @@ -23,15 +25,17 @@ export const action = async ({ request }: ActionArgs) => {
};

export default function Index() {
const fetcher = useFetcher();
const { register, handleSubmit, formState } = useRemixForm<FormData>({
resolver,
fetcher,
});

return (
<div>
<p>Add a thing...</p>
<p>Current Errors: {JSON.stringify(formState.errors)}</p>
<form method="post" onSubmit={handleSubmit}>
<fetcher.Form method="post" onSubmit={handleSubmit}>
<div>
<label>
Content: <input type="text" {...register("content")} />
Expand All @@ -43,7 +47,7 @@ export default function Index() {
Add
</button>
</div>
</form>
</fetcher.Form>
</div>
);
}

0 comments on commit 48a6cc6

Please sign in to comment.