diff --git a/packages/live-preview/src/traverseFields.ts b/packages/live-preview/src/traverseFields.ts index 271ff5f00..86064e5f0 100644 --- a/packages/live-preview/src/traverseFields.ts +++ b/packages/live-preview/src/traverseFields.ts @@ -25,6 +25,14 @@ export const traverseFields = (args: { switch (fieldSchema.type) { case 'array': + if ( + !incomingData[fieldName] && + incomingData[fieldName] !== undefined && + result?.[fieldName] !== undefined + ) { + result[fieldName] = [] + } + if (Array.isArray(incomingData[fieldName])) { result[fieldName] = incomingData[fieldName].map((incomingRow, i) => { if (!result[fieldName]) { @@ -85,7 +93,7 @@ export const traverseFields = (args: { break case 'group': - + // falls through case 'tabs': if (!result[fieldName]) { result[fieldName] = {} @@ -100,8 +108,9 @@ export const traverseFields = (args: { }) break - case 'relationship': + case 'relationship': + // falls through case 'upload': // Handle `hasMany` relationships if (fieldSchema.hasMany && Array.isArray(incomingData[fieldName])) { diff --git a/test/live-preview/int.spec.ts b/test/live-preview/int.spec.ts index 0b744c89b..4df7b919a 100644 --- a/test/live-preview/int.spec.ts +++ b/test/live-preview/int.spec.ts @@ -169,6 +169,53 @@ describe('Collections - Live Preview', () => { expect(mergedData._numberOfRequests).toEqual(0) }) + it('— arrays - can clear all rows', async () => { + const initialData: Partial = { + title: 'Test Page', + arrayOfRelationships: [ + { + id: '123', + relationshipInArrayMonoHasOne: testPost.id, + }, + ], + } + + const mergedData = await mergeData({ + depth: 1, + fieldSchema: schemaJSON, + incomingData: { + ...initialData, + arrayOfRelationships: [], + }, + initialData, + serverURL, + returnNumberOfRequests: true, + collectionPopulationRequestHandler, + }) + + expect(mergedData.arrayOfRelationships).toEqual([]) + expect(mergedData._numberOfRequests).toEqual(0) + + // do the same but with arrayOfRelationships: 0 + + const mergedData2 = await mergeData({ + depth: 1, + fieldSchema: schemaJSON, + incomingData: { + ...initialData, + // @ts-expect-error eslint-disable-next-line + arrayOfRelationships: 0, // this is how form state represents an empty array + }, + initialData, + serverURL, + returnNumberOfRequests: true, + collectionPopulationRequestHandler, + }) + + expect(mergedData2.arrayOfRelationships).toEqual([]) + expect(mergedData2._numberOfRequests).toEqual(0) + }) + it('— uploads - adds and removes media', async () => { const initialData: Partial = { title: 'Test Page',