Skip to content
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 / export default submit handler directly, or through context / callbacks #280

Open
joshuatz opened this issue Nov 10, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@joshuatz
Copy link

Is your feature request related to a problem? Please describe.

I might be missing something, but it seems like there is not a super-straightforward way (at least not as easy as I'd like) to customize the form data right before submitting, but otherwise keep the submit handler exactly the same as the default behavior of felte.

E.g., if I just want to override a single field value at the time of submission, but otherwise leave the values the same.

I see two DX issues here:

  • createDefaultSubmitHandler is not exported (from @felte/core) in a way that package consumers can easily access, which makes it hard to write wrappers that do something and then call the default handler, especially since providing a value for onSubmit causes this default handler to no longer be called. Nor does the default handler appear to be exposed in some other way.
  • onSubmit could made to be more flexible. Or perhaps expose a beforeSubmit or transformBeforeSubmit hook. Right now, you don't get access to the default handler from within it, nor can you do something like return {...values, myField: myOverride} and have that call the default handler

Describe the solution you'd like

I think it might be a good idea to expose createDefaultSubmitHandler regardless if whether a utility callback is added to transform data before submission, as this seems like a useful function to have access to. And in addition, for perhaps the best DX, expose the bound default handler directly through context, in a form that accepts values.

If you had access to it, you could do stuff like this, right?:

createForm({
    onSubmit(values, context) {
        // do something cool

        // This would be cool
        return createDefaultSubmitHandler(context.form)();

        // even better
        return context.defaultSubmitHandler(values);
    },
})

As I'd imagine that mutating data right before submission / customizing the submission payload is a somewhat common workflow, it would also be nice to add a specific callback for this. As mentioned, something like beforeSubmit or transformBeforeSubmit that takes values and context, and can return the final values payload to be used for submission.

E.g.:

createForm({
    beforeSubmit: (values, context) => ({
        ...values,
        myField: myOverride,
    }),
});

Describe alternatives you've considered

I've been looking for built-in ways to accomplish this, but so far most of them require far more unnecessary indirection and painful refactoring than I'd like.

Additional context

This has been basically my only pain point with this package - it is overall awesome and very enjoyable to use!

@joshuatz joshuatz added the enhancement New feature or request label Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant