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
5 changes: 4 additions & 1 deletion src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from "prop-types";
import { default as DefaultErrorList } from "./ErrorList";
import {
getDefaultFormState,
retrieveSchema,
shouldRender,
toIdSchema,
setState,
Expand Down Expand Up @@ -39,14 +40,16 @@ 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)
: {
errors: state.errors || [],
errorSchema: state.errorSchema || {},
};
const idSchema = toIdSchema(
schema,
retrievedSchema,
uiSchema["ui:rootFieldId"],
definitions,
formData
Expand Down
96 changes: 96 additions & 0 deletions test/Form_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down