-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Expose default submit handler #148
Comments
I'm surprised it's not there. How did the developers use this library then? |
there's a lot of code and checks to do at the moment... const wasFormSubmitted = useRef(false)
const fetcher = useFetcher<ActionResponse>()
const methods = useRemixForm<FormDataType>({
resolver,
fetcher,
defaultValues: {
secretKey: '',
},
submitConfig: {
method: 'POST',
action: '/settings/api-credentials/save',
},
})
const {
handleSubmit,
formState: { errors, isSubmitSuccessful },
} = methods
useEffect(() => {
if (
isSubmitSuccessful &&
fetcher.state === 'idle' &&
fetcher.data?.success &&
!wasFormSubmitted.current
) {
wasFormSubmitted.current = true
shopify.toast.show('Saved')
}
if (fetcher.state === 'submitting') {
wasFormSubmitted.current = false
}
}, [isSubmitSuccessful, fetcher.state, fetcher.data]) |
What would be the use case that you want to wrap it and not use the one provided by the hook or the onValid provided by the submitHandler, that one on it's own wraps around onValid. |
Our use case would be to add more data to the form when the form is submitted. Basically we make a request to a third-party (this must be called from the client, we can't move this to the server-side), then we append the data from the response to the form. Of course we can do this before calling I guess we would need to do this somewhere here: remix-hook-form/src/hook/index.tsx Line 104 in 7bbaef9
Before creating the formData to make sure that we can change the data that's being sent to the server. Submit form -> Form is in submitting state -> Send request to third-party -> Manipulate data based on the response -> Continue on with the action submitting After researching this more in-depth I'm not even sure if this is a good idea anymore. Probably way better to handle this outside of the form lifecycle. |
Did a quick prototype: remix-hook-form/src/hook/index.tsx Line 224 in 7bbaef9
Exposed remix-hook-form/src/hook/index.tsx Lines 231 to 239 in 7bbaef9
defaultSubmitHandler: onSubmit Then used it like this: submitHandlers: {
onValid: async (...args) => {
await new Promise((resolve) => setTimeout(resolve, 10000));
args[0].thirdPartyData= "...";
await form.defaultSubmitHandler(...args);
},
}, This way it's possible to do something when the form is invalid and still submit it the original way.
remix-hook-form/src/hook/index.tsx Line 220 in 7bbaef9
|
submitHandlers.onValid
allows us to override the valid submit handler, but we can't call the default submit handler. Therefore it's not possible to "wrap" the default submit handler with custom logic - I'm not aware of any way to do that.Is it possible to do it? If not, would it make sense to expose the default submit handler?
The text was updated successfully, but these errors were encountered: