( props: RenderableProps<T> & Partial<T> & Record<string, any>, lazyProps: Record<string, any>, name: string, )
| 4 | // shared logic between components that use either render prop, |
| 5 | // children render function, or component prop |
| 6 | export default function renderComponent<T>( |
| 7 | props: RenderableProps<T> & Partial<T> & Record<string, any>, |
| 8 | lazyProps: Record<string, any>, |
| 9 | name: string, |
| 10 | ): React.ReactNode { |
| 11 | const { render, children, component, ...rest } = props; |
| 12 | if (component) { |
| 13 | // FIX: Don't use Object.assign which tries to overwrite getters |
| 14 | // Instead, create a new object with lazyProps descriptors first, |
| 15 | // then add non-conflicting properties from rest |
| 16 | const result = {} as any; |
| 17 | Object.defineProperties(result, Object.getOwnPropertyDescriptors(lazyProps)); |
| 18 | const restDescriptors = Object.getOwnPropertyDescriptors(rest); |
| 19 | for (const key in restDescriptors) { |
| 20 | if (!(key in result)) { |
| 21 | Object.defineProperty(result, key, restDescriptors[key]); |
| 22 | } |
| 23 | } |
| 24 | result.children = children; |
| 25 | result.render = render; |
| 26 | return React.createElement(component, result); |
| 27 | } |
| 28 | if (render) { |
| 29 | const result = {} as T; |
| 30 | Object.defineProperties( |
| 31 | result, |
| 32 | Object.getOwnPropertyDescriptors(lazyProps), |
| 33 | ); |
| 34 | // Only add properties from rest that don't already exist |
| 35 | const restDescriptors = Object.getOwnPropertyDescriptors(rest); |
| 36 | for (const key in restDescriptors) { |
| 37 | if (!(key in (result as any))) { |
| 38 | Object.defineProperty(result as any, key, restDescriptors[key]); |
| 39 | } |
| 40 | } |
| 41 | if (children !== undefined) { |
| 42 | (result as any).children = children; |
| 43 | } |
| 44 | return render(result); |
| 45 | } |
| 46 | if (typeof children !== "function") { |
| 47 | throw new Error( |
| 48 | `Must specify either a render prop, a render function as children, or a component prop to ${name}`, |
| 49 | ); |
| 50 | } |
| 51 | const result = {} as T; |
| 52 | Object.defineProperties(result, Object.getOwnPropertyDescriptors(lazyProps)); |
| 53 | // Only add properties from rest that don't already exist |
| 54 | const restDescriptors = Object.getOwnPropertyDescriptors(rest); |
| 55 | for (const key in restDescriptors) { |
| 56 | if (!(key in (result as any))) { |
| 57 | Object.defineProperty(result as any, key, restDescriptors[key]); |
| 58 | } |
| 59 | } |
| 60 | return children(result); |
| 61 | } |
no test coverage detected
searching dependent graphs…