Skip to content

Commit 9ba4c3a

Browse files
committed
fix: Call latest onChange
1 parent ac42a22 commit 9ba4c3a

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

packages/core/src/components/Form.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ export default class Form extends Component {
4646
if (
4747
!deepEquals(nextState.formData, nextProps.formData) &&
4848
!deepEquals(nextState.formData, this.state.formData) &&
49-
this.props.onChange
49+
nextProps.onChange
5050
) {
51-
this.props.onChange(nextState);
51+
nextProps.onChange(nextState);
5252
}
5353
this.setState(nextState);
5454
}

packages/core/test/Form_test.js

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from "chai";
22
import sinon from "sinon";
33
import React from "react";
4-
import { renderIntoDocument, Simulate } from "react-dom/test-utils";
4+
import { renderIntoDocument, act, Simulate } from "react-dom/test-utils";
55
import { findDOMNode } from "react-dom";
66
import { Portal } from "react-portal";
77
import { createRef } from "create-react-ref";
@@ -871,7 +871,55 @@ describeRepeated("Form common", createFormComponent => {
871871
},
872872
});
873873
});
874+
875+
it("should call last provided change handler", async () => {
876+
const schema = {
877+
type: "object",
878+
properties: {
879+
foo: {
880+
type: "string",
881+
default: "bar",
882+
},
883+
},
884+
};
885+
886+
const secondOnChange = sandbox.spy();
887+
888+
const { comp, onChange } = createFormComponent({
889+
schema,
890+
formData: { foo: "bar1" },
891+
});
892+
893+
act(() => {
894+
setProps(comp, {
895+
schema,
896+
formData: {},
897+
onChange,
898+
});
899+
});
900+
901+
sinon.assert.callCount(onChange, 1);
902+
903+
act(() => {
904+
setProps(comp, {
905+
schema,
906+
formData: { foo: "bar2" },
907+
});
908+
});
909+
910+
act(() => {
911+
setProps(comp, {
912+
schema,
913+
formData: {},
914+
onChange: secondOnChange,
915+
});
916+
});
917+
918+
sinon.assert.callCount(onChange, 1);
919+
sinon.assert.callCount(secondOnChange, 1);
920+
});
874921
});
922+
875923
describe("Blur handler", () => {
876924
it("should call provided blur handler on form input blur event", () => {
877925
const schema = {

0 commit comments

Comments
 (0)