From 34f994b6111bb898fc9239600caa5635f68386df Mon Sep 17 00:00:00 2001 From: Navin Moorthy Date: Fri, 10 Jun 2022 20:21:40 +0530 Subject: [PATCH] =?UTF-8?q?fix(react-utils):=20=F0=9F=A9=B9=20types=20for?= =?UTF-8?q?=20passprops=20for=20render=20props?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/react.ts | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/utils/react.ts b/src/utils/react.ts index adc85e32..5e2c4d78 100644 --- a/src/utils/react.ts +++ b/src/utils/react.ts @@ -1,5 +1,5 @@ import * as React from "react"; -import { AnyObject } from "ariakit-utils"; +import { AnyObject, Options, Props } from "ariakit-utils"; import { isFunction } from "./assertions"; import { cx } from "./tailwindMerge"; @@ -111,3 +111,37 @@ export const getComponentProps = ( return { componentProps, finalChildren }; }; + +export function runRenderFn> = AnyObject>( + component: RenderProp, + props: T, +): React.ReactNode { + return isFunction(component) ? component(props) : component; +} + +// Merge library & user prop +export const passPropsNew = > = AnyObject>( + component: RenderProp, + props: T, +) => { + return React.isValidElement(component) + ? React.cloneElement(component, { + ...props, + ...component.props, + className: cx(props?.className, component.props.className), + }) + : runRenderFn(component, props); +}; + +// Add a11y to the icon passed +export const withIconA11yNew = > = AnyObject>( + icon: RenderProp, + props: T, +) => { + return passProps(icon, { + role: "img", + focusable: false, + "aria-hidden": true, + ...props, + }); +};