From 43ddb5217691dc4112d8d98ae07511a8be6d4b94 Mon Sep 17 00:00:00 2001 From: Martin Trapp <94928215+martrapp@users.noreply.github.com> Date: Fri, 1 Dec 2023 18:55:22 +0100 Subject: [PATCH] Adds missing name/value of the submit button to the form data of a view transition (#9248) * Adds missing name/value of the submit button to the form data of a view transition * Update .changeset/ninety-weeks-juggle.md Co-authored-by: Sarah Rainsberger --------- Co-authored-by: Sarah Rainsberger --- .changeset/ninety-weeks-juggle.md | 5 +++++ .../astro/components/ViewTransitions.astro | 3 +-- .../src/pages/form-four.astro | 20 +++++++++++++++++++ packages/astro/e2e/view-transitions.test.js | 12 +++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 .changeset/ninety-weeks-juggle.md create mode 100644 packages/astro/e2e/fixtures/view-transitions/src/pages/form-four.astro diff --git a/.changeset/ninety-weeks-juggle.md b/.changeset/ninety-weeks-juggle.md new file mode 100644 index 000000000000..a78fda8467d1 --- /dev/null +++ b/.changeset/ninety-weeks-juggle.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Adds properties of the submit button (name, value) to the form data of a view transition diff --git a/packages/astro/components/ViewTransitions.astro b/packages/astro/components/ViewTransitions.astro index 934ae650f39a..6ce08cec9556 100644 --- a/packages/astro/components/ViewTransitions.astro +++ b/packages/astro/components/ViewTransitions.astro @@ -97,10 +97,9 @@ const { fallback = 'animate' } = Astro.props; if (el.tagName !== 'FORM' || isReloadEl(el)) { return; } - const form = el as HTMLFormElement; const submitter = ev.submitter; - const formData = new FormData(form); + const formData = new FormData(form, submitter); // Use the form action, if defined, otherwise fallback to current path. let action = submitter?.getAttribute('formaction') ?? form.action ?? location.pathname; const method = submitter?.getAttribute('formmethod') ?? form.method; diff --git a/packages/astro/e2e/fixtures/view-transitions/src/pages/form-four.astro b/packages/astro/e2e/fixtures/view-transitions/src/pages/form-four.astro new file mode 100644 index 000000000000..7a62fbe47ba0 --- /dev/null +++ b/packages/astro/e2e/fixtures/view-transitions/src/pages/form-four.astro @@ -0,0 +1,20 @@ +--- +import { ViewTransitions } from 'astro:transitions'; +--- + + + + + + + +

Voting Form

+ {Astro.url.search} +

This form does neither have a `method`, nor an `action`, nor an `input` defined

+
+ + + +
+ + diff --git a/packages/astro/e2e/view-transitions.test.js b/packages/astro/e2e/view-transitions.test.js index c56abae2423e..fafea3e697fa 100644 --- a/packages/astro/e2e/view-transitions.test.js +++ b/packages/astro/e2e/view-transitions.test.js @@ -1059,4 +1059,16 @@ test.describe('View Transitions', () => { await expect(p, 'should have content').toHaveText('Page 2'); expect(loads.length, 'There should only be 1 page load').toEqual(1); }); + + test('Submitter with a name property is included in form data', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/form-four')); + + let locator = page.locator('h2'); + await expect(locator, 'should have content').toHaveText('Voting Form'); + + // Submit the form + const expected = page.url() + '?stars=3'; + await page.click('#three'); + await expect(page).toHaveURL(expected); + }); });