-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Changes from 1 commit
c66fbd6
c171226
2f2a88b
56950c7
d23f03f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
return defaults || []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO But other tests would break. However, it would be more clear when all defaults would originate in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. I've updated the PR |
||
} | ||
return defaults; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO I've added a test for this behaviour |
||
}); | ||
}); | ||
|
||
describe("Multiple choices list", () => { | ||
|
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed