Skip to content

Commit 7a73265

Browse files
authored
fix(ui): clearing value from relationship filter leaves stale query (#11023)
When filtering the list view using conditions on a relationship field, clearing the value from the field would leave it in the query despite being removed from the component.
1 parent ec593b4 commit 7a73265

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,20 @@ export const Condition: React.FC<Props> = (props) => {
7676
const debouncedValue = useDebounce(internalQueryValue, 300)
7777

7878
useEffect(() => {
79-
// This is to trigger changes when the debounced value changes
80-
if (
81-
(fieldOption?.value || typeof fieldOption?.value === 'number') &&
82-
internalOperatorOption &&
83-
![null, undefined].includes(debouncedValue)
84-
) {
79+
if (debouncedValue === undefined) {
80+
return
81+
}
82+
83+
if (debouncedValue === null) {
84+
removeCondition({
85+
andIndex,
86+
orIndex,
87+
})
88+
89+
return
90+
}
91+
92+
if ((fieldOption?.value || typeof fieldOption?.value === 'number') && internalOperatorOption) {
8593
updateCondition({
8694
andIndex,
8795
fieldName: fieldOption.value,
@@ -98,6 +106,7 @@ export const Condition: React.FC<Props> = (props) => {
98106
orIndex,
99107
updateCondition,
100108
operator,
109+
removeCondition,
101110
])
102111

103112
const booleanSelect =

test/admin/e2e/list-view/e2e.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { reorderColumns } from '../../../helpers/e2e/reorderColumns.js'
4444
import { reInitializeDB } from '../../../helpers/reInitializeDB.js'
4545
import { POLL_TOPASS_TIMEOUT, TEST_TIMEOUT_LONG } from '../../../playwright.config.js'
4646
import { addListFilter } from 'helpers/e2e/addListFilter.js'
47+
4748
const filename = fileURLToPath(import.meta.url)
4849
const currentFolder = path.dirname(filename)
4950
const dirname = path.resolve(currentFolder, '../../')
@@ -356,6 +357,24 @@ describe('List View', () => {
356357
await expect(page.locator('.condition__value input')).toHaveValue('')
357358
})
358359

360+
test('should remove condition from URL when value is cleared', async () => {
361+
await page.goto(postsUrl.list)
362+
363+
await addListFilter({
364+
page,
365+
fieldLabel: 'Relationship',
366+
operatorLabel: 'equals',
367+
value: 'post1',
368+
})
369+
370+
await page.waitForURL(/&where/)
371+
372+
const valueInput = page.locator('.condition__value')
373+
const removeButton = valueInput.locator('.clear-indicator').click()
374+
375+
await page.waitForURL(/^(?!.*&where)/)
376+
})
377+
359378
test('should accept where query from valid URL where parameter', async () => {
360379
// delete all posts created by the seed
361380
await deleteAllPosts()

tsconfig.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
}
3232
],
3333
"paths": {
34-
"@payload-config": ["./test/fields/config.ts"],
34+
"@payload-config": ["./test/admin/config.ts"],
3535
"@payloadcms/live-preview": ["./packages/live-preview/src"],
3636
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],
3737
"@payloadcms/live-preview-vue": ["./packages/live-preview-vue/src/index.ts"],

0 commit comments

Comments
 (0)