Skip to content

Conversation

@GoldGroove06
Copy link
Contributor

@GoldGroove06 GoldGroove06 commented Jun 2, 2025

#1128

Solved by using a hidden select tag inside the root of select component.
Addition - a hook to conditional render the select tag if a form element is in the parent dom is in progress.

Summary by CodeRabbit

  • New Features
    • Added support for associating a name with the custom select component, enabling its value to be included in form submissions.
    • Introduced a new story demonstrating how the custom select integrates with forms and how submitted data is captured and displayed.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 2, 2025

Walkthrough

The changes introduce an optional name prop to the SelectPrimitiveRoot component, allowing it to render a hidden native <select> element for form integration. Additionally, a new story, FormExample, demonstrates how the custom select component interacts with native form submission and displays the submitted data. A new hook useIsInsideForm is added to detect if the component is inside a form, and tests for this hook are included.

Changes

File(s) Change Summary
src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx Added optional name prop to SelectPrimitiveRootProps and component; renders hidden native <select> element when inside a form.
src/core/primitives/Select/stories/Select.stories.tsx Added FormExample story demonstrating form integration and submission with the custom select component.
src/core/hooks/useIsInsideFrom/index.ts Added useIsInsideForm hook to detect if an element is inside a form element.
src/core/hooks/useIsInsideFrom/useIsInsideForm.test.tsx Added tests for useIsInsideForm hook covering various DOM scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Form
    participant SelectPrimitiveRoot
    participant NativeSelect

    User->>Form: Submit form
    Form->>SelectPrimitiveRoot: Collect selected value
    SelectPrimitiveRoot->>NativeSelect: Set value (hidden)
    Form->>Form: Gather all form data (including hidden select)
    Form->>User: Display submitted data
Loading

Possibly related PRs

  • Select primitive t1 #1117: Introduced the SelectPrimitive component and its subcomponents; this PR extends SelectPrimitiveRoot to support native form integration using a hidden <select> element.

Suggested labels

automerge

Suggested reviewers

  • kotAPI

Poem

In the meadow of code, a new root now grows,
With a hidden select, where the form data flows.
Stories now show how the values are caught,
Submitting with ease, just as rabbits had thought.
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm error Exit handler never called!
npm error This is an error with npm itself. Please report this error at:
npm error https://github.com/npm/cli/issues
npm error A complete log of this run can be found in: /.npm/_logs/2025-06-04T13_01_35_283Z-debug-0.log

✨ Finishing Touches
  • 📝 Generate Docstrings

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 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
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: 0

🧹 Nitpick comments (3)
src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx (1)

61-69: Optimize the hidden select rendering and handle edge cases.

The hidden select implementation is functionally correct, but there are a few improvements to consider:

  1. Conditional rendering: The select should only render when name is provided
  2. Empty value handling: Consider what happens when selectedValue is empty or undefined
  3. Option text: Using selectedValue as option text may not be user-friendly if it's a technical identifier

Apply this diff to address these concerns:

                    {children}
-                    {/* Add hidden native select for form control */}
-                    <select
-                        name={name}
-                        value={selectedValue}
-                        hidden
-                        aria-hidden="true"
-                        tabIndex={-1}
-                    >
-                        <option value={selectedValue}>{selectedValue}</option>
-                    </select>
+                    {/* Add hidden native select for form control */}
+                    {name && (
+                        <select
+                            name={name}
+                            value={selectedValue || ''}
+                            hidden
+                            aria-hidden="true"
+                            tabIndex={-1}
+                            readOnly
+                        >
+                            <option value={selectedValue || ''}>{selectedValue || ''}</option>
+                        </select>
+                    )}

The changes include:

  • Conditional rendering only when name is provided
  • Safe handling of empty/undefined selectedValue
  • Added readOnly attribute to prevent direct interaction
src/core/primitives/Select/stories/Select.stories.tsx (2)

90-94: Improve consistency between custom and native select.

For better comparison and validation, consider adding a default value to the native select to match the potential behavior of the custom select.

Apply this diff to improve consistency:

                <select name="nativeSelect">
+                    <option value="">Select an option</option>
                    <option value="option1">Option 1</option>
                    <option value="option2">Option 2</option>
                    <option value="option3">Option 3</option>
                </select>

105-110: Enhance user experience with better feedback.

Consider adding user feedback when no data has been submitted yet and improve the visual presentation.

Apply this diff to enhance the user experience:

            {submittedData && (
                <div className="mt-4 p-4 bg-gray-100 rounded">
                    <h3 className="font-bold mb-2">Submitted Form Data:</h3>
                    <pre>{submittedData}</pre>
                </div>
            )}
+            {!submittedData && (
+                <div className="mt-4 p-4 bg-gray-50 rounded">
+                    <p className="text-gray-600">Submit the form to see the captured data</p>
+                </div>
+            )}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f1480f2 and 783cf74.

📒 Files selected for processing (2)
  • src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx (2 hunks)
  • src/core/primitives/Select/stories/Select.stories.tsx (1 hunks)
🔇 Additional comments (4)
src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx (2)

15-15: Good addition of the optional name prop.

The addition of the optional name prop aligns well with the PR objectives to enable form control support.


21-21: Function signature correctly updated.

The destructuring properly includes the new name parameter.

src/core/primitives/Select/stories/Select.stories.tsx (2)

62-74: Well-structured form handling logic.

The form submission handler correctly prevents default behavior, extracts form data, and updates state. The implementation demonstrates proper React form handling patterns.


79-88: Good demonstration of the custom select with form integration.

The custom select properly uses the name prop to enable form submission, which directly demonstrates the PR's objective.

<Primitive.div {...props} className={className}>
{children}
{/* Add hidden native select for form control */}
<select
Copy link
Collaborator

Choose a reason for hiding this comment

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

this doesnt conditionally render based on if form is present right? it will always render, either form or not

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Solved by using a hidden select tag inside the root of select component. Addition - a hook to conditional render the select tag if a form element is in the parent dom is in progress.

it is in progress

Copy link
Collaborator

Choose a reason for hiding this comment

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

makes sense


let current: HTMLElement | null = element;
while (current) {
if (current.tagName === 'FORM') return true;
Copy link
Collaborator

Choose a reason for hiding this comment

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

how do these work? do we also need to check for lowercased form?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests pretty much describe it all

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: 0

♻️ Duplicate comments (1)
src/core/hooks/useIsInsideFrom/index.ts (1)

19-19: The uppercase tagName check is correct.

Addressing the past review comment: No need to check for lowercase form. In HTML documents, the tagName property always returns uppercase tag names, so checking for 'FORM' is the correct approach.

🧹 Nitpick comments (1)
src/core/hooks/useIsInsideFrom/index.ts (1)

1-33: Fix the folder name typo.

The folder name contains a typo: useIsInsideFrom should be useIsInsideForm to match the hook name and functionality.

-src/core/hooks/useIsInsideFrom/index.ts
+src/core/hooks/useIsInsideForm/index.ts
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d09bc55 and f4851e0.

📒 Files selected for processing (1)
  • src/core/hooks/useIsInsideFrom/index.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (1)
src/core/hooks/useIsInsideFrom/index.ts (1)

8-30: LGTM! Solid hook implementation.

The hook implementation is well-structured with proper:

  • Edge case handling for null elements
  • Correct DOM traversal logic
  • Appropriate React hook patterns with useState and useEffect
  • Clear JSDoc documentation

The logic correctly identifies form ancestry by traversing up the DOM tree.

@kotAPI kotAPI added the automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met label Jun 4, 2025
@kodiakhq kodiakhq bot merged commit 9fdf573 into rad-ui:main Jun 4, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants