import * as React from 'react' import { Props } from '../types' export function render( props: Props, bag: TBag, tag: React.ElementType ) { const { as: Component = tag, children, ...passThroughProps } = props const resolvedChildren = (typeof children === 'function' ? children(bag) : children) as | React.ReactElement | React.ReactElement[] if (Component === React.Fragment) { if (Object.keys(passThroughProps).length > 0) { if (Array.isArray(resolvedChildren) && resolvedChildren.length > 1) { const err = new Error('You should only render 1 child') if (Error.captureStackTrace) Error.captureStackTrace(err, render) throw err } if (!React.isValidElement(resolvedChildren)) { const err = new Error( `You should render an element as a child. Did you forget the as="..." prop?` ) if (Error.captureStackTrace) Error.captureStackTrace(err, render) throw err } return React.cloneElement( resolvedChildren, // Filter out undefined values so that they don't override the existing values mergeEventFunctions(compact(passThroughProps), resolvedChildren.props, ['onClick']) ) } } return React.createElement(Component, passThroughProps, resolvedChildren) } /** * We can use this function for the following useCase: * *