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

Fix #1424: Set value for boolean field without default value #2525

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions packages/netlify-cms-core/src/actions/__tests__/entries.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,18 @@ describe('entries', () => {
]);
expect(createEmptyDraftData(fields)).toEqual({});
});

it('should not return an empty object for boolean widgets without a default value', () => {
const fields = fromJS([
{
name: 'post',
widget: 'boolean',
fields: [{ name: 'title', widget: 'boolean' }],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boolean widgets don't have fields attrs, test for { name: 'boolean', widget: 'boolean' } for example, which should output { boolean: false }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched the parent back to object, like all the other tests. That was a leftover from some back and forth testing...

},
]);
expect(createEmptyDraftData(fields)).toEqual({
post: { title: false },
});
});
});
});
6 changes: 5 additions & 1 deletion packages/netlify-cms-core/src/actions/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,11 @@ export function createEmptyDraftData(fields, withNameKey = true) {
const subfields = item.get('field') || item.get('fields');
const list = item.get('widget') == 'list';
const name = item.get('name');
const defaultValue = item.get('default', null);
// A boolean field without a default value defined will initially render
// render as false, but the field will fail the required check. So we are
// assinging false.
const widgetDefaultValue = item.get('widget') == 'boolean' ? false : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really want to avoid hardcoding widget names. The underlying issue here is bigger than just the boolean widget.

const defaultValue = item.get('default', widgetDefaultValue);
const isEmptyDefaultValue = val => [[{}], {}].some(e => isEqual(val, e));

if (List.isList(subfields)) {
Expand Down