33import { useWindowInfo } from '@faceless-ui/window-info'
44import { getTranslation } from '@payloadcms/translations'
55import { validateWhereQuery } from 'payload/shared'
6- import React , { Fragment , useEffect , useRef , useState } from 'react'
6+ import React , { Fragment , useEffect , useMemo , useRef , useState } from 'react'
77
88import type { ListControlsProps } from './types.js'
99
@@ -74,9 +74,28 @@ export const ListControls: React.FC<ListControlsProps> = (props) => {
7474 i18n ,
7575 )
7676
77- const searchLabelTranslated = useRef (
78- t ( 'general:searchBy' , { label : getTranslation ( searchLabel , i18n ) } ) ,
79- )
77+ const searchLabelTranslated = useMemo ( ( ) => {
78+ if ( listSearchableFields ?. length > 0 ) {
79+ return listSearchableFields . reduce ( ( placeholderText : string , field , i : number ) => {
80+ const label =
81+ 'label' in field && field . label ? field . label : 'name' in field ? field . name : null
82+
83+ if ( i === 0 ) {
84+ return `${ t ( 'general:searchBy' , {
85+ label : getTranslation ( label , i18n ) ,
86+ } ) } `
87+ }
88+
89+ if ( i === listSearchableFields . length - 1 ) {
90+ return `${ placeholderText } ${ t ( 'general:or' ) } ${ getTranslation ( label , i18n ) } `
91+ }
92+
93+ return `${ placeholderText } , ${ getTranslation ( label , i18n ) } `
94+ } , '' )
95+ }
96+
97+ return t ( 'general:searchBy' , { label : getTranslation ( searchLabel , i18n ) } )
98+ } , [ t , listSearchableFields , i18n , searchLabel ] )
8099
81100 const hasWhereParam = useRef ( Boolean ( query ?. where ) )
82101
@@ -94,34 +113,6 @@ export const ListControls: React.FC<ListControlsProps> = (props) => {
94113 }
95114 } , [ setVisibleDrawer , query ?. where ] )
96115
97- useEffect ( ( ) => {
98- if ( listSearchableFields ?. length > 0 ) {
99- searchLabelTranslated . current = listSearchableFields . reduce (
100- ( placeholderText : string , field , i : number ) => {
101- const label =
102- 'label' in field && field . label ? field . label : 'name' in field ? field . name : null
103-
104- if ( i === 0 ) {
105- return `${ t ( 'general:searchBy' , {
106- label : getTranslation ( label , i18n ) ,
107- } ) } `
108- }
109-
110- if ( i === listSearchableFields . length - 1 ) {
111- return `${ placeholderText } ${ t ( 'general:or' ) } ${ getTranslation ( label , i18n ) } `
112- }
113-
114- return `${ placeholderText } , ${ getTranslation ( label , i18n ) } `
115- } ,
116- '' ,
117- )
118- } else {
119- searchLabelTranslated . current = t ( 'general:searchBy' , {
120- label : getTranslation ( searchLabel , i18n ) ,
121- } )
122- }
123- } , [ t , listSearchableFields , i18n , searchLabel ] )
124-
125116 return (
126117 < div className = { baseClass } >
127118 { collectionConfig ?. enableQueryPresets && ! disableQueryPresets && (
@@ -219,7 +210,7 @@ export const ListControls: React.FC<ListControlsProps> = (props) => {
219210 ) ,
220211 ] . filter ( Boolean ) }
221212 key = { collectionSlug }
222- label = { searchLabelTranslated . current }
213+ label = { searchLabelTranslated }
223214 onSearchChange = { handleSearchChange }
224215 searchQueryParam = { query ?. search }
225216 />
0 commit comments