Skip to content
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

Primitive space bug fix #908

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Primitive space bug fix #908

wants to merge 4 commits into from

Conversation

GoldGroove06
Copy link
Collaborator

@GoldGroove06 GoldGroove06 commented Mar 1, 2025

fixes #667 by throwing error when multiple elements in children

Summary by CodeRabbit

  • Bug Fixes
    • Improved component validation by enforcing a single child element, reducing potential rendering issues related to extra spaces or unintended multiple elements.

@GoldGroove06 GoldGroove06 requested a review from kotAPI March 1, 2025 08:08
Copy link
Contributor

coderabbitai bot commented Mar 1, 2025

Walkthrough

The pull request adds a new error handling condition to the PrimitiveComponent function within the createPrimitiveComponent method. It throws an error if the children prop contains more than one element, ensuring that only a single React element is passed. There are no other alterations to the functionality or public API.

Changes

File Change Summary
src/core/primitives/Primitive/index.tsx Added error handling to enforce that the children prop only accepts a single React element.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant PC as PrimitiveComponent

    U->>PC: Invoke component with children prop
    PC->>PC: Check if children has multiple elements
    alt More Than One Child
        PC-->>U: Throw Error ("children is only expected to receive a single React element child. Try trimming spaces.")
    else Single Child
        PC-->>U: Render child element
    end
Loading

Possibly related PRs

Suggested labels

automerge

Suggested reviewers

  • kotAPI

Poem

I'm just a hopping rabbit in the codewood so deep,
With one child allowed and no more to keep.
The error hops in, ensuring things stay neat,
A tidy solution where only single elements meet.
Cheers to clean logic with every byte I leap! 🐰✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6d74b7f and e9e1a3b.

📒 Files selected for processing (1)
  • src/core/primitives/Primitive/index.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/core/primitives/Primitive/index.tsx

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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

🧹 Nitpick comments (3)
src/core/primitives/Primitive/index.tsx (3)

18-18: Consider handling edge cases in child validation

The current check doesn't account for all possible scenarios:

  1. A single React element wrapped in an array would incorrectly trigger the error
  2. Fragments with multiple children might bypass this check
  3. Text with spaces might be interpreted as multiple children in JSX

A more comprehensive approach would be:

// Check for multiple children
if (React.Children.count(children) > 1) {
  throw new Error("children is only expected to receive a single React element child. Try trimming spaces.");
}

// Validate the single child if needed
if (React.Children.count(children) === 1 && !React.isValidElement(React.Children.only(children))) {
  throw new Error("children must be a valid React element.");
}
🧰 Tools
🪛 GitHub Actions: Build RAD UI

[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


8-11: Consider making children prop more specific

Since this component expects exactly one React element as a child, you could make your TypeScript type more specific:

interface PrimitiveProps extends React.HTMLAttributes<HTMLElement> {
  asChild?: boolean;
- children?: React.ReactNode;
+ children?: React.ReactElement;
}

This would provide better type safety and documentation of the component's expectations.


17-18: Error message could be more specific

The current error message mentions "trimming spaces," but the check doesn't specifically target whitespace issues.

Consider making the error message more explicit about the requirement for a single child element:

- throw new Error("children is only expected to receive a single React element child. Try trimming spaces.")
+ throw new Error("Primitive component expects exactly one React element child. Multiple children (including whitespace) are not supported.")
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8cbaee0 and 6d74b7f.

📒 Files selected for processing (1)
  • src/core/primitives/Primitive/index.tsx (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: Build RAD UI
src/core/primitives/Primitive/index.tsx

[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.

@@ -14,6 +14,8 @@ interface PrimitiveProps extends React.HTMLAttributes<HTMLElement> {
const createPrimitiveComponent = (elementType: SupportedElement) => {
const PrimitiveComponent = React.forwardRef<HTMLElement, PrimitiveProps>((props, ref) => {
const { asChild = false, children, ...elementProps } = props;

if (children?.length) throw new Error("children is only expected to receive a single React element child. Try trimming spaces.")
Copy link
Contributor

@coderabbitai coderabbitai bot Mar 1, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix TypeScript error: Property 'length' does not exist on type 'React.ReactNode'

The build pipeline is failing because React.ReactNode (the type of children) can be a string, number, boolean, React element, iterable, or portal - and not all of these types have a length property (specifically numbers and booleans).

Here's a more robust solution that properly checks for multiple children:

- if (children?.length) throw new Error("children is only expected to receive a single React element child. Try trimming spaces.")
+ if (React.Children.count(children) > 1) throw new Error("children is only expected to receive a single React element child. Try trimming spaces.")

Using React.Children.count() will safely count the number of children regardless of their type.

📝 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 (children?.length) throw new Error("children is only expected to receive a single React element child. Try trimming spaces.")
if (React.Children.count(children) > 1) throw new Error("children is only expected to receive a single React element child. Try trimming spaces.")
🧰 Tools
🪛 GitHub Actions: Build RAD UI

[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.


[error] 18-23: TS2339: Property 'length' does not exist on type 'string | number | boolean | ReactElement<any, string | JSXElementConstructor> | Iterable | ReactPortal'. Property 'length' does not exist on type 'number'.

Copy link
Collaborator

Choose a reason for hiding this comment

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

you can use this suggestion

Copy link
Contributor

Choose a reason for hiding this comment

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

@kotAPI Great! The suggested change to use React.Children.count(children) > 1 will resolve the TypeScript error while properly implementing the intended validation for multiple children. This is the correct approach since React.ReactNode can be various types (string, number, boolean, React element, etc.), and not all of them have a length property.

@GoldGroove06
Copy link
Collaborator Author

image
Reason for failing build

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.

BUG: Primitive Space bug (silly bug)
2 participants