Skip to content

Commit

Permalink
fix: Modify return type of className of NavLinkProps interface (#8542)
Browse files Browse the repository at this point in the history
`className` may be undefined
  • Loading branch information
koojaa committed Jan 4, 2022
1 parent 02fcd50 commit 9bdf8ae
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions contributors.yml
Expand Up @@ -25,3 +25,4 @@
- turansky
- underager
- vijaypushkin
- koojaa
2 changes: 1 addition & 1 deletion docs/api.md
Expand Up @@ -355,7 +355,7 @@ interface NavLinkProps
| ((props: { isActive: boolean }) => React.ReactNode);
className?:
| string
| ((props: { isActive: boolean }) => string);
| ((props: { isActive: boolean }) => string | undefined);
end?: boolean;
style?:
| React.CSSProperties
Expand Down
29 changes: 29 additions & 0 deletions packages/react-router-dom/__tests__/nav-link-active-test.tsx
Expand Up @@ -44,6 +44,35 @@ describe("NavLink", () => {

expect(anchor.children[0]).toMatch("Somewhere else");
});

it("applies an 'undefined' className to the underlying <a>", () => {
let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/home"]}>
<Routes>
<Route
path="/home"
element={
<NavLink
to="somewhere-else"
className={({ isActive }) =>
isActive ? "some-active-classname" : undefined
}
>
Home
</NavLink>
}
/>
</Routes>
</MemoryRouter>
);
});

let anchor = renderer.root.findByType("a");

expect(anchor.props.className).toBeUndefined();
});
});

describe("when it matches to the end", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-router-dom/index.tsx
Expand Up @@ -287,7 +287,7 @@ export interface NavLinkProps
| React.ReactNode
| ((props: { isActive: boolean }) => React.ReactNode);
caseSensitive?: boolean;
className?: string | ((props: { isActive: boolean }) => string);
className?: string | ((props: { isActive: boolean }) => string | undefined);
end?: boolean;
style?:
| React.CSSProperties
Expand Down Expand Up @@ -329,7 +329,7 @@ export const NavLink = React.forwardRef<HTMLAnchorElement, NavLinkProps>(

let ariaCurrent = isActive ? ariaCurrentProp : undefined;

let className: string;
let className: string | undefined;
if (typeof classNameProp === "function") {
className = classNameProp({ isActive });
} else {
Expand Down

0 comments on commit 9bdf8ae

Please sign in to comment.