Checkbox not working with FormData API #1908
-
Hi team. I'm implementing a form for the new Twilio Console and decided to take the uncontrolled approach using the now widely supported FormData interface. However, I noticed that both radio and checkbox components do not implement native radio and checkbox elements under the hood. Then, If I try to access the form data via the FormData API I won't get the data from the radio buttons and checkboxes in the form (and probably other custom components that I've not tried yet). const handleSubmit = (e: FormEvent<HTMLFormElement>): void => {
const formElement = e.target as HTMLFormElement;
e.preventDefault();
const formData = new FormData(formElement);
const data = Object.fromEntries(formData); // No data from checkboxes or radio buttons here :(
} Since this is the first form I'm implementing with paste, I just wanted to confirm if this is something you've considered and if controlled components is your recommended approach for implementing forms in Paste (I know it's the recommended approach in React). Additionally, given the high importance of forms in the Twilio console and Paste, I would like to suggest if you can expand the information about this in the documentation. Maybe a page under Patterns with your recommended guidelines on how to implement forms with Paste components: Should they be controlled? Should we avoid using the FormData or other native APIs and stick a more react-ish approach? Are you planning to support FormData in the future? I've seen that some teams in the console are using the react-hook-form-paste library, is this a library you'd recommend or support? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi there @arodriguezcortes 👋 Check out this codesandbox I created demonstrating how you can use 🕵️♀️ Some questions that will help us gain clarity 👇
I would expect an error to be thrown indicating that the parameter provided to the ❓ Does Paste have opinions about whether consumers should use controlled or uncontrolled forms? 💡 If you are looking for a more modern approach to uncontrolled forms, I think folks have been using Thanks for helping us make Paste better, looking forward to helping reach a resolution. 🙌 |
Beta Was this translation helpful? Give feedback.
-
Hi @andipants12 . Thanks a lot for your detailed answer and the CodeSandbox example. Here's something along the lines of what I was trying to achieve yesterday: import React from "react";
import { Box } from "@twilio-paste/core/box";
import { Checkbox } from "@twilio-paste/core/checkbox";
export const MyForm = () => {
const handleSubmit = (e) => {
e.preventDefault();
const formElement = e.target;
const formData = new FormData(formElement);
const data = Object.fromEntries(formData); // No data from checkboxes or radio buttons here :(
console.log(entries);
};
return (
<form id="event-listner-example" onSubmit={handleSubmit}>
<Checkbox
id="mycheckbox"
name="mycheckbox"
value="mycheckbox"
>
First
</Checkbox>
<Input
type="text"
name="myinput"
id="myinput"
defaultValue="myinput"
/>
<Box
as="button"
type="submit"
form="event-listner-example"
alignSelf="flex-start"
>
Submit form
</Box>
</form>
);
}; As per your follow up questions:
This morning I had a call with the Console team and they also recommended using |
Beta Was this translation helpful? Give feedback.
Hi there @arodriguezcortes 👋
Check out this codesandbox I created demonstrating how you can use
Checkbox
as an uncontrolled component and get the form data usingFormData
. Feel free to fork the above sandbox and show us more clearly how you are implementing this uncontrolled form, as without being able to see the context around this implementation, it is difficult to debug.🕵️♀️ Some questions that will help us gain clarity 👇
Is this
handleSubmit
function being used as a click handler on a button that is intended to submit the form? If so I have a few follow up tips and questions that might be of use:handleSubmit
function as a callback for theonSubmit
function on the…