Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dist/react-jsonschema-form-0.4.1.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/react-jsonschema-form-0.4.1.js.map

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ export default class Form extends Component {

constructor(props) {
super(props);
this.state = this.getStateFromProps(props);
}

componentWillReceiveProps(nextProps) {
this.setState(this.getStateFromProps(nextProps));
}

getStateFromProps(props) {
const edit = !!props.formData;
const formData = props.formData || getDefaultFormState(props.schema) || null;
this.state = {
return {
status: "initial",
formData,
edit,
Expand Down
10 changes: 9 additions & 1 deletion src/components/fields/ArrayField.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ class ArrayField extends Component {

constructor(props) {
super(props);
this.state = this.getStateFromProps(props);
}

componentWillReceiveProps(nextProps) {
this.setState(this.getStateFromProps(nextProps));
}

getStateFromProps(props) {
const formData = Array.isArray(props.formData) ? props.formData : null;
this.state = {items: formData || getDefaultFormState(props.schema) || []};
return {items: formData || getDefaultFormState(props.schema) || []};
}

get itemTitle() {
Expand Down
10 changes: 9 additions & 1 deletion src/components/fields/ObjectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ class ObjectField extends Component {

constructor(props) {
super(props);
this.state = props.formData || getDefaultFormState(props.schema) || {};
this.state = this.getStateFromProps(props);
}

componentWillReceiveProps(nextProps) {
this.setState(this.getStateFromProps(nextProps));
}

getStateFromProps(props) {
return props.formData || getDefaultFormState(props.schema) || {};
}

isRequired(name) {
Expand Down
47 changes: 47 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,4 +830,51 @@ describe("Form", () => {
sinon.assert.calledOnce(onError);
});
});

describe("External formData updates", () => {
describe("root level", () => {
it("should update form state from new formData prop value", () => {
const schema = {type: "string"};
const {comp} = createComponent({schema});

comp.componentWillReceiveProps({formData: "yo"});

expect(comp.state.formData).eql("yo");
});
});

describe("object level", () => {
it("should update form state from new formData prop value", () => {
const schema = {
type: "object",
properties: {
foo: {
type: "string"
}
}
};
const {comp} = createComponent({schema});

comp.componentWillReceiveProps({formData: {foo: "yo"}});

expect(comp.state.formData).eql({foo: "yo"});
});
});

describe("array level", () => {
it("should update form state from new formData prop value", () => {
const schema = {
type: "array",
items: {
type: "string"
}
};
const {comp} = createComponent({schema});

comp.componentWillReceiveProps({formData: ["yo"]});

expect(comp.state.formData).eql(["yo"]);
});
});
});
});