fix: allow setting an empty array as default for complex lists (#3746)
This commit is contained in:
parent
3cc7484725
commit
6042383b9d
@ -125,6 +125,60 @@ describe('entries', () => {
|
||||
});
|
||||
});
|
||||
describe('createEmptyDraftData', () => {
|
||||
it('should allow an empty array as list default for a single field list', () => {
|
||||
const fields = fromJS([
|
||||
{
|
||||
name: 'images',
|
||||
widget: 'list',
|
||||
default: [],
|
||||
field: { name: 'url', widget: 'text' },
|
||||
},
|
||||
]);
|
||||
expect(createEmptyDraftData(fields)).toEqual({ images: fromJS([]) });
|
||||
});
|
||||
|
||||
it('should allow an empty array as list default for a fields list', () => {
|
||||
const fields = fromJS([
|
||||
{
|
||||
name: 'images',
|
||||
widget: 'list',
|
||||
default: [],
|
||||
fields: [
|
||||
{ name: 'title', widget: 'text' },
|
||||
{ name: 'url', widget: 'text' },
|
||||
],
|
||||
},
|
||||
]);
|
||||
expect(createEmptyDraftData(fields)).toEqual({ images: fromJS([]) });
|
||||
});
|
||||
|
||||
it('should not allow setting a non empty array as a default value for a single field list', () => {
|
||||
const fields = fromJS([
|
||||
{
|
||||
name: 'images',
|
||||
widget: 'list',
|
||||
default: [{ name: 'url' }, { other: 'field' }],
|
||||
field: { name: 'url', widget: 'text' },
|
||||
},
|
||||
]);
|
||||
expect(createEmptyDraftData(fields)).toEqual({});
|
||||
});
|
||||
|
||||
it('should not allow setting a non empty array as a default value for a fields list', () => {
|
||||
const fields = fromJS([
|
||||
{
|
||||
name: 'images',
|
||||
widget: 'list',
|
||||
default: [{ name: 'url' }, { other: 'field' }],
|
||||
fields: [
|
||||
{ name: 'title', widget: 'text' },
|
||||
{ name: 'url', widget: 'text' },
|
||||
],
|
||||
},
|
||||
]);
|
||||
expect(createEmptyDraftData(fields)).toEqual({});
|
||||
});
|
||||
|
||||
it('should set default value for list field widget', () => {
|
||||
const fields = fromJS([
|
||||
{
|
||||
@ -136,6 +190,18 @@ describe('entries', () => {
|
||||
expect(createEmptyDraftData(fields)).toEqual({ images: ['https://image.png'] });
|
||||
});
|
||||
|
||||
it('should override list default with field default', () => {
|
||||
const fields = fromJS([
|
||||
{
|
||||
name: 'images',
|
||||
widget: 'list',
|
||||
default: [],
|
||||
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([
|
||||
{
|
||||
@ -152,6 +218,23 @@ describe('entries', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should override list default with fields default', () => {
|
||||
const fields = fromJS([
|
||||
{
|
||||
name: 'images',
|
||||
widget: 'list',
|
||||
default: [],
|
||||
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([
|
||||
{
|
||||
|
@ -633,15 +633,16 @@ interface DraftEntryData {
|
||||
| string
|
||||
| null
|
||||
| boolean
|
||||
| List<unknown>
|
||||
| DraftEntryData
|
||||
| DraftEntryData[]
|
||||
| (string | DraftEntryData | boolean)[];
|
||||
| (string | DraftEntryData | boolean | List<unknown>)[];
|
||||
}
|
||||
|
||||
export function createEmptyDraftData(fields: EntryFields, withNameKey = true) {
|
||||
return fields.reduce(
|
||||
(
|
||||
reduction: DraftEntryData | string | undefined | boolean,
|
||||
reduction: DraftEntryData | string | undefined | boolean | List<unknown>,
|
||||
value: EntryField | undefined | boolean,
|
||||
) => {
|
||||
const acc = reduction as DraftEntryData;
|
||||
@ -658,6 +659,9 @@ export function createEmptyDraftData(fields: EntryFields, withNameKey = true) {
|
||||
: createEmptyDraftData(subfields as EntryFields);
|
||||
if (!isEmptyDefaultValue(subDefaultValue)) {
|
||||
acc[name] = subDefaultValue;
|
||||
} else if (list && List.isList(defaultValue) && (defaultValue as List<unknown>).isEmpty()) {
|
||||
// allow setting an empty list as a default
|
||||
acc[name] = defaultValue;
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
@ -668,6 +672,9 @@ export function createEmptyDraftData(fields: EntryFields, withNameKey = true) {
|
||||
: createEmptyDraftData(List([subfields as EntryField]));
|
||||
if (!isEmptyDefaultValue(subDefaultValue)) {
|
||||
acc[name] = subDefaultValue;
|
||||
} else if (list && List.isList(defaultValue) && (defaultValue as List<unknown>).isEmpty()) {
|
||||
// allow setting an empty list as a default
|
||||
acc[name] = defaultValue;
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ export type EntryField = StaticallyTypedRecord<{
|
||||
types?: List<EntryField>;
|
||||
widget: string;
|
||||
name: string;
|
||||
default: string | null | boolean;
|
||||
default: string | null | boolean | List<unknown>;
|
||||
media_folder?: string;
|
||||
public_folder?: string;
|
||||
comment?: string;
|
||||
|
Loading…
x
Reference in New Issue
Block a user