Skip to content

Commit

Permalink
fix: forwardRef from SideNavigationLink component
Browse files Browse the repository at this point in the history
  • Loading branch information
aaryanporwal committed Sep 5, 2024
1 parent 63c11c9 commit a0e8fd4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ it("displays a dark icon", () => {
});

it("can use a custom link component", () => {
const Link = ({ ...props }: ButtonHTMLAttributes<HTMLButtonElement>) => (
<button {...props} />
const Link = ({ to }: { to: () => void }) => (
<button onClick={to}>link</button>
);
const label = "Test content";
render(<SideNavigationLink label={label} component={Link} />);
render(<SideNavigationLink label={label} component={Link} to={jest.fn()} />);
expect(screen.getByRole("button", { name: label })).toHaveClass(
"p-side-navigation__link",
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,30 @@ export type Props<L = LinkDefaultElement> = Omit<
* @default a
*/
component?: SideNavigationBaseProps<L>["component"];
/**
* A ref to pass to the link element.
*/
forwardRef?: React.Ref<HTMLAnchorElement> | null;
};

const SideNavigationLink = <L = LinkDefaultElement,>({
component,
forwardRef,
...props
}: Props<L>) => {
let className: string | null = null;
if ("className" in props && typeof props.className === "string") {
className = props.className;
delete props.className;
}
return (
<SideNavigationBase
className={classNames("p-side-navigation__link", className)}
component={component ?? "a"}
{...props}
ref={forwardRef}
/>
);
};
const SideNavigationLink = forwardRef<HTMLAnchorElement, Props>(
({ component, ...props }: Props) => {
let className: string | null = null;
if ("className" in props && typeof props.className === "string") {
className = props.className;
delete props.className;
}
return (
<SideNavigationBase
className={classNames("p-side-navigation__link", className)}
component={component ?? "a"}
{...props}
/>
);
},
);

export default SideNavigationLink;

0 comments on commit a0e8fd4

Please sign in to comment.