Skip to content

Commit 59038c0

Browse files
travisdahlepicfaace
authored andcommittedFeb 8, 2019
Submit event should return original event as second param (#1172)
1 parent 919a164 commit 59038c0

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed
 

‎docs/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ render((
107107

108108
#### Form submission
109109

110-
You can pass a function as the `onSubmit` prop of your `Form` component to listen to when the form is submitted and its data are valid. It will be passed a result object having a `formData` attribute, which is the valid form data you're usually after:
110+
You can pass a function as the `onSubmit` prop of your `Form` component to listen to when the form is submitted and its data are valid. It will be passed a result object having a `formData` attribute, which is the valid form data you're usually after. The original event will also be passed as a second parameter:
111111

112112
```js
113-
const onSubmit = ({formData}) => console.log("Data submitted: ", formData);
113+
const onSubmit = ({formData}, e) => console.log("Data submitted: ", formData);
114114

115115
render((
116116
<Form schema={schema}

‎playground/app.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,10 @@ class App extends Component {
476476
uiSchema={uiSchema}
477477
formData={formData}
478478
onChange={this.onFormDataChange}
479-
onSubmit={({ formData }) =>
480-
console.log("submitted formData", formData)
481-
}
479+
onSubmit={({ formData }, e) => {
480+
console.log("submitted formData", formData);
481+
console.log("submit event", e);
482+
}}
482483
fields={{ geo: GeoPosition }}
483484
validate={validate}
484485
onBlur={(id, value) =>

‎src/components/Form.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export default class Form extends Component {
151151

152152
onSubmit = event => {
153153
event.preventDefault();
154+
event.persist();
154155

155156
if (!this.props.noValidate) {
156157
const { errors, errorSchema } = this.validate(this.state.formData);
@@ -168,7 +169,7 @@ export default class Form extends Component {
168169

169170
this.setState({ errors: [], errorSchema: {} }, () => {
170171
if (this.props.onSubmit) {
171-
this.props.onSubmit({ ...this.state, status: "submitted" });
172+
this.props.onSubmit({ ...this.state, status: "submitted" }, event);
172173
}
173174
});
174175
};

‎test/Form_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -774,15 +774,15 @@ describe("Form", () => {
774774
foo: "bar",
775775
};
776776
const onSubmit = sandbox.spy();
777+
const event = { type: "submit" };
777778
const { comp, node } = createFormComponent({
778779
schema,
779780
formData,
780781
onSubmit,
781782
});
782783

783-
Simulate.submit(node);
784-
785-
sinon.assert.calledWithMatch(onSubmit, comp.state);
784+
Simulate.submit(node, event);
785+
sinon.assert.calledWithMatch(onSubmit, comp.state, event);
786786
});
787787

788788
it("should not call provided submit handler on validation errors", () => {

0 commit comments

Comments
 (0)
Please sign in to comment.