Skip to content

fix(checkbox): pass form prop to checkbox #4979

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

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smart-cats-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heroui/checkbox": patch
---

Pass form prop to checkbox
10 changes: 10 additions & 0 deletions packages/components/checkbox/__tests__/checkbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ describe("Checkbox", () => {
expect(onChange).toHaveBeenCalled();
});

it('should work correctly with "form" prop', () => {
const wrapper = render(
<Checkbox data-testid="checkbox-test" form="test-form-id">
Option
</Checkbox>,
);

expect(wrapper.container.querySelector("input")).toHaveAttribute("form", "test-form-id");
});

describe("validation", () => {
describe("validationBehavior=native", () => {
it("supports isRequired", async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/components/checkbox/src/use-checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,9 @@ export function useCheckbox(props: UseCheckboxProps = {}) {
...mergeProps(inputProps, focusProps),
className: slots.hiddenInput({class: classNames?.hiddenInput}),
onChange: chain(inputProps.onChange, handleCheckboxChange),
...(otherProps.form ? {form: otherProps.form} : {}),
};
}, [inputProps, focusProps, handleCheckboxChange, classNames?.hiddenInput]);
}, [inputProps, focusProps, handleCheckboxChange, classNames?.hiddenInput, otherProps]);
Comment on lines +323 to +325
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unsure how best to do this, originally I tried using filterDOMProps like is used in the input. But that ended up including other props which were already being passed to the <Label> that gets created by this component.

Surprised that this isn't coming through from the useCheckbox from react-aria

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In < Input/>, does form only exist in input and not in base? I think this PR's change only cover form prop and there should be other cases.

Copy link
Contributor Author

@smozely smozely Aug 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry missed the notification for this comment.

Yeah I guess what I mean from my comment, is it felt like this should probably use filterDomProps but I was unsure of what props should be passed to filterDomProps.

i.e. should it match packages/components/input/src/use-input.ts which has this ...

          filterDOMProps(otherProps, {
            enabled: true,
            labelable: true,
            omitEventNames: new Set(Object.keys(inputProps)),
          }),

Which seemed like it was potentially passing a lot more things down to the DOM element, which might be the right thing to do, but I was little worried there would be some unknown prop that shouldn't be passed.

So I put this PR up with just what I had done to get it working, basically to get the conversation started


const getLabelProps: PropGetter = useCallback(
() => ({
Expand Down