Skip to content

Commit

Permalink
WIP: Pre-add items to arrays fields when they are mandatory.
Browse files Browse the repository at this point in the history
  • Loading branch information
bfabio committed Feb 5, 2020
1 parent ecbed13 commit 5e59914
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/app/form/widgets/ArrayWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const renderArrayFields = (
fieldName,
remove,
context,
swap
swap,
required,
) => {
const prefix = fieldName + ".";

Expand All @@ -26,16 +27,22 @@ const renderArrayFields = (
isSummary = true;
}
schema.isSummary = isSummary;

// We always show the button for removing the field unless
// it's the first field of a required array field (ie. we don't allow its
// removal).
const show_close_button = (required && idx > 0) || !required;

return (
<div key={idx}>
<div className="float-right">
{show_close_button && <div className="float-right">
<CloseButton
onClick={e => {
e.preventDefault();
remove(idx);
}}
/>
</div>
</div>}
{renderField(
{ ...schema, showLabel: false },
idx.toString(),
Expand All @@ -57,6 +64,11 @@ const renderInput = field => {
{ "has-error": field.meta.submitFailed && field.meta.error }
]);

/* Pre-add a field if the array field is required. */
if (field.fields.length == 0 && field.schema.required) {
field.fields.push();
}

return (
<div className={className}>
{field.showLabel && (
Expand All @@ -74,7 +86,8 @@ const renderInput = field => {
field.context,
(a, b) => {
field.fields.swap(a, b);
}
},
field.schema.required,
)}
<div>
<a href="#" className="link" onClick={() => field.fields.push()}>
Expand Down
2 changes: 1 addition & 1 deletion src/test/rf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ it('editorForm renders correctly', async () => {
</Provider>);

const inputs = wrapper.find('input');
expect(inputs.length).toBe(54);
expect(inputs.length).toBe(55);
})

0 comments on commit 5e59914

Please sign in to comment.