Skip to content

Commit f3f36d8

Browse files
authored
fix(ui): checkboxes as first field were crashing WhereBuilder (#10290)
Fixes an issue where if a checkbox field was in the first position of a collection, and you tried to filter on it via the List view, the page would crash.
1 parent ee3c2cc commit f3f36d8

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ jobs:
289289
- fields-relationship
290290
- fields__collections__Array
291291
- fields__collections__Blocks
292+
- fields__collections__Checkbox
292293
- fields__collections__Collapsible
293294
- fields__collections__ConditionalLogic
294295
- fields__collections__CustomID

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export const WhereBuilder: React.FC<WhereBuilderProps> = (props) => {
174174
const initialValue =
175175
conditions[orIndex].and[andIndex]?.[initialFieldName]?.[
176176
initialOperator
177-
] || ''
177+
] || undefined
178178

179179
return (
180180
<li key={andIndex}>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import type { Page } from '@playwright/test'
2+
3+
import { expect, test } from '@playwright/test'
4+
import path from 'path'
5+
import { wait } from 'payload/shared'
6+
import { fileURLToPath } from 'url'
7+
8+
import { ensureCompilationIsDone, initPageConsoleErrorCatch } from '../../../helpers.js'
9+
import { AdminUrlUtil } from '../../../helpers/adminUrlUtil.js'
10+
import { initPayloadE2ENoConfig } from '../../../helpers/initPayloadE2ENoConfig.js'
11+
import { reInitializeDB } from '../../../helpers/reInitializeDB.js'
12+
import { RESTClient } from '../../../helpers/rest.js'
13+
import { TEST_TIMEOUT_LONG } from '../../../playwright.config.js'
14+
import { checkboxFieldsSlug } from '../../slugs.js'
15+
16+
const filename = fileURLToPath(import.meta.url)
17+
const currentFolder = path.dirname(filename)
18+
const dirname = path.resolve(currentFolder, '../../')
19+
20+
const { beforeAll, beforeEach, describe } = test
21+
22+
let client: RESTClient
23+
let page: Page
24+
let serverURL: string
25+
let url: AdminUrlUtil
26+
27+
describe('Checkboxes', () => {
28+
beforeAll(async ({ browser }, testInfo) => {
29+
testInfo.setTimeout(TEST_TIMEOUT_LONG)
30+
process.env.SEED_IN_CONFIG_ONINIT = 'false' // Makes it so the payload config onInit seed is not run. Otherwise, the seed would be run unnecessarily twice for the initial test run - once for beforeEach and once for onInit
31+
;({ serverURL } = await initPayloadE2ENoConfig({
32+
dirname,
33+
// prebuild,
34+
}))
35+
36+
url = new AdminUrlUtil(serverURL, checkboxFieldsSlug)
37+
38+
const context = await browser.newContext()
39+
page = await context.newPage()
40+
initPageConsoleErrorCatch(page)
41+
42+
await ensureCompilationIsDone({ page, serverURL })
43+
})
44+
45+
beforeEach(async () => {
46+
await reInitializeDB({
47+
serverURL,
48+
snapshotKey: 'fieldsTest',
49+
uploadsDir: path.resolve(dirname, './collections/Upload/uploads'),
50+
})
51+
if (client) {
52+
await client.logout()
53+
}
54+
client = new RESTClient(null, { defaultSlug: 'users', serverURL })
55+
await client.login()
56+
await ensureCompilationIsDone({ page, serverURL })
57+
})
58+
59+
test('should not crash on filtering where checkbox is first field', async () => {
60+
await page.goto(url.list)
61+
62+
const filterButton = page.locator('.list-controls__toggle-where')
63+
await filterButton.click()
64+
65+
const addButton = page.locator('.where-builder__add-first-filter')
66+
await addButton.click()
67+
68+
const operator = page.locator('.condition__operator .rs__control')
69+
await operator.click()
70+
71+
const equals = page.locator('.rs__option:has-text("equals")')
72+
await equals.click()
73+
74+
const value = page.locator('.condition__value')
75+
await value.click()
76+
77+
const trueOption = page.locator('.rs__option:has-text("True")')
78+
await trueOption.click()
79+
80+
await wait(1000)
81+
82+
await expect(page.locator('table > tbody > tr')).toHaveCount(1)
83+
})
84+
})

test/fields/seed.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { uploadsDoc } from './collections/Upload/shared.js'
3131
import {
3232
arrayFieldsSlug,
3333
blockFieldsSlug,
34+
checkboxFieldsSlug,
3435
codeFieldsSlug,
3536
collapsibleFieldsSlug,
3637
collectionSlugs,
@@ -308,6 +309,24 @@ export const seed = async (_payload: Payload) => {
308309
overrideAccess: true,
309310
})
310311

312+
await _payload.create({
313+
collection: checkboxFieldsSlug,
314+
data: {
315+
checkbox: true,
316+
},
317+
depth: 0,
318+
overrideAccess: true,
319+
})
320+
321+
await _payload.create({
322+
collection: checkboxFieldsSlug,
323+
data: {
324+
checkbox: false,
325+
},
326+
depth: 0,
327+
overrideAccess: true,
328+
})
329+
311330
await _payload.create({
312331
collection: codeFieldsSlug,
313332
data: codeDoc,

0 commit comments

Comments
 (0)