-
-
Notifications
You must be signed in to change notification settings - Fork 33
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe pull request adds a new error handling condition to the Changes
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
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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 validationThe current check doesn't account for all possible scenarios:
- A single React element wrapped in an array would incorrectly trigger the error
- Fragments with multiple children might bypass this check
- 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 specificSince 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 specificThe 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
📒 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.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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'.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
…into fix/primitive-space-bug
fixes #667 by throwing error when multiple elements in children
Summary by CodeRabbit