Open lukaszsamson opened 1 month ago
Hey @lukaszsamson, your issue is that the checkbox has the form
attribute set to the id of the form yet still does not belong to it. You say that it is because of it being a <button />
instead of a <input />
. The form attribute works with both in HTML. You can very well use it with a button, for example:
<form action="/action_page.php" method="get" id="form1">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname">
</form>
<button type="submit" form="form1" value="Submit">Submit</button>
The real reason that it doesn't work is that when a native checkbox is created, the browser automatically includes it in the FormData when it is submitted but when we use a non-native element like the ones from radix-ui
.
The @radix-ui/react-checkbox
component is not a native input element. It's implemented as a button
that visually behaves like a checkbox but doesn't naturally interact with the form data submission process. Because it's a button element:
If you really wanna add it to the FormData, you're better of using a simple state for it like
const [isChecked, setIsChecked] = useState(false);
const onSubmit = (e: FormEvent) => {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);
formData.append('foo', isChecked ? 'true' : 'false');
const payload = Object.fromEntries(formData.entries());
console.log(payload);
};
and the checkbox becomes
<Checkboxn
name="foo"
checked={isChecked}
onCheckedChange={setIsChecked} />
I hope this helps!
Sure I can hack a workaround like that or modify the component to render an invisible controlled checkbox input. My point is such basic functionality should work without resorting to hacks
Describe the bug
When form is passed as an attribute, checkbox does not contribute value to submitted data
The reason is Checkbox is implemented as button and does not have a correctly behaving HTML input element
Related issue https://github.com/shadcn-ui/ui/issues/733
Affected component/components
Checkbox
How to reproduce
Codesandbox/StackBlitz link
No response
Logs
No response
System Info
Before submitting