Skip to content

Commit

Permalink
#434 - Render empty array item fields when minItems is specified (#484)
Browse files Browse the repository at this point in the history
* #434 - Render empty array item fields when minItems is specified

* Added tests + playground feature for minItems

* Lint issue

* Split tests: 2 assertions = 2 tests

* Don't fall back to array
  • Loading branch information
Reggino authored and glasserc committed Feb 24, 2017
1 parent bfcc87d commit 38a850b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 10 deletions.
21 changes: 20 additions & 1 deletion playground/samples/arrays.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
module.exports = {
schema: {
definitions: {
Thing: {
type: "object",
properties: {
name: {
type: "string",
default: "Default name"
}
}
}
},
type: "object",
properties: {
listOfStrings: {
Expand All @@ -15,7 +26,7 @@ module.exports = {
title: "A multiple choices list",
items: {
type: "string",
enum: ["foo", "bar", "fuzz", "qux"],
enum: ["foo", "bar", "fuzz", "qux"]
},
uniqueItems: true
},
Expand All @@ -38,6 +49,14 @@ module.exports = {
type: "number"
}
},
minItemsList: {
type: "array",
title: "A list with a minimal number of items",
minItems: 3,
items: {
$ref: "#/definitions/Thing"
}
},
nestedList: {
type: "array",
title: "Nested list",
Expand Down
25 changes: 16 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,22 @@ 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(computeDefaults(schema.items, defaults, definitions));
}
}
return defaults;
}
Expand Down
61 changes: 61 additions & 0 deletions test/ArrayField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,67 @@ 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 with proper defaults to match minItems in schema when no formData is set", () => {
const complexSchema = {
type: "object",
definitions: {
Thing: {
type: "object",
properties: {
name: {
type: "string",
default: "Default name"
}
}
}
},
properties: {
foo: {
type: "array",
minItems: 2,
items: {
$ref: "#/definitions/Thing"
}
}
}
};
let form = createFormComponent({schema: complexSchema, formData: { }});
let inputs = form.node.querySelectorAll("input[type=text]");
expect(inputs[0].value).eql("Default name");
expect(inputs[1].value).eql("Default name");
});

it("should honor given formData, even when it does not meet ths minItems-requirement", () => {
const complexSchema = {
type: "object",
definitions: {
Thing: {
type: "object",
properties: {
name: {
type: "string",
default: "Default name"
}
}
}
},
properties: {
foo: {
type: "array",
minItems: 2,
items: {
$ref: "#/definitions/Thing"
}
}
}
};
const form = createFormComponent({schema: complexSchema, formData: {foo: []}});
const inputs = form.node.querySelectorAll("input[type=text]");
expect(inputs.length).eql(0);
});


});

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

0 comments on commit 38a850b

Please sign in to comment.