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

WIP: Pre-add items to arrays fields when they are mandatory. #116

Open
wants to merge 1 commit into
base: legacy
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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);
})