Skip to content

feat: support form submission via FormData #2324

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

josiahwiebe
Copy link

This is a proposed improvement to simplify form input handling with minimal abstraction.

I'm creating this as a draft because I don't think it's necessarily dogmatic but I see it as an improvement.

After attending Leah's Inertia.js talk at React Miami, I found myself thinking the form input handling was a bit too verbose for some use cases. Currently, forms require individual onChange events for every input (updating the component state), which feels like it can be unnecessarily complex for some use-cases.

This PR adds the ability to pass the form element (target) as an option to the form.post method, which will then use native FormData to handle the payload.

For example:

function submit(e: React.FormEvent<HTMLFormElement>) {
  e.preventDefault();
  form.post('/user', {
    target: e.currentTarget,
  })
}

While this pattern might not be used everywhere, I believe it's valuable to offer a more native, standards-focused API for use-cases that require minimal abstraction.

@chasegiunta
Copy link

I wish this could be taken one step further. Why a submit handler to begin with?

What if there was a Form component?

<Form action="/user">
  <input type="text" name="fullName">
  <input type="text" name="fields[nestedField]">
  <input type="text" name="fields[nestedArrayField][]" value="new item">
  
  <button type="submit">Submit</button>
</Form>

@josiahwiebe
Copy link
Author

I think a Form component is probably a better approach overall.

  • Keep the submit handler for more complex scenarios
  • Create a simpler Form component that handles FormData by default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants