( props: RenderPropsHookOptions<T, E> )
| 218 | } |
| 219 | |
| 220 | export function useRenderProps<T, E extends keyof React.JSX.IntrinsicElements>( |
| 221 | props: RenderPropsHookOptions<T, E> |
| 222 | ): RenderPropsHookRetVal<T, E> { |
| 223 | let { |
| 224 | className, |
| 225 | style, |
| 226 | children, |
| 227 | defaultClassName = undefined, |
| 228 | defaultChildren = undefined, |
| 229 | defaultStyle, |
| 230 | values, |
| 231 | render |
| 232 | } = props; |
| 233 | |
| 234 | return useMemo(() => { |
| 235 | let computedClassName: string | undefined; |
| 236 | let computedStyle: React.CSSProperties | undefined; |
| 237 | let computedChildren: React.ReactNode | undefined; |
| 238 | |
| 239 | if (typeof className === 'function') { |
| 240 | computedClassName = className({...values, defaultClassName}); |
| 241 | } else { |
| 242 | computedClassName = className; |
| 243 | } |
| 244 | |
| 245 | if (typeof style === 'function') { |
| 246 | computedStyle = style({...values, defaultStyle: defaultStyle || {}}); |
| 247 | } else { |
| 248 | computedStyle = style; |
| 249 | } |
| 250 | |
| 251 | if (typeof children === 'function') { |
| 252 | computedChildren = children({...values, defaultChildren}); |
| 253 | } else if (children == null) { |
| 254 | computedChildren = defaultChildren; |
| 255 | } else { |
| 256 | computedChildren = children; |
| 257 | } |
| 258 | |
| 259 | return { |
| 260 | className: computedClassName ?? defaultClassName, |
| 261 | style: computedStyle || defaultStyle ? {...defaultStyle, ...computedStyle} : undefined, |
| 262 | children: computedChildren ?? defaultChildren, |
| 263 | 'data-rac': '', |
| 264 | render: render ? props => render(props, values) : undefined |
| 265 | }; |
| 266 | }, [className, style, children, defaultClassName, defaultChildren, defaultStyle, values, render]); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * A helper function that accepts a user-provided render prop value (either a static value or a |
no test coverage detected