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

Support formmethod and formaction in ViewTransitions #9084

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/new-pets-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Supports `formmethod` and `formaction` for form overrides
7 changes: 5 additions & 2 deletions packages/astro/components/ViewTransitions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ const { fallback = 'animate', handleForms } = Astro.props;
}

const form = el as HTMLFormElement;
const submitter = ev.submitter;
const formData = new FormData(form);
// Use the form action, if defined, otherwise fallback to current path.
let action = form.action ?? location.pathname;
let action = submitter?.getAttribute('formaction') ?? form.action ?? location.pathname;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let action = submitter?.getAttribute('formaction') ?? form.action ?? location.pathname;
const action = submitter?.getAttribute('formaction') ?? form.action ?? location.pathname;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

action is mutated in an if after this.

const method = submitter?.getAttribute('formmethod') ?? form.method;

const options: Options = {};
if (form.method === 'get') {
if (method === 'get') {
const params = new URLSearchParams(formData as any);
const url = new URL(action);
url.search = params.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
import Layout from '../components/Layout.astro';
let formData: FormData | undefined;
if(Astro.request.method === 'POST') {
formData = await Astro.request.formData();
}
---
<Layout>
{
Astro.request.method === 'GET' ? (
<h2>Contact Form</h2>
<form action="/contact" method="get">
<input type="hidden" name="name" value="Testing">
<button id="submit" type="submit" formmethod="post" formaction="/form-three">Submit</button>
</form>
) : (
<div id="three-result">Got: {formData?.get('name')}</div>
)
}
</Layout>
12 changes: 12 additions & 0 deletions packages/astro/e2e/view-transitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1004,4 +1004,16 @@ test.describe('View Transitions', () => {
'There should be only 1 page load. No additional loads for the form submission'
).toEqual(1);
});

test('forms are overridden by formmethod and formaction', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/form-three'));

let locator = page.locator('h2');
await expect(locator, 'should have content').toHaveText('Contact Form');

// Submit the form
await page.click('#submit');
const result = page.locator('#three-result');
await expect(result, 'should have content').toHaveText('Got: Testing');
});
});
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
},
"devDependencies": {
"@astrojs/check": "^0.1.0",
"@playwright/test": "^1.37.1",
"@playwright/test": "1.40.0-alpha-nov-13-2023",
"@types/babel__generator": "^7.6.4",
"@types/babel__traverse": "^7.20.1",
"@types/chai": "^4.3.5",
Expand Down
28 changes: 26 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.