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 Nov 15, 2019
1 parent ecbed13 commit 0dd4994
Showing 1 changed file with 17 additions and 4 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

0 comments on commit 0dd4994

Please sign in to comment.