-
-
Notifications
You must be signed in to change notification settings - Fork 345
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
[FEATURE] Form callback API #989
Comments
Hi @crutchcorn can I be assigned this ticket? |
Hey @crutchcorn . Thank you very much for taking up my ideas of #949 ! However, I don't find this API proposal of yours particularly advantageous. It would require me to put the business logic that handles the server response (e.g. navigating to different routes after submissions) inside the But that is just my opinion. I guess I could also live with the API suggested above. |
I've got this exact use case, and I was able to solve it by looking at the id of the submitting button. () => {
let form = useForm({
defaultValues: {},
onSubmit: async ({ value }) => await post(value),
});
return (
<form
onSubmit={async (event) => {
event.preventDefault();
event.stopPropagation();
await form.handleSubmit();
if (event.nativeEvent.submitter.id === "save-and-go-back") {
// redirect
}
}}
>
<button id="save" name="save" type="submit">
Save
</button>
<button id="save-and-go-back" name="save-and-go-back" type="submit">
Save & Go Back
</button>
</form>
);
}; What I would love, is the ability to get the response returned from my So it would become () => {
let form = useForm({
defaultValues: {},
onSubmit: async ({ value }) => await post(value),
});
return (
<form
onSubmit={async (event) => {
event.preventDefault();
event.stopPropagation();
const response = await form.handleSubmit();
if (event.nativeEvent.submitter.id === "save-and-go-back") {
history.push(`items/${response.id}`)
}
}}
>
<button id="save" name="save" type="submit">
Save
</button>
<button id="save-and-go-back" name="save-and-go-back" type="submit">
Save & Go Back
</button>
</form>
);
}; |
I think there's value in getting the result from One possibility would be to move closer to the |
@zoodirector, can you help me understand (in psuedo-code) what a real-world example of what you'd want to build is? I'm still not seeing the differences between your suggestion and our design here with the exception of typesafety |
Imagine a usecase where the user wants two buttons, they both send the item to the backend (after form validation) but one goes back to the list and the other goes to a different page, like a step 2 or a detail page.
To solve this, we're thinking of introducing a new API called
onSubmitMeta
that allows you to pass arguments to theonSubmit
function via ameta
field.This API might look something like this:
By having
onSubmitMeta
it enables us to have a few features on top of passing properties such as:meta
field from the FW itselfThis API should be purely additive, so it may not make it into v1 of TanStack Form
The text was updated successfully, but these errors were encountered: