Skip to content

Commit 29d3c83

Browse files
authored
ReactDOM: Fix missing form data when the submitter is outside the form (#28056)
1 parent 4508873 commit 29d3c83

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

packages/react-dom-bindings/src/events/plugins/FormActionEventPlugin.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ function extractEvents(
9292
const temp = submitter.ownerDocument.createElement('input');
9393
temp.name = submitter.name;
9494
temp.value = submitter.value;
95+
if (form.id) {
96+
temp.setAttribute('form', form.id);
97+
}
9598
(submitter.parentNode: any).insertBefore(temp, submitter);
9699
formData = new FormData(form);
97100
(temp.parentNode: any).removeChild(temp);

packages/react-dom/src/__tests__/ReactDOMForm-test.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ describe('ReactDOMForm', () => {
481481
it('can read the clicked button in the formdata event', async () => {
482482
const inputRef = React.createRef();
483483
const buttonRef = React.createRef();
484+
const outsideButtonRef = React.createRef();
484485
let button;
485486
let title;
486487

@@ -492,14 +493,27 @@ describe('ReactDOMForm', () => {
492493
const root = ReactDOMClient.createRoot(container);
493494
await act(async () => {
494495
root.render(
495-
<form action={action}>
496-
<input type="text" name="title" defaultValue="hello" />
497-
<input type="submit" name="button" value="save" />
498-
<input type="submit" name="button" value="delete" ref={inputRef} />
499-
<button name="button" value="edit" ref={buttonRef}>
500-
Edit
496+
<>
497+
<form action={action}>
498+
<input type="text" name="title" defaultValue="hello" />
499+
<input type="submit" name="button" value="save" />
500+
<input type="submit" name="button" value="delete" ref={inputRef} />
501+
<button name="button" value="edit" ref={buttonRef}>
502+
Edit
503+
</button>
504+
</form>
505+
<form id="form" action={action}>
506+
<input type="text" name="title" defaultValue="hello" />
507+
</form>
508+
<button
509+
form="form"
510+
name="button"
511+
value="outside"
512+
ref={outsideButtonRef}>
513+
Button outside form
501514
</button>
502-
</form>,
515+
,
516+
</>,
503517
);
504518
});
505519

@@ -520,6 +534,11 @@ describe('ReactDOMForm', () => {
520534
expect(button).toBe('edit');
521535
expect(title).toBe('hello');
522536

537+
await submit(outsideButtonRef.current);
538+
539+
expect(button).toBe('outside');
540+
expect(title).toBe('hello');
541+
523542
// Ensure that the type field got correctly restored
524543
expect(inputRef.current.getAttribute('type')).toBe('submit');
525544
expect(buttonRef.current.getAttribute('type')).toBe(null);

0 commit comments

Comments
 (0)