From 3b764732b88428b501968c2a8a38a03edf916161 Mon Sep 17 00:00:00 2001 From: Aaryan Porwal Date: Fri, 6 Sep 2024 08:31:21 +0530 Subject: [PATCH] fix: forwardRef from SideNavigationLink component --- .../SideNavigationItem/SideNavigationItem.tsx | 2 +- .../SideNavigationLink.test.tsx | 11 ++--- .../SideNavigationLink/SideNavigationLink.tsx | 44 +++++++++++-------- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/components/SideNavigation/SideNavigationItem/SideNavigationItem.tsx b/src/components/SideNavigation/SideNavigationItem/SideNavigationItem.tsx index 2222bda7..259bfe81 100644 --- a/src/components/SideNavigation/SideNavigationItem/SideNavigationItem.tsx +++ b/src/components/SideNavigation/SideNavigationItem/SideNavigationItem.tsx @@ -37,7 +37,7 @@ const SideNavigationItem = ( const { nonInteractive: _, ...textProps } = props; content = ; } else if (!("children" in props)) { - content = ; + content = {...props} />; } else { ({ children: content, ...liProps } = props); } diff --git a/src/components/SideNavigation/SideNavigationLink/SideNavigationLink.test.tsx b/src/components/SideNavigation/SideNavigationLink/SideNavigationLink.test.tsx index 8990982b..c6b20637 100644 --- a/src/components/SideNavigation/SideNavigationLink/SideNavigationLink.test.tsx +++ b/src/components/SideNavigation/SideNavigationLink/SideNavigationLink.test.tsx @@ -1,6 +1,5 @@ import React from "react"; import { render, screen } from "@testing-library/react"; -import type { ButtonHTMLAttributes } from "react"; import SideNavigationLink from "./SideNavigationLink"; @@ -32,11 +31,13 @@ it("displays a dark icon", () => { }); it("can use a custom link component", () => { - const Link = ({ ...props }: ButtonHTMLAttributes) => ( - + ); + render(); expect(screen.getByRole("button", { name: label })).toHaveClass( "p-side-navigation__link", ); diff --git a/src/components/SideNavigation/SideNavigationLink/SideNavigationLink.tsx b/src/components/SideNavigation/SideNavigationLink/SideNavigationLink.tsx index 15493174..0b6c3734 100644 --- a/src/components/SideNavigation/SideNavigationLink/SideNavigationLink.tsx +++ b/src/components/SideNavigation/SideNavigationLink/SideNavigationLink.tsx @@ -1,4 +1,4 @@ -import React, { forwardRef } from "react"; +import React from "react"; import classNames from "classnames"; import type { HTMLProps } from "react"; @@ -7,7 +7,7 @@ import SideNavigationBase from "../SideNavigationBase"; export type LinkDefaultElement = HTMLProps; -export type Props = Omit< +export type Props = Omit< SideNavigationBaseProps, "component" > & { @@ -16,22 +16,30 @@ export type Props = Omit< * @default a */ component?: SideNavigationBaseProps["component"]; + /** + * A ref to pass to the link element. + */ + forwardRef?: React.Ref | null; +}; + +const SideNavigationLink = ({ + component, + forwardRef, + ...props +}: Props) => { + let className: string | null = null; + if ("className" in props && typeof props.className === "string") { + className = props.className; + delete props.className; + } + return ( + + ); }; -const SideNavigationLink = forwardRef( - ({ component, ...props }: Props) => { - let className: string | null = null; - if ("className" in props && typeof props.className === "string") { - className = props.className; - delete props.className; - } - return ( - - ); - }, -); export default SideNavigationLink;