Skip to content

Commit a2b92aa

Browse files
authored
fix(ui): watch "where" query param inside route and reset WhereBuilder (#6184)
1 parent 544a228 commit a2b92aa

File tree

1 file changed

+14
-2
lines changed
  • packages/ui/src/elements/ListControls

1 file changed

+14
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use client'
2-
import type { ClientCollectionConfig, FieldAffectingData, Where } from 'payload/types'
2+
import type { ClientCollectionConfig, Where } from 'payload/types'
33

44
import * as facelessUIImport from '@faceless-ui/window-info'
55
import { getTranslation } from '@payloadcms/translations'
6-
import React, { useState } from 'react'
6+
import React, { useEffect, useRef, useState } from 'react'
77
import AnimateHeightImport from 'react-animate-height'
88

99
const AnimateHeight = (AnimateHeightImport.default ||
@@ -59,11 +59,22 @@ export const ListControls: React.FC<ListControlsProps> = (props) => {
5959
breakpoints: { s: smallBreak },
6060
} = useWindowInfo()
6161

62+
const hasWhereParam = useRef(Boolean(searchParams?.where))
63+
6264
const shouldInitializeWhereOpened = validateWhereQuery(searchParams?.where)
6365
const [visibleDrawer, setVisibleDrawer] = useState<'columns' | 'sort' | 'where'>(
6466
shouldInitializeWhereOpened ? 'where' : undefined,
6567
)
6668

69+
useEffect(() => {
70+
if (hasWhereParam.current && !searchParams?.where) {
71+
setVisibleDrawer(undefined)
72+
hasWhereParam.current = false
73+
} else if (searchParams?.where) {
74+
hasWhereParam.current = true
75+
}
76+
}, [setVisibleDrawer, searchParams?.where])
77+
6778
return (
6879
<div className={baseClass}>
6980
<div className={`${baseClass}__wrap`}>
@@ -156,6 +167,7 @@ export const ListControls: React.FC<ListControlsProps> = (props) => {
156167
<WhereBuilder
157168
collectionPluralLabel={collectionConfig?.labels?.plural}
158169
collectionSlug={collectionConfig.slug}
170+
key={String(hasWhereParam.current && !searchParams?.where)}
159171
/>
160172
</AnimateHeight>
161173
{enableSort && (

0 commit comments

Comments
 (0)