Skip to content

Commit

Permalink
fix: respect relative prop in NavLink for isActive (#9453)
Browse files Browse the repository at this point in the history
* fix: respect relative prop in NavLink for isActive (#9451)

Co-authored-by: Mikaël ANZANO <m.anzano@meetic-corp.com>

* fix: add unit test for NavLink relative=path

* add changeset

Co-authored-by: Mikaël Anzano <mikael.anzano@gmail.com>
Co-authored-by: Mikaël ANZANO <m.anzano@meetic-corp.com>
  • Loading branch information
3 people authored Oct 17, 2022
1 parent 9cf02f3 commit 35ff025
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fluffy-buttons-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router-dom": patch
---

Respect relative=path prop on NavLink
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 35ff025

Please sign in to comment.