Skip to content

Commit

Permalink
Fix useFormAction inheriting ?index param from child route action sub…
Browse files Browse the repository at this point in the history
…mission (#11025)
  • Loading branch information
brophdawg11 authored Nov 14, 2023
1 parent 39854ce commit 5563672
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/form-action-index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router-dom": patch
---

Fix `useFormAction` which was incorrectly inheriting the `?index` query param from child route `action` submissions
22 changes: 22 additions & 0 deletions packages/react-router-dom/__tests__/data-browser-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2698,6 +2698,28 @@ function testDomRouter(
"/foo"
);
});

it("does not include the index parameter if we've submitted to a child index route", async () => {
let router = createTestRouter(
createRoutesFromElements(
<Route path="/">
<Route path="foo">
<Route path="bar" element={<NoActionComponent />}>
<Route index={true} element={<h1>Index</h1>} />
</Route>
</Route>
</Route>
),
{
window: getWindow("/foo/bar?index&a=1"),
}
);
let { container } = render(<RouterProvider router={router} />);

expect(container.querySelector("form")?.getAttribute("action")).toBe(
"/foo/bar?a=1"
);
});
});

describe("index routes", () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1551,11 +1551,11 @@ export function useFormAction(
// would have called useResolvedPath(".") which will never include a search
path.search = location.search;

// When grabbing search params from the URL, remove the automatically
// inserted ?index param so we match the useResolvedPath search behavior
// which would not include ?index
if (match.route.index) {
let params = new URLSearchParams(path.search);
// When grabbing search params from the URL, remove any included ?index param
// since it might not apply to our contextual route. We add it back based
// on match.route.index below
let params = new URLSearchParams(path.search);
if (params.has("index") && params.get("index") === "") {
params.delete("index");
path.search = params.toString() ? `?${params.toString()}` : "";
}
Expand Down

0 comments on commit 5563672

Please sign in to comment.