Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: forwardRef from SideNavigationLink component #1123

Merged
merged 1 commit into from
Sep 6, 2024

Conversation

aaryanporwal
Copy link
Member

@aaryanporwal aaryanporwal commented Sep 2, 2024

Done

Percy steps

  • No visual changes expected

@webteam-app
Copy link

Copy link
Collaborator

@huwshimi huwshimi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like most of the changes need to be reverted from the previous PR: https://github.com/canonical/react-components/pull/1118/files as removing the generic types has broken then ability to handle props when passing component.

For example when using this with a Link from React Router it would give an error for the to prop here as it can't infer the props of the Link component:

<SideNavigationLink label="docs" component={Link} to="/docs" />

Unfortunately forwardRef does not support components with generic types, so instead you have to manually pass a ref. We do that in a few places e.g.

forwardRef?: React.Ref<HTMLDivElement> | null;

This issue could be caught in the future if we change this test:

To:

it("can use a custom link component", () => {
  const Link = ({ to }: { to: () => void }) => (
    <button onClick={to}>link</button>
  );
  const label = "Test content";
  render(<SideNavigationLink label={label} component={Link} to={jest.fn()} />);
  expect(screen.getByRole("button", { name: label })).toHaveClass(
    "p-side-navigation__link",
  );
});

@aaryanporwal
Copy link
Member Author

aaryanporwal commented Sep 5, 2024

Unfortunately forwardRef does not support components with generic types, so instead you have to manually pass a ref. We do that in a few places e.g.

...

Thank you Huw for pointing that out! I've updated the code and the test like you suggested. Can you please take a look again?

Copy link
Collaborator

@huwshimi huwshimi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaryanporwal aaryanporwal force-pushed the forwardRef-SideNavLink branch 3 times, most recently from 4ee5aac to 7ed1fa0 Compare September 6, 2024 03:53
Copy link
Collaborator

@huwshimi huwshimi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this fix!

@aaryanporwal aaryanporwal merged commit c971b10 into canonical:main Sep 6, 2024
11 of 12 checks passed
Copy link

github-actions bot commented Sep 6, 2024

🎉 This PR is included in version 1.2.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

forwardRef error
3 participants