Skip to content

Commit

Permalink
fix(react-utils): 🩹 types for passprops for render props
Browse files Browse the repository at this point in the history
  • Loading branch information
navin-moorthy committed Jun 13, 2022
1 parent e87e010 commit 34f994b
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/utils/react.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -111,3 +111,37 @@ export const getComponentProps = <T extends any, P>(

return { componentProps, finalChildren };
};

export function runRenderFn<T extends Props<Options<"div">> = AnyObject>(
component: RenderProp<T>,
props: T,
): React.ReactNode {
return isFunction(component) ? component(props) : component;
}

// Merge library & user prop
export const passPropsNew = <T extends Props<Options<"div">> = AnyObject>(
component: RenderProp<T>,
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 = <T extends Props<Options<"div">> = AnyObject>(
icon: RenderProp<T>,
props: T,
) => {
return passProps(icon, {
role: "img",
focusable: false,
"aria-hidden": true,
...props,
});
};

0 comments on commit 34f994b

Please sign in to comment.