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: preserve search params on unspecified <Form> action #9060

Merged
merged 7 commits into from Aug 10, 2022

Conversation

brophdawg11
Copy link
Contributor

React-router implementation of remix-run/remix#3697

End state is the behavior specified in @ryanflorence's comment:

element preserves search
<Form method="get" action=".">
<Form method="get">
<form method="post" action=".">
<form method="post">

@changeset-bot
Copy link

changeset-bot bot commented Jul 14, 2022

🦋 Changeset detected

Latest commit: 4e5925f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
react-router-dom Patch
react-router Patch
react-router-dom-v5-compat Patch
react-router-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

}

expect(container.querySelector("form")?.getAttribute("action")).toBe(
"/?a=1#hash"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserves everything if no action is specified

);
}

expect(container.querySelector("form")?.getAttribute("action")).toBe("/");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But only keeps pathname for action="."

@@ -3320,7 +3320,7 @@ describe("a router", () => {
expect(t.router.state.navigation.formData).toBeUndefined();
});

it("merges URLSearchParams for formMethod=get", async () => {
it("does not preserve existing 'action' URLSearchParams for formMethod='get'", async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a previous misunderstanding on my part - browser do indeed blow away query params on GET submissions. I thought it would just append them

Copy link
Collaborator

@chaance chaance left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick gut check question here as our implementations look a bit different. Let me know what you think.

let [match] = routeContext.matches.slice(-1);
let { pathname, search } = useResolvedPath(action);
let path = useResolvedPath(action || location);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should still resolve the path based on "." if the action is undefined, no? Does that make a difference here? My reading of resolveTo makes me believe that it would if we were in a nested route situation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that puts us back in the same scenario though where we lose search params? Down inside useResolvedPath -> resolveTo -> parsePath we end up with a Path of { pathname: '.' } without any search or hash. By passing the current location when no action is specified we preserve those throughout?

Just to be safe, I added a few tests for the nested route scenario to confirm they're handled - and they break if we change to let path = useResolvedPath(action || '.')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants