Skip to content

Commit

Permalink
Add the submitter value with name to formData for use:enhance
Browse files Browse the repository at this point in the history
  • Loading branch information
surefire committed Sep 24, 2022
1 parent 6c02f55 commit 2956322
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/kit/src/runtime/app/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export function enhance(form, submit = () => {}) {
);

const data = new FormData(form);

const submitter_name = event.submitter?.getAttribute('name');
if (submitter_name) {
data.append(submitter_name, event.submitter?.getAttribute('value') ?? '');
}

const controller = new AbortController();

let cancelled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@ export const actions = {
},
slow: async () => {
await new Promise((resolve) => setTimeout(resolve, 500));
},
submitter: async ({ request }) => {
const fields = await request.formData();
return {
result: 'submitter: ' + fields.get('submitter')
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<input name="username" type="text" />
<button class="form1">Submit</button>
<button class="form1-register" formAction="?/register">Submit</button>
<button class="form1-submitter" formAction="?/submitter" name="submitter" value="foo">Submit</button>
</form>

<span class="count">{count}</span>
Expand Down
15 changes: 15 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,21 @@ test.describe('Actions', () => {
);
});

test('use:enhance button with name', async ({ page, app }) => {
await page.goto('/actions/enhance');

expect(await page.textContent('pre.formdata1')).toBe(JSON.stringify(null));

await Promise.all([
page.waitForRequest((request) => request.url().includes('/actions/enhance')),
page.click('button.form1-submitter')
]);

await expect(page.locator('pre.formdata1')).toHaveText(
JSON.stringify({ result: 'submitter: foo' })
);
});

test('redirect', async ({ page }) => {
await page.goto('/actions/redirect');

Expand Down

0 comments on commit 2956322

Please sign in to comment.