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: respect relative prop in NavLink for isActive #9453

Merged
merged 3 commits into from Oct 17, 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
5 changes: 5 additions & 0 deletions .changeset/fluffy-buttons-push.md
@@ -0,0 +1,5 @@
---
"react-router-dom": patch
---

Respect relative=path prop on NavLink
1 change: 1 addition & 0 deletions contributors.yml
Expand Up @@ -72,6 +72,7 @@
- loun4
- lqze
- lukerSpringTree
- manzano78
- marc2332
- markivancho
- marvinruder
Expand Down
27 changes: 27 additions & 0 deletions packages/react-router-dom/__tests__/nav-link-active-test.tsx
Expand Up @@ -394,6 +394,33 @@ describe("NavLink", () => {
});
});
});

describe("when it matches with relative=path links", () => {
it("applies the default 'active' className to the underlying <a>", () => {
let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/contacts/1"]}>
<Routes>
<Route
path="contacts/:id"
element={
<NavLink to="../1" relative="path">
Link
</NavLink>
}
/>
</Routes>
</MemoryRouter>
);
});

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

expect(anchor.props.href).toEqual("/contacts/1");
expect(anchor.props.className).toEqual("active");
});
});
});

describe("NavLink using a data router", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router-dom/index.tsx
Expand Up @@ -441,7 +441,7 @@ export const NavLink = React.forwardRef<HTMLAnchorElement, NavLinkProps>(
},
ref
) {
let path = useResolvedPath(to);
let path = useResolvedPath(to, { relative: rest.relative });
let match = useMatch({ path: path.pathname, end, caseSensitive });

let routerState = React.useContext(DataRouterStateContext);
Expand Down