Skip to content

Commit

Permalink
Submit event should return original event as second param (#1172)
Browse files Browse the repository at this point in the history
  • Loading branch information
travisdahl authored and epicfaace committed Feb 8, 2019
1 parent 919a164 commit 59038c0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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((
<Form schema={schema}
Expand Down
7 changes: 4 additions & 3 deletions playground/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,10 @@ class App extends Component {
uiSchema={uiSchema}
formData={formData}
onChange={this.onFormDataChange}
onSubmit={({ formData }) =>
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) =>
Expand Down
3 changes: 2 additions & 1 deletion src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
});
};
Expand Down
6 changes: 3 additions & 3 deletions test/Form_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 59038c0

Please sign in to comment.