From 3cf99adc0780edd1d6872d98c9e3f327fe217e0c Mon Sep 17 00:00:00 2001 From: aon <21188659+aon@users.noreply.github.com> Date: Thu, 10 Oct 2024 22:44:44 -0300 Subject: [PATCH 1/2] feat: add action support from Form config --- README.md | 5 ++++- src/hook/index.tsx | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6682f22..d0f98c4 100644 --- a/README.md +++ b/README.md @@ -397,7 +397,10 @@ If you're using a GET request formData is not available on the request so you ca - `submitHandlers`: an object containing two properties: - `onValid`: can be passed into the function to override the default behavior of the `handleSubmit` success case provided by the hook. - `onInvalid`: can be passed into the function to override the default behavior of the `handleSubmit` error case provided by the hook. -- `submitConfig`: allows you to pass additional configuration to the `useSubmit` function from Remix, such as `{ replace: true }` to replace the current history entry instead of pushing a new one. The `submitConfig` trumps `Form` props from Remix. But the `Form` props are used if no submitConfig is provided. +- `submitConfig`: allows you to pass additional configuration to the `useSubmit` function from Remix, such as `{ replace: true }` to replace the current history entry instead of pushing a new one. The `submitConfig` trumps `Form` props from Remix. The following props will be used from `Form` if no submitConfig is provided: + - `method` + - `action` + - `encType` - `submitData`: allows you to pass additional data to the backend when the form is submitted. - `fetcher`: if provided then this fetcher will be used to submit data and get a response (errors / defaultValues) instead of Remix's `useSubmit` and `useActionData` hooks. diff --git a/src/hook/index.tsx b/src/hook/index.tsx index 14136a3..b28c039 100644 --- a/src/hook/index.tsx +++ b/src/hook/index.tsx @@ -90,11 +90,26 @@ export const useRemixForm = ({ // Submits the data to the server when form is valid const onSubmit = useMemo( () => - (data: T, e: any, formEncType?: FormEncType, formMethod?: FormMethod) => { + ( + data: T, + e: any, + formEncType?: FormEncType, + formMethod?: FormMethod, + formAction?: string, + ) => { setIsSubmittingNetwork(true); setIsSubmittedSuccessfully(true); const encType = submitConfig?.encType ?? formEncType; const method = submitConfig?.method ?? formMethod ?? "post"; + + let action = submitConfig?.action; + if (!action && formAction) { + const formActionUrl = new URL(formAction, window.location.origin); + action = formActionUrl.origin === window.location.origin + ? `${formActionUrl.pathname}${formActionUrl.search}` + : formActionUrl.href; + } + const submitPayload = { ...data, ...submitData }; const formData = encType === "application/json" @@ -104,6 +119,7 @@ export const useRemixForm = ({ ...submitConfig, method, encType, + action, }); }, [submit, submitConfig, submitData, stringifyAllValues], @@ -196,11 +212,12 @@ export const useRemixForm = ({ () => (e?: FormEvent) => { const encType = e?.currentTarget?.enctype as FormEncType | undefined; const method = e?.currentTarget?.method as FormMethod | undefined; + const action = e?.currentTarget?.action; const onValidHandler = submitHandlers?.onValid ?? onSubmit; const onInvalidHandler = submitHandlers?.onInvalid ?? onInvalid; return methods.handleSubmit( - (data, e) => onValidHandler(data, e, encType, method), + (data, e) => onValidHandler(data, e, encType, method, action), onInvalidHandler, )(e); }, From 0fcbccc76f30fce7a435c34483447df7f93800e0 Mon Sep 17 00:00:00 2001 From: aon <21188659+aon@users.noreply.github.com> Date: Tue, 15 Oct 2024 09:26:08 -0300 Subject: [PATCH 2/2] fix: simplify action parsing logic --- src/hook/index.tsx | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/hook/index.tsx b/src/hook/index.tsx index b28c039..f367f14 100644 --- a/src/hook/index.tsx +++ b/src/hook/index.tsx @@ -101,15 +101,7 @@ export const useRemixForm = ({ setIsSubmittedSuccessfully(true); const encType = submitConfig?.encType ?? formEncType; const method = submitConfig?.method ?? formMethod ?? "post"; - - let action = submitConfig?.action; - if (!action && formAction) { - const formActionUrl = new URL(formAction, window.location.origin); - action = formActionUrl.origin === window.location.origin - ? `${formActionUrl.pathname}${formActionUrl.search}` - : formActionUrl.href; - } - + const action = submitConfig?.action ?? formAction; const submitPayload = { ...data, ...submitData }; const formData = encType === "application/json"