diff --git a/packages/core/src/components/Form.js b/packages/core/src/components/Form.js index da07b91e6f..33c083a72b 100644 --- a/packages/core/src/components/Form.js +++ b/packages/core/src/components/Form.js @@ -46,9 +46,9 @@ export default class Form extends Component { if ( !deepEquals(nextState.formData, nextProps.formData) && !deepEquals(nextState.formData, this.state.formData) && - this.props.onChange + nextProps.onChange ) { - this.props.onChange(nextState); + nextProps.onChange(nextState); } this.setState(nextState); } diff --git a/packages/core/test/Form_test.js b/packages/core/test/Form_test.js index 412715946e..2bccab8b82 100644 --- a/packages/core/test/Form_test.js +++ b/packages/core/test/Form_test.js @@ -1,7 +1,7 @@ import { expect } from "chai"; import sinon from "sinon"; import React from "react"; -import { renderIntoDocument, Simulate } from "react-dom/test-utils"; +import { renderIntoDocument, act, Simulate } from "react-dom/test-utils"; import { findDOMNode } from "react-dom"; import { Portal } from "react-portal"; import { createRef } from "create-react-ref"; @@ -871,7 +871,55 @@ describeRepeated("Form common", createFormComponent => { }, }); }); + + it("should call last provided change handler", async () => { + const schema = { + type: "object", + properties: { + foo: { + type: "string", + default: "bar", + }, + }, + }; + + const secondOnChange = sandbox.spy(); + + const { comp, onChange } = createFormComponent({ + schema, + formData: { foo: "bar1" }, + }); + + act(() => { + setProps(comp, { + schema, + formData: {}, + onChange, + }); + }); + + sinon.assert.callCount(onChange, 1); + + act(() => { + setProps(comp, { + schema, + formData: { foo: "bar2" }, + }); + }); + + act(() => { + setProps(comp, { + schema, + formData: {}, + onChange: secondOnChange, + }); + }); + + sinon.assert.callCount(onChange, 1); + sinon.assert.callCount(secondOnChange, 1); + }); }); + describe("Blur handler", () => { it("should call provided blur handler on form input blur event", () => { const schema = {