Skip to content
Open
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
11 changes: 11 additions & 0 deletions packages/react-dom-bindings/src/events/SyntheticEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,17 @@ export const SyntheticPointerEvent: $FlowFixMe = createSyntheticEvent(
PointerEventInterface,
);

/**
* @interface SubmitEvent
* @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#the-submitevent-interface
*/
const SubmitEventInterface: EventInterfaceType = {
...EventInterface,
submitter: 0,
};
export const SyntheticSubmitEvent: $FlowFixMe =
createSyntheticEvent(SubmitEventInterface);

/**
* @interface TouchEvent
* @see http://www.w3.org/TR/touch-events/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
SyntheticWheelEvent,
SyntheticClipboardEvent,
SyntheticPointerEvent,
SyntheticSubmitEvent,
SyntheticToggleEvent,
} from '../../events/SyntheticEvent';

Expand Down Expand Up @@ -162,6 +163,9 @@ function extractEvents(
case 'pointerup':
SyntheticEventCtor = SyntheticPointerEvent;
break;
case 'submit':
SyntheticEventCtor = SyntheticSubmitEvent;
break;
case 'toggle':
case 'beforetoggle':
// MDN claims <details> should not receive ToggleEvent contradicting the spec: https://html.spec.whatwg.org/multipage/indices.html#event-toggle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,10 @@ describe('ReactDOMEventListener', () => {
reactEventType: 'submit',
nativeEvent: 'submit',
dispatch(node) {
const e = new Event('submit', {
const e = new SubmitEvent('submit', {
bubbles: true,
cancelable: true,
submitter: null,
});
node.dispatchEvent(e);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,36 @@ describe('SimpleEventPlugin', function () {
);
});
});

it('includes the submitter in submit events', async function () {
container = document.createElement('div');

const onSubmit = jest.fn(event => {
event.preventDefault();
});
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<form onSubmit={onSubmit}>
<button type="submit" id="submitter">
Submit
</button>
</form>,
);
});

const submitter = container.querySelector('#submitter');
const submitEvent = new SubmitEvent('submit', {
bubbles: true,
cancelable: true,
submitter: submitter,
});
await act(() => {
submitter.dispatchEvent(submitEvent);
});

expect(onSubmit).toHaveBeenCalledTimes(1);
const event = onSubmit.mock.calls[0][0];
expect(event.submitter).toBe(submitter);
});
});
Loading