Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#434 - Render empty array item fields when minItems is specified #484

Merged
merged 5 commits into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 17 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,23 @@ function computeDefaults(schema, parentDefaults, definitions={}) {
if (typeof(defaults) === "undefined") {
defaults = schema.default;
}
// We need to recur for object schema inner default values.
if (schema.type === "object") {
return Object.keys(schema.properties).reduce((acc, key) => {
// Compute the defaults for this node, with the parent defaults we might
// have from a previous run: defaults[key].
acc[key] = computeDefaults(
schema.properties[key], (defaults || {})[key], definitions);
return acc;
}, {});

switch (schema.type) {
// We need to recur for object schema inner default values.
case "object":
return Object.keys(schema.properties).reduce((acc, key) => {
// Compute the defaults for this node, with the parent defaults we might
// have from a previous run: defaults[key].
acc[key] = computeDefaults(
schema.properties[key], (defaults || {})[key], definitions);
return acc;
}, {});

case "array":
if (schema.minItems) {
return new Array(schema.minItems).fill(schema.items.default);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this involve a recursive call to computeDefaults ? For example, what if the items are an object or have $ref ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed.

Copy link
Contributor Author

@Reggino Reggino Feb 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed

}
return defaults || [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we fallback to an empty array here? I understand it may be confusing but we need to distinguish between an empty array and no array value defined at all. I'd suggest we just return defaults here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO computeDefaults would ideally be the only place where defaults are "made up". I actually tried to remove this line:
https://github.com/mozilla-services/react-jsonschema-form/blob/master/src/components/fields/ArrayField.js#L153

But other tests would break. However, it would be more clear when all defaults would originate in computeDefaults

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW when no value would be defined at all, that would lead to [] due to the defaultProps in ArrayField.js

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, we'll have to work to make this more consistent in the future. Meanwhile here, I'm pretty sure we could remove the whole return defaults || []; line without breaking anything, as that would match previous behavior, don't you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I've updated the PR

}
return defaults;
}
Expand Down
19 changes: 19 additions & 0 deletions test/ArrayField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,25 @@ describe("ArrayField", () => {
expect(inputs[2].id).eql("root_foo_1_bar");
expect(inputs[3].id).eql("root_foo_1_baz");
});

it("should render enough inputs to match minItems in schema when no formData is set", () => {
const complexSchema = {
type: "object",
properties: {
foo: {
type: "array",
minItems: 4,
items: {
type: "string"
}
}
}
};
const {node} = createFormComponent({schema: complexSchema, formData: { }});

const inputs = node.querySelectorAll("input[type=text]");
expect(inputs.length).eql(4);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test that the right default value is chosen? I see that there's no other tests for the "default" behavior for ArrayField, so this might be excessive.

Should we test that merging formData with defaults works correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO formData and defaults can really be merged in case of an array: it is either set or not set. When set, the form should honor the given value. When not set it may compute a sane default.

I've added a test for this behaviour

});
});

describe("Multiple choices list", () => {
Expand Down