Skip to content

Commit 62fc2f5

Browse files
authored
fix(ui): collapsed array state on input change (#9800)
### What? Previously, `initCollapsed: true` `array` fields would auto collapse when typing in their respective inputs while in the create new view. ### Why? This was due to the fact that we were only checking if `preferences` existed in `form state` to handle the current state of the array row and then falling back on the `initCollapsed` prop if `preferences` didn't exist. This was a problem because during create - `preferences` do not exist yet. As a result, the state of the array row would keep falling back to collapsed if `initCollapsed` was set to `true`. ### How? To fix this, we now check the actual form state first before falling back to preferences and then falling back to the initCollapsed prop value. Fixes #9775
1 parent 10eab87 commit 62fc2f5

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

packages/ui/src/elements/TableColumns/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const TableColumnsProvider: React.FC<Props> = ({
7575
}) as ClientCollectionConfig
7676

7777
const prevCollection = React.useRef<SanitizedCollectionConfig['slug']>(collectionSlug)
78-
const { getPreference, setPreference } = usePreferences()
78+
const { getPreference } = usePreferences()
7979

8080
const [tableColumns, setTableColumns] = React.useState(columnState)
8181
const tableStateControllerRef = React.useRef<AbortController>(null)

packages/ui/src/forms/fieldSchemasToFormState/addFieldStatePromise.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,26 @@ export const addFieldStatePromise = async (args: AddFieldStatePromiseArgs): Prom
274274
}),
275275
)
276276

277-
const collapsedRowIDs = preferences?.fields?.[path]?.collapsed
277+
const previousRows = previousFormState?.[path]?.rows || []
278+
const collapsedRowIDsFromPrefs = preferences?.fields?.[path]?.collapsed
278279

279280
acc.rows.push({
280281
id: row.id,
281-
collapsed:
282-
collapsedRowIDs === undefined
283-
? field.admin.initCollapsed
284-
: collapsedRowIDs.includes(row.id),
282+
collapsed: (() => {
283+
// First, check if `previousFormState` has a matching row
284+
const previousRow = previousRows.find((prevRow) => prevRow.id === row.id)
285+
if (previousRow?.collapsed !== undefined) {
286+
return previousRow.collapsed
287+
}
288+
289+
// If previousFormState is undefined, check preferences
290+
if (collapsedRowIDsFromPrefs !== undefined) {
291+
return collapsedRowIDsFromPrefs.includes(row.id) // Check if collapsed in preferences
292+
}
293+
294+
// If neither exists, fallback to `field.admin.initCollapsed`
295+
return field.admin.initCollapsed
296+
})(),
285297
})
286298

287299
return acc

test/fields/collections/Array/e2e.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,12 @@ describe('Array', () => {
301301
await page.locator('#updateArrayExternally').click()
302302
await expect(page.locator('#custom-field')).toBeVisible()
303303
})
304+
305+
test('should not re-close initCollapsed true array rows on input in create new view', async () => {
306+
await page.goto(url.create)
307+
await page.locator('#field-collapsedArray >> .array-field__add-row').click()
308+
await page.locator('#field-collapsedArray__0__text').fill('test')
309+
const collapsedArrayRow = page.locator('#collapsedArray-row-0 .collapsible--collapsed')
310+
await expect(collapsedArrayRow).toBeHidden()
311+
})
304312
})

0 commit comments

Comments
 (0)