Skip to content

Commit e259c8d

Browse files
committed
Fixes #14: Update form state when new formData prop is passed.
1 parent 72c3f28 commit e259c8d

File tree

6 files changed

+61
-6
lines changed

6 files changed

+61
-6
lines changed

dist/react-jsonschema-form-0.4.1.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-jsonschema-form-0.4.1.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Form.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@ export default class Form extends Component {
1313

1414
constructor(props) {
1515
super(props);
16+
this.state = this.getStateFromProps(props);
17+
}
18+
19+
componentWillReceiveProps(nextProps) {
20+
this.setState(this.getStateFromProps(nextProps));
21+
}
22+
23+
getStateFromProps(props) {
1624
const edit = !!props.formData;
1725
const formData = props.formData || getDefaultFormState(props.schema) || null;
18-
this.state = {
26+
return {
1927
status: "initial",
2028
formData,
2129
edit,

src/components/fields/ArrayField.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ class ArrayField extends Component {
1111

1212
constructor(props) {
1313
super(props);
14+
this.state = this.getStateFromProps(props);
15+
}
16+
17+
componentWillReceiveProps(nextProps) {
18+
this.setState(this.getStateFromProps(nextProps));
19+
}
20+
21+
getStateFromProps(props) {
1422
const formData = Array.isArray(props.formData) ? props.formData : null;
15-
this.state = {items: formData || getDefaultFormState(props.schema) || []};
23+
return {items: formData || getDefaultFormState(props.schema) || []};
1624
}
1725

1826
get itemTitle() {

src/components/fields/ObjectField.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ class ObjectField extends Component {
1111

1212
constructor(props) {
1313
super(props);
14-
this.state = props.formData || getDefaultFormState(props.schema) || {};
14+
this.state = this.getStateFromProps(props);
15+
}
16+
17+
componentWillReceiveProps(nextProps) {
18+
this.setState(this.getStateFromProps(nextProps));
19+
}
20+
21+
getStateFromProps(props) {
22+
return props.formData || getDefaultFormState(props.schema) || {};
1523
}
1624

1725
isRequired(name) {

test/index_test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,4 +830,35 @@ describe("Form", () => {
830830
sinon.assert.calledOnce(onError);
831831
});
832832
});
833+
834+
describe("External formData updates", () => {
835+
describe("root level", () => {
836+
it("should update form state from new formData prop value", () => {
837+
const schema = {type: "string"};
838+
const {comp} = createComponent({schema});
839+
840+
comp.componentWillReceiveProps({formData: "yo"});
841+
842+
expect(comp.state.formData).eql("yo");
843+
});
844+
});
845+
846+
describe("object level", () => {
847+
it("should update form state from new formData prop value", () => {
848+
const schema = {
849+
type: "object",
850+
properties: {
851+
foo: {
852+
type: "string"
853+
}
854+
}
855+
};
856+
const {comp} = createComponent({schema});
857+
858+
comp.componentWillReceiveProps({formData: {foo: "yo"}});
859+
860+
expect(comp.state.formData).eql({foo: "yo"});
861+
});
862+
});
863+
});
833864
});

0 commit comments

Comments
 (0)