Skip to content

Commit

Permalink
Use props' default children if no children are set in the component e…
Browse files Browse the repository at this point in the history
…lement's children and when asChild is true. (#435)

Co-authored-by: Maayan <[email protected]>
  • Loading branch information
psd-coder and 0xmaayan authored Oct 28, 2024
1 parent 518bc43 commit b6959cc
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/wallet-adapter-react/src/components/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Slot } from "@radix-ui/react-slot";
import { ReactNode, forwardRef } from "react";
import { ReactNode, cloneElement, forwardRef, isValidElement } from "react";

export interface HeadlessComponentProps {
/** A class name for styling the element. */
Expand Down Expand Up @@ -38,7 +38,14 @@ export function createHeadlessComponent<
const Component = asChild ? Slot : elementType;

const { children: defaultChildren, ...resolvedProps } =
typeof props === "function" ? props(displayName) : (props ?? {});
typeof props === "function" ? props(displayName) : props ?? {};
const resolvedChildren =
/**
* Use props' default children if no children are set in the component element's children and when asChild is true.
*/
asChild && isValidElement(children) && !children.props.children
? cloneElement(children, {}, defaultChildren)
: (children ?? defaultChildren);

return (
/**
Expand All @@ -53,7 +60,7 @@ export function createHeadlessComponent<
*/
// @ts-expect-error
<Component ref={ref} className={className} {...resolvedProps}>
{children ?? defaultChildren}
{resolvedChildren}
</Component>
);
});
Expand Down

0 comments on commit b6959cc

Please sign in to comment.