( props: RenderPropsHookOptions<T, E> )
| 233 | } |
| 234 | |
| 235 | export function useRenderProps<T, E extends keyof React.JSX.IntrinsicElements>( |
| 236 | props: RenderPropsHookOptions<T, E> |
| 237 | ): RenderPropsHookRetVal<T, E> { |
| 238 | let { |
| 239 | className, |
| 240 | style, |
| 241 | children, |
| 242 | defaultClassName = undefined, |
| 243 | defaultChildren = undefined, |
| 244 | defaultStyle, |
| 245 | values, |
| 246 | render |
| 247 | } = props; |
| 248 | |
| 249 | return useMemo(() => { |
| 250 | let computedClassName: string | undefined; |
| 251 | let computedStyle: React.CSSProperties | undefined; |
| 252 | let computedChildren: React.ReactNode | undefined; |
| 253 | |
| 254 | if (typeof className === 'function') { |
| 255 | computedClassName = className({...values, defaultClassName}); |
| 256 | } else { |
| 257 | computedClassName = className; |
| 258 | } |
| 259 | |
| 260 | if (typeof style === 'function') { |
| 261 | computedStyle = style({...values, defaultStyle: defaultStyle || {}}); |
| 262 | } else { |
| 263 | computedStyle = style; |
| 264 | } |
| 265 | |
| 266 | if (typeof children === 'function') { |
| 267 | computedChildren = children({...values, defaultChildren}); |
| 268 | } else if (children == null) { |
| 269 | computedChildren = defaultChildren; |
| 270 | } else { |
| 271 | computedChildren = children; |
| 272 | } |
| 273 | |
| 274 | return { |
| 275 | className: computedClassName ?? defaultClassName, |
| 276 | style: computedStyle || defaultStyle ? {...defaultStyle, ...computedStyle} : undefined, |
| 277 | children: computedChildren ?? defaultChildren, |
| 278 | 'data-rac': '', |
| 279 | render: render ? props => render(props, values) : undefined |
| 280 | }; |
| 281 | }, [className, style, children, defaultClassName, defaultChildren, defaultStyle, values, render]); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * A helper function that accepts a user-provided render prop value (either a static value or a |
no test coverage detected