Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 61 additions & 2 deletions src/hook/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ const fetcherSubmitMock = vi.fn();
const useActionDataMock = vi.hoisted(() => vi.fn());

const useNavigationMock = vi.hoisted(() =>
vi.fn<() => Pick<Navigation, "state" | "formData">>(() => ({
vi.fn<() => Pick<Navigation, "state" | "formData" | "json">>(() => ({
state: "idle",
formData: undefined,
json: undefined,
})),
);

Expand Down Expand Up @@ -290,12 +291,70 @@ describe("useRemixForm", () => {
useNavigationMock.mockReturnValue({
state: "submitting",
formData: new FormData(),
json: undefined,
});
rerender();

expect(result.current.formState.isSubmitting).toBe(true);

useNavigationMock.mockReturnValue({ state: "idle", formData: undefined });
useNavigationMock.mockReturnValue({
state: "idle",
formData: undefined,
json: undefined,
});
rerender();

expect(result.current.formState.isSubmitting).toBe(false);
});

it("should reset isSubmitting when the form is submitted using encType: application/json", async () => {
submitMock.mockReset();
useNavigationMock.mockClear();

const { result, rerender } = renderHook(() =>
useRemixForm({
resolver: () => ({ values: {}, errors: {} }),
submitConfig: {
action: "/submit",
encType: "application/json",
},
}),
);

expect(result.current.formState.isSubmitting).toBe(false);

act(() => {
result.current.handleSubmit({} as any);
});
expect(result.current.formState.isSubmitting).toBe(true);

await waitFor(() => expect(submitMock).toHaveBeenCalledTimes(1));

expect(result.current.formState.isSubmitting).toBe(true);

expect(submitMock).toHaveBeenCalledWith(
{},
{
method: "post",
action: "/submit",
encType: "application/json",
},
);

useNavigationMock.mockReturnValue({
state: "submitting",
formData: undefined,
json: {},
});
rerender();

expect(result.current.formState.isSubmitting).toBe(true);

useNavigationMock.mockReturnValue({
state: "idle",
formData: undefined,
json: undefined,
});
rerender();

expect(result.current.formState.isSubmitting).toBe(false);
Expand Down
26 changes: 18 additions & 8 deletions src/hook/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,24 @@ export const useRemixForm = <
const methods = useForm({ ...formProps, errors: data?.errors });
const navigation = useNavigation();
// Either it's submitted to an action or submitted to a fetcher (or neither)
const isSubmittingForm = useMemo(
() =>
Boolean(
(navigation.state !== "idle" && navigation.formData !== undefined) ||
(fetcher?.state !== "idle" && fetcher?.formData !== undefined),
),
[navigation.state, navigation.formData, fetcher?.state, fetcher?.formData],
);
const isSubmittingForm = useMemo(() => {
const navigationIsSubmitting =
navigation.state !== "idle" &&
(navigation.formData ?? navigation.json) !== undefined;

const fetcherIsSubmitting =
fetcher?.state !== "idle" &&
(fetcher?.formData ?? fetcher?.json) !== undefined;

return navigationIsSubmitting || fetcherIsSubmitting;
}, [
navigation.state,
navigation.formData,
navigation.json,
fetcher?.state,
fetcher?.formData,
fetcher?.json,
]);

// A state to keep track whether we're actually submitting the form through the network
const [isSubmittingNetwork, setIsSubmittingNetwork] = useState(false);
Expand Down