diff --git a/src/components/Form.js b/src/components/Form.js index 79215ffae8..163fc33fe9 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -4,6 +4,7 @@ import PropTypes from "prop-types"; import { default as DefaultErrorList } from "./ErrorList"; import { getDefaultFormState, + retrieveSchema, shouldRender, toIdSchema, setState, @@ -39,6 +40,8 @@ export default class Form extends Component { const mustValidate = edit && !props.noValidate && liveValidate; const { definitions } = schema; const formData = getDefaultFormState(schema, props.formData, definitions); + const retrievedSchema = retrieveSchema(schema, definitions, formData); + const { errors, errorSchema } = mustValidate ? this.validate(formData, schema) : { @@ -46,7 +49,7 @@ export default class Form extends Component { errorSchema: state.errorSchema || {}, }; const idSchema = toIdSchema( - schema, + retrievedSchema, uiSchema["ui:rootFieldId"], definitions, formData diff --git a/test/Form_test.js b/test/Form_test.js index 282134448b..d1cbfdb51a 100644 --- a/test/Form_test.js +++ b/test/Form_test.js @@ -1259,6 +1259,102 @@ describe("Form", () => { }); }); + describe("idSchema updates based on formData", () => { + const schema = { + type: "object", + properties: { + a: { type: "string", enum: ["int", "bool"] }, + }, + dependencies: { + a: { + oneOf: [ + { + properties: { + a: { enum: ["int"] }, + }, + }, + { + properties: { + a: { enum: ["bool"] }, + b: { type: "boolean" }, + }, + }, + ], + }, + }, + }; + + it("should not update idSchema for a falsey value", () => { + const formData = { a: "int" }; + const { comp } = createFormComponent({ schema, formData }); + comp.componentWillReceiveProps({ + schema: { + type: "object", + properties: { + a: { type: "string", enum: ["int", "bool"] }, + }, + dependencies: { + a: { + oneOf: [ + { + properties: { + a: { enum: ["int"] }, + }, + }, + { + properties: { + a: { enum: ["bool"] }, + b: { type: "boolean" }, + }, + }, + ], + }, + }, + }, + formData: { a: "int" }, + }); + expect(comp.state.idSchema).eql({ $id: "root", a: { $id: "root_a" } }); + }); + + it("should update idSchema based on truthy value", () => { + const formData = { + a: "int", + }; + const { comp } = createFormComponent({ schema, formData }); + comp.componentWillReceiveProps({ + schema: { + type: "object", + properties: { + a: { type: "string", enum: ["int", "bool"] }, + }, + dependencies: { + a: { + oneOf: [ + { + properties: { + a: { enum: ["int"] }, + }, + }, + { + properties: { + a: { enum: ["bool"] }, + b: { type: "boolean" }, + }, + }, + ], + }, + }, + }, + formData: { a: "bool" }, + }); + expect(comp.state.idSchema).eql({ + $id: "root", + a: { $id: "root_a" }, + b: { $id: "root_b" }, + }); + }); + }); + describe("Attributes", () => { const formProps = { schema: {},