{ "$schema": "https://ui.shadcn.com/schema/registry-item.json", "name": "native-internal-utils-children-to-string", "type": "registry:lib", "title": "Children To String", "description": "Children To String from @pitsi-ui/native for native projects.", "dependencies": [ "react-native-reanimated@^4.1.1" ], "registryDependencies": [], "files": [ { "path": "registry/native-ui/src/helpers/internal/utils/children-to-string.ts", "content": "import { Children, isValidElement, type ReactNode } from \"react\";\nimport type { SharedValue } from \"react-native-reanimated\";\n\n/**\n * Recursively checks if children contain any React elements.\n * Used to determine if children can be stringified.\n *\n * @param children - React children to check\n * @returns True if children contain React elements, false otherwise\n */\nfunction hasReactElements(children: ReactNode): boolean {\n if (children == null || typeof children === \"boolean\") {\n return false;\n }\n\n if (isValidElement(children)) {\n return true;\n }\n\n if (Array.isArray(children)) {\n return children.some((child) => hasReactElements(child));\n }\n\n return false;\n}\n\n/**\n * Converts React children to a string representation.\n * Handles cases where children might be an array of mixed types (strings, numbers, variables).\n *\n * @param children - React children that might be string, number, array, or React elements\n * @returns A string representation of the children or null if not convertible\n */\nexport function childrenToString(children: ReactNode | SharedValue): string | null {\n // Handle null/undefined\n if (children == null) {\n return null;\n }\n\n // Handle string directly\n if (typeof children === \"string\") {\n return children;\n }\n\n // Handle number\n if (typeof children === \"number\") {\n return String(children);\n }\n\n // Handle boolean (usually we don't want to render true/false as text)\n if (typeof children === \"boolean\") {\n return null;\n }\n\n // Check if children is a React element - if so, cannot be stringified\n if (isValidElement(children)) {\n return null;\n }\n\n // Handle array of children (e.g., {someVar} text)\n if (Array.isArray(children)) {\n // Check if array contains any React elements - if so, cannot be stringified\n // This handles cases where conditional children create arrays with React elements\n if (hasReactElements(children)) {\n return null;\n }\n\n const stringified = children\n .map((child) => {\n // Recursively handle each child\n if (typeof child === \"string\" || typeof child === \"number\") {\n return String(child);\n }\n // Skip booleans, null, undefined\n if (child == null || typeof child === \"boolean\") {\n return \"\";\n }\n // Recursively process nested arrays (only if they don't contain React elements)\n if (Array.isArray(child)) {\n const nested = childrenToString(child);\n return nested ?? \"\";\n }\n return String(child);\n })\n .join(\"\");\n\n return stringified || null;\n }\n\n // Handle React fragments and other iterable children\n try {\n const childArray = Children.toArray(children as ReactNode);\n if (childArray.length > 0) {\n // Check if any children are React elements\n if (hasReactElements(childArray)) {\n return null;\n }\n return childrenToString(childArray);\n }\n } catch {\n // Not iterable or other error, return null\n }\n\n return null;\n}\n\n/**\n * Checks if React children can be converted to a string.\n *\n * @param children - React children to check\n * @returns True if children can be converted to string, false otherwise\n */\nexport function isStringifiableChildren(children: ReactNode): boolean {\n return childrenToString(children) !== null;\n}\n", "type": "registry:lib", "target": "@components/pitsi-ui/native-ui/src/helpers/internal/utils/children-to-string.ts" } ], "categories": [ "native", "react-native", "internal-lib" ], "meta": { "package": "@pitsi-ui/native", "packageSlug": "native-ui", "platform": "native" } }