Skip to content

Commit 35ff025

Browse files
fix: respect relative prop in NavLink for isActive (#9453)
* fix: respect relative prop in NavLink for isActive (#9451) Co-authored-by: Mikaël ANZANO <[email protected]> * fix: add unit test for NavLink relative=path * add changeset Co-authored-by: Mikaël Anzano <[email protected]> Co-authored-by: Mikaël ANZANO <[email protected]>
1 parent 9cf02f3 commit 35ff025

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

.changeset/fluffy-buttons-push.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router-dom": patch
3+
---
4+
5+
Respect relative=path prop on NavLink

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
- loun4
7474
- lqze
7575
- lukerSpringTree
76+
- manzano78
7677
- marc2332
7778
- markivancho
7879
- marvinruder

packages/react-router-dom/__tests__/nav-link-active-test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,33 @@ describe("NavLink", () => {
394394
});
395395
});
396396
});
397+
398+
describe("when it matches with relative=path links", () => {
399+
it("applies the default 'active' className to the underlying <a>", () => {
400+
let renderer: TestRenderer.ReactTestRenderer;
401+
TestRenderer.act(() => {
402+
renderer = TestRenderer.create(
403+
<MemoryRouter initialEntries={["/contacts/1"]}>
404+
<Routes>
405+
<Route
406+
path="contacts/:id"
407+
element={
408+
<NavLink to="../1" relative="path">
409+
Link
410+
</NavLink>
411+
}
412+
/>
413+
</Routes>
414+
</MemoryRouter>
415+
);
416+
});
417+
418+
let anchor = renderer.root.findByType("a");
419+
420+
expect(anchor.props.href).toEqual("/contacts/1");
421+
expect(anchor.props.className).toEqual("active");
422+
});
423+
});
397424
});
398425

399426
describe("NavLink using a data router", () => {

packages/react-router-dom/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ export const NavLink = React.forwardRef<HTMLAnchorElement, NavLinkProps>(
441441
},
442442
ref
443443
) {
444-
let path = useResolvedPath(to);
444+
let path = useResolvedPath(to, { relative: rest.relative });
445445
let match = useMatch({ path: path.pathname, end, caseSensitive });
446446

447447
let routerState = React.useContext(DataRouterStateContext);

0 commit comments

Comments
 (0)