Skip to content

Commit

Permalink
Revert "Ensure Form contains splat portion of pathname when no action…
Browse files Browse the repository at this point in the history
… is specified (#10933)" (#10965)

This reverts commit 908a40a.
  • Loading branch information
brophdawg11 authored Oct 26, 2023
1 parent 677d6c8 commit 805924d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 52 deletions.
5 changes: 0 additions & 5 deletions .changeset/splat-form-action.md

This file was deleted.

40 changes: 1 addition & 39 deletions packages/react-router-dom/__tests__/data-browser-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2894,7 +2894,7 @@ function testDomRouter(
let { container } = render(<RouterProvider router={router} />);

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

Expand Down Expand Up @@ -2937,44 +2937,6 @@ function testDomRouter(
"/foo"
);
});

it("includes splat portion of path when no action is specified (inline splat)", async () => {
let router = createTestRouter(
createRoutesFromElements(
<Route path="/">
<Route path="foo">
<Route path="*" element={<NoActionComponent />} />
</Route>
</Route>
),
{
window: getWindow("/foo/bar/baz"),
}
);
let { container } = render(<RouterProvider router={router} />);

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

it("includes splat portion of path when no action is specified (nested splat)", async () => {
let router = createTestRouter(
createRoutesFromElements(
<Route path="/">
<Route path="foo/*" element={<NoActionComponent />} />
</Route>
),
{
window: getWindow("/foo/bar/baz"),
}
);
let { container } = render(<RouterProvider router={router} />);

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

it("allows user to specify search params and hash", async () => {
Expand Down
14 changes: 6 additions & 8 deletions packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1457,20 +1457,18 @@ export function useFormAction(
let { basename } = React.useContext(NavigationContext);
let routeContext = React.useContext(RouteContext);
invariant(routeContext, "useFormAction must be used inside a RouteContext");
let location = useLocation();

let [match] = routeContext.matches.slice(-1);
// Shallow clone path so we can modify it below, otherwise we modify the
// object referenced by useMemo inside useResolvedPath
let path = {
...useResolvedPath(action != null ? action : location.pathname, {
relative,
}),
};
let path = { ...useResolvedPath(action ? action : ".", { relative }) };

// If no action was specified, browsers will persist current search params
// when determining the path, so match that behavior
// Previously we set the default action to ".". The problem with this is that
// `useResolvedPath(".")` excludes search params of the resolved URL. This is
// the intended behavior of when "." is specifically provided as
// the form action, but inconsistent w/ browsers when the action is omitted.
// https://github.com/remix-run/remix/issues/927
let location = useLocation();
if (action == null) {
// Safe to write to this directly here since if action was undefined, we
// would have called useResolvedPath(".") which will never include a search
Expand Down

0 comments on commit 805924d

Please sign in to comment.