From 59038c0ce0f9f215e78e0f730d9e6138b5dcbf20 Mon Sep 17 00:00:00 2001 From: Travis Dahl Date: Fri, 8 Feb 2019 14:10:34 -0800 Subject: [PATCH] Submit event should return original event as second param (#1172) --- docs/index.md | 4 ++-- playground/app.js | 7 ++++--- src/components/Form.js | 3 ++- test/Form_test.js | 6 +++--- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/docs/index.md b/docs/index.md index e1550ef85d..0887052f5e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -107,10 +107,10 @@ render(( #### Form submission -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: +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: ```js -const onSubmit = ({formData}) => console.log("Data submitted: ", formData); +const onSubmit = ({formData}, e) => console.log("Data submitted: ", formData); render((
- console.log("submitted formData", formData) - } + onSubmit={({ formData }, e) => { + console.log("submitted formData", formData); + console.log("submit event", e); + }} fields={{ geo: GeoPosition }} validate={validate} onBlur={(id, value) => diff --git a/src/components/Form.js b/src/components/Form.js index 87b96c0e08..182f56af38 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -151,6 +151,7 @@ export default class Form extends Component { onSubmit = event => { event.preventDefault(); + event.persist(); if (!this.props.noValidate) { const { errors, errorSchema } = this.validate(this.state.formData); @@ -168,7 +169,7 @@ export default class Form extends Component { this.setState({ errors: [], errorSchema: {} }, () => { if (this.props.onSubmit) { - this.props.onSubmit({ ...this.state, status: "submitted" }); + this.props.onSubmit({ ...this.state, status: "submitted" }, event); } }); }; diff --git a/test/Form_test.js b/test/Form_test.js index 23b6ea8169..3933f570d3 100644 --- a/test/Form_test.js +++ b/test/Form_test.js @@ -774,15 +774,15 @@ describe("Form", () => { foo: "bar", }; const onSubmit = sandbox.spy(); + const event = { type: "submit" }; const { comp, node } = createFormComponent({ schema, formData, onSubmit, }); - Simulate.submit(node); - - sinon.assert.calledWithMatch(onSubmit, comp.state); + Simulate.submit(node, event); + sinon.assert.calledWithMatch(onSubmit, comp.state, event); }); it("should not call provided submit handler on validation errors", () => {