Skip to content

fix(checkbox): pass form id to input tag #5518

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 3 commits into
base: canary
Choose a base branch
from

Conversation

kivojenko
Copy link

@kivojenko kivojenko commented Jul 24, 2025

Closes

📝 Description

There already is similar request present #4979 but it looks abandoned.

In HTML it is possible to add form id to inputs that are not children of this form and they are being picked up for this form.

<Checkbox form="test-form" />
<Form id="test-form"/>

⛳️ Current behavior (updates)

Adding form currently works for HeroUI Input component, but not for Checkbox. For Checkbox form parameter is being added to label tag, which will not make it work.

<label form="test-form">
    <input type="checkbox" value="" />
</label>

🚀 New behavior

Form parameter is being passed to input tag, making it possible to use checkboxes outside of form.

<label>
    <input form="test-form" type="checkbox" value="" />
</label>

💣 Is this a breaking change (Yes/No):

No.

Summary by CodeRabbit

  • New Features

    • Checkboxes can now be associated with a specific form using the form ID, allowing inputs to be linked to forms even when rendered outside the form element.
    • Added a new example demonstrating how to use a checkbox outside of its associated form.
  • Documentation

    • Updated stories to showcase the new checkbox and form association capability.

@kivojenko kivojenko requested a review from jrgarciadev as a code owner July 24, 2025 14:07
Copy link

changeset-bot bot commented Jul 24, 2025

🦋 Changeset detected

Latest commit: f5326da

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@heroui/checkbox Minor
@heroui/form Minor
@heroui/table Patch
@heroui/react Patch
@heroui/autocomplete Patch
@heroui/date-input Patch
@heroui/date-picker Patch
@heroui/input-otp Patch
@heroui/input Patch
@heroui/number-input Patch
@heroui/radio Patch
@heroui/select Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jul 24, 2025

@ksenijakivojenko is attempting to deploy a commit to the HeroUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Jul 24, 2025

Walkthrough

The changes update the @heroui/checkbox package to ensure the form attribute is passed to the checkbox input element, enabling association with a specific form even when the checkbox is rendered outside the form. A new story demonstrates this behavior. No other logic or structural changes are introduced.

Changes

File(s) Change Summary
packages/components/checkbox/src/use-checkbox.ts Updated useCheckbox hook to accept and pass form prop to input element.
packages/components/checkbox/stories/checkbox.stories.tsx Added a new story (SeparateFromFormTemplate and export) to demonstrate checkbox association via form attr.
.changeset/tasty-glasses-marry.md Added changeset describing the update to pass form ID to checkbox input.

Sequence Diagram(s)

sequenceDiagram
    participant ConsumerApp
    participant Checkbox
    participant Form

    ConsumerApp->>Checkbox: Render <Checkbox form="form-123" name="terms" />
    Checkbox->>Checkbox: Pass form prop to input element
    ConsumerApp->>Form: Render <Form id="form-123" />
    Note over Checkbox,Form: Checkbox input is outside Form but linked via form="form-123"
    ConsumerApp->>Form: Submit Form
    Form->>Form: Collect form data (includes Checkbox input)
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

same comment. we need a general solution to handle all the cases rather than just passing one.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🔭 Outside diff range comments (1)
.changeset/tasty-glasses-marry.md (1)

1-7: Remove @heroui/form from the changeset

We ran a check against packages/components/form and found no modified files in this PR:

  • git status --porcelain returned no changes in packages/components/form

Since there are no updates to the form package, please remove it from your changeset (or add the corresponding form-related changes if they were intended).

• File: .changeset/tasty-glasses-marry.md
• Remove this line:

-"@heroui/form": minor
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9dceb11 and f5326da.

📒 Files selected for processing (3)
  • .changeset/tasty-glasses-marry.md (1 hunks)
  • packages/components/checkbox/src/use-checkbox.ts (2 hunks)
  • packages/components/checkbox/stories/checkbox.stories.tsx (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/components/checkbox/stories/checkbox.stories.tsx (4)
packages/components/checkbox/src/index.ts (2)
  • CheckboxProps (9-9)
  • Checkbox (17-17)
apps/docs/content/components/input/built-in-validation.raw.jsx (2)
  • submitted (4-4)
  • onSubmit (6-11)
packages/components/form/src/form.tsx (1)
  • Form (9-15)
packages/core/theme/src/components/button.ts (1)
  • button (475-475)
🔇 Additional comments (3)
packages/components/checkbox/src/use-checkbox.ts (1)

90-90: LGTM: Form prop extraction is correct.

The form prop is properly extracted from the component props, enabling the HTML form attribute functionality for checkboxes.

packages/components/checkbox/stories/checkbox.stories.tsx (2)

213-261: Story template demonstrates the new feature effectively.

The SeparateFromFormTemplate correctly showcases the new form attribute functionality by rendering a checkbox outside the form but linking it via the form prop. The implementation follows established patterns for form handling and state management.


367-373: Story export is properly configured.

The SeparateFromForm story export correctly references the new template and uses appropriate default props.

@@ -313,6 +314,7 @@ export function useCheckbox(props: UseCheckboxProps = {}) {
...mergeProps(inputProps, focusProps),
className: slots.hiddenInput({class: classNames?.hiddenInput}),
onChange: chain(inputProps.onChange, handleCheckboxChange),
form: form,
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix missing dependency in useMemo hook.

The form prop should be included in the dependency array for the getInputProps callback to ensure proper re-renders when the form value changes.

  const getInputProps: PropGetter = useCallback(() => {
    return {
      ref: mergeRefs(inputRef, ref),
      ...mergeProps(inputProps, focusProps),
      className: slots.hiddenInput({class: classNames?.hiddenInput}),
      onChange: chain(inputProps.onChange, handleCheckboxChange),
      form: form,
    };
- }, [inputProps, focusProps, handleCheckboxChange, classNames?.hiddenInput]);
+ }, [inputProps, focusProps, handleCheckboxChange, classNames?.hiddenInput, form]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
form: form,
const getInputProps: PropGetter = useCallback(() => {
return {
ref: mergeRefs(inputRef, ref),
...mergeProps(inputProps, focusProps),
className: slots.hiddenInput({class: classNames?.hiddenInput}),
onChange: chain(inputProps.onChange, handleCheckboxChange),
form: form,
};
}, [inputProps, focusProps, handleCheckboxChange, classNames?.hiddenInput, form]);
🤖 Prompt for AI Agents
In packages/components/checkbox/src/use-checkbox.ts at line 317, the useMemo
hook for the getInputProps callback is missing the form prop in its dependency
array. Add form to the dependency array of the useMemo hook to ensure the
callback updates correctly when the form value changes.

e.preventDefault();
const data = Object.fromEntries(new FormData(e.currentTarget));

if (data.terms == "true") {
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Use strict equality operator.

Use strict equality (===) instead of loose equality (==) for better type safety and clarity.

-    if (data.terms == "true") {
+    if (data.terms === "true") {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (data.terms == "true") {
if (data.terms === "true") {
🤖 Prompt for AI Agents
In packages/components/checkbox/stories/checkbox.stories.tsx at line 223,
replace the loose equality operator (==) with the strict equality operator (===)
in the condition checking if data.terms equals "true" to ensure type-safe
comparison.

label: "text-small",
}}
form="heroui-form"
name="checkbox"
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix checkbox name mismatch.

The checkbox has name="checkbox" but the form submission logic checks for data.terms. This will cause the checkbox value to never be found, making the story non-functional.

-        name="checkbox"
+        name="terms"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
name="checkbox"
name="terms"
🤖 Prompt for AI Agents
In packages/components/checkbox/stories/checkbox.stories.tsx at line 240, the
checkbox input has the name attribute set to "checkbox" while the form
submission logic expects the name "terms". To fix this, change the checkbox's
name attribute from "checkbox" to "terms" so that the form data correctly
captures the checkbox value during submission.

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.

3 participants