1515 * <http://www.mongodb.com/licensing/server-side-public-license>.
1616 */
1717import React , { useEffect , useMemo } from 'react' ;
18- import { Formik } from 'formik' ;
18+ import { Formik , useFormikContext } from 'formik' ;
1919import { OrderedMap } from 'immutable' ;
2020import { v4 as uuidv4 } from 'uuid' ;
2121
@@ -27,9 +27,22 @@ type Props = {
2727 filters : SearchFilter [ ] ;
2828 onChange : ( filters : OrderedMap < string , SearchFilter > ) => void ;
2929 hideFiltersPreview ?: ( val : boolean ) => void ;
30+ queryString ?: string ;
3031} ;
3132
32- function SearchFiltersFormControls ( { filters, onChange, hideFiltersPreview = ( ) => { } } : Props ) {
33+ // Keeps the isolated Formik's `queryString` field in sync with the manually-entered query
34+ // living outside this form, without resetting `searchFilters` on every keystroke.
35+ const SyncQueryString = ( { queryString } : { queryString : string } ) => {
36+ const { setFieldValue } = useFormikContext ( ) ;
37+
38+ useEffect ( ( ) => {
39+ setFieldValue ( 'queryString' , queryString ) ;
40+ } , [ queryString , setFieldValue ] ) ;
41+
42+ return null ;
43+ } ;
44+
45+ function SearchFiltersFormControls ( { filters, onChange, hideFiltersPreview = ( ) => { } , queryString = '' } : Props ) {
3346 const searchFiltersPlugin = usePluginEntities ( 'eventDefinitions.components.searchForm' ) ?? [ ] ;
3447 const pluggableControls = searchFiltersPlugin . map ( ( controlFn ) => controlFn ( ) ) . filter ( ( control ) => ! ! control ) ;
3548
@@ -41,8 +54,8 @@ function SearchFiltersFormControls({ filters, onChange, hideFiltersPreview = ()
4154 filters . map ( ( filter ) => [ filter . id || uuidv4 ( ) , { frontendId : filter . id || uuidv4 ( ) , ...filter } ] ) ,
4255 ) ;
4356
44- return { searchFilters } ;
45- } , [ filters ] ) ;
57+ return { searchFilters, queryString } ;
58+ } , [ filters , queryString ] ) ;
4659
4760 if ( ! pluggableControls . length )
4861 return < SearchFilterBanner onHide = { ( ) => hideFiltersPreview ( true ) } pluggableControls = { pluggableControls } /> ;
@@ -58,7 +71,10 @@ function SearchFiltersFormControls({ filters, onChange, hideFiltersPreview = ()
5871
5972 return (
6073 < Formik onSubmit = { handleSearchFiltersChange } initialValues = { initialFilters } >
61- < SearchFiltersComponent />
74+ < >
75+ < SyncQueryString queryString = { queryString } />
76+ < SearchFiltersComponent />
77+ </ >
6278 </ Formik >
6379 ) ;
6480}
0 commit comments