From 1806a2f0412ace84ea052e81108e31b176415cf0 Mon Sep 17 00:00:00 2001 From: Bartholomew Date: Fri, 14 Jun 2019 16:46:33 +0100 Subject: [PATCH] fix(core): list widget default values (#2374) --- .../src/actions/__tests__/entries.spec.js | 82 +++++++++++++++++++ .../netlify-cms-core/src/actions/entries.js | 21 ++++- 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 packages/netlify-cms-core/src/actions/__tests__/entries.spec.js diff --git a/packages/netlify-cms-core/src/actions/__tests__/entries.spec.js b/packages/netlify-cms-core/src/actions/__tests__/entries.spec.js new file mode 100644 index 00000000..c4f22ea3 --- /dev/null +++ b/packages/netlify-cms-core/src/actions/__tests__/entries.spec.js @@ -0,0 +1,82 @@ +import { fromJS } from 'immutable'; +import { createEmptyDraftData } from '../entries'; + +describe('entries', () => { + describe('createEmptyDraftData', () => { + it('should set default value for list field widget', () => { + const fields = fromJS([ + { + name: 'images', + widget: 'list', + field: { name: 'url', widget: 'text', default: 'https://image.png' }, + }, + ]); + expect(createEmptyDraftData(fields)).toEqual({ images: ['https://image.png'] }); + }); + + it('should set default values for list fields widget', () => { + const fields = fromJS([ + { + name: 'images', + widget: 'list', + fields: [ + { name: 'title', widget: 'text', default: 'default image' }, + { name: 'url', widget: 'text', default: 'https://image.png' }, + ], + }, + ]); + expect(createEmptyDraftData(fields)).toEqual({ + images: [{ title: 'default image', url: 'https://image.png' }], + }); + }); + + it('should not set empty value for list fields widget', () => { + const fields = fromJS([ + { + name: 'images', + widget: 'list', + fields: [{ name: 'title', widget: 'text' }, { name: 'url', widget: 'text' }], + }, + ]); + expect(createEmptyDraftData(fields)).toEqual({}); + }); + + it('should set default value for object field widget', () => { + const fields = fromJS([ + { + name: 'post', + widget: 'object', + field: { name: 'image', widget: 'text', default: 'https://image.png' }, + }, + ]); + expect(createEmptyDraftData(fields)).toEqual({ post: { image: 'https://image.png' } }); + }); + + it('should set default values for object fields widget', () => { + const fields = fromJS([ + { + name: 'post', + widget: 'object', + fields: [ + { name: 'title', widget: 'text', default: 'default title' }, + { name: 'url', widget: 'text', default: 'https://image.png' }, + ], + }, + ]); + expect(createEmptyDraftData(fields)).toEqual({ + post: { title: 'default title', url: 'https://image.png' }, + }); + }); + + it('should not set empty value for object fields widget', () => { + const fields = fromJS([ + { + name: 'post', + widget: 'object', + fields: [{ name: 'title', widget: 'text' }, { name: 'url', widget: 'text' }], + }, + ]); + expect(createEmptyDraftData(fields)).toEqual({}); + }); + }); +}); diff --git a/packages/netlify-cms-core/src/actions/entries.js b/packages/netlify-cms-core/src/actions/entries.js index cd88ff5c..7bfc10d4 100644 --- a/packages/netlify-cms-core/src/actions/entries.js +++ b/packages/netlify-cms-core/src/actions/entries.js @@ -1,4 +1,5 @@ import { fromJS, List, Map } from 'immutable'; +import { isEqual } from 'lodash'; import { actions as notifActions } from 'redux-notifications'; import { serializeValues } from 'Lib/serializeEntryValues'; import { currentBackend } from 'coreSrc/backend'; @@ -422,24 +423,38 @@ export function createEmptyDraft(collection) { }; } -function createEmptyDraftData(fields) { +export function createEmptyDraftData(fields, withNameKey = true) { return fields.reduce((acc, item) => { 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); + const isEmptyDefaultValue = val => [[{}], {}].some(e => isEqual(val, e)); if (List.isList(subfields)) { - acc[name] = list ? [createEmptyDraftData(subfields)] : createEmptyDraftData(subfields); + const subDefaultValue = list + ? [createEmptyDraftData(subfields)] + : createEmptyDraftData(subfields); + if (!isEmptyDefaultValue(subDefaultValue)) { + acc[name] = subDefaultValue; + } return acc; } if (Map.isMap(subfields)) { - acc[name] = list ? [createEmptyDraftData([subfields])] : createEmptyDraftData([subfields]); + const subDefaultValue = list + ? [createEmptyDraftData([subfields], false)] + : createEmptyDraftData([subfields]); + if (!isEmptyDefaultValue(subDefaultValue)) { + acc[name] = subDefaultValue; + } return acc; } if (defaultValue !== null) { + if (!withNameKey) { + return defaultValue; + } acc[name] = defaultValue; }