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: Modify return type of className of NavLinkProps interface #8542

Merged
merged 2 commits into from Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.yml
Expand Up @@ -24,3 +24,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