fix(core): list widget default values (#2374)
This commit is contained in:
parent
6329c35d27
commit
1806a2f041
@ -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({});
|
||||
});
|
||||
});
|
||||
});
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user