@@ -80,14 +80,19 @@ type InMemoryTableProps<T extends object> = Omit<
8080 */
8181 noItemsMessage ?: ReactNode ;
8282 /**
83- * Configures {@link Search}.
83+ * Configures the search bar. Can be `true` for defaults,
84+ * or an {@link EuiSearchBarProps} object.
85+ *
86+ * When `searchFormat="text"`, `query` and `defaultQuery` must be strings
87+ * ({@link Query} objects are ignored).
8488 */
8589 search ?: Search ;
8690 /**
8791 * By default, tables use `eql` format for search which allows using advanced filters.
8892 *
8993 * However, certain special characters (such as quotes, parentheses, and colons)
9094 * are reserved for EQL syntax and will error if used.
95+ *
9196 * If your table does not require filter search and instead requires searching for certain
9297 * symbols, use a plain `text` search format instead (note that filters will be ignored
9398 * in this format).
@@ -154,7 +159,7 @@ interface State<T extends object> {
154159 search ?: Search ;
155160 } ;
156161 search ?: Search ;
157- query : Query | null ;
162+ query : Query | string | null ;
158163 pageIndex : number ;
159164 pageSize ?: number ;
160165 pageSizeOptions ?: number [ ] ;
@@ -164,23 +169,34 @@ interface State<T extends object> {
164169 showPerPageOptions : boolean | undefined ;
165170}
166171
172+ /**
173+ * Extracts and formats a query from search props based on the search format
174+ * @param search - The search configuration
175+ * @param defaultQuery - Whether to use the defaultQuery property as fallback
176+ * @param searchFormat - The search format: 'eql' for parsed queries, 'text' for plain text
177+ * @returns Formatted query string or Query object
178+ */
167179const getQueryFromSearch = (
168180 search : Search | undefined ,
169- defaultQuery : boolean
170- ) => {
171- let query : Query | string ;
181+ defaultQuery : boolean ,
182+ searchFormat : InMemoryTableProps < { } > [ 'searchFormat' ]
183+ ) : Query | string => {
172184 if ( ! search ) {
173- query = '' ;
174- } else {
175- query =
176- ( defaultQuery
177- ? ( search as EuiSearchBarProps ) . defaultQuery ||
178- ( search as EuiSearchBarProps ) . query ||
179- ''
180- : ( search as EuiSearchBarProps ) . query ) || '' ;
185+ return searchFormat === 'text' ? '""' : '' ;
186+ }
187+
188+ const searchProps = search as EuiSearchBarProps ;
189+ const queryString = defaultQuery
190+ ? searchProps . defaultQuery ?? searchProps . query ?? ''
191+ : searchProps . query ?? '' ;
192+
193+ if ( searchFormat === 'text' ) {
194+ return `"${ queryString } "` ;
181195 }
182196
183- return isString ( query ) ? EuiSearchBar . Query . parse ( query ) : query ;
197+ return isString ( queryString )
198+ ? EuiSearchBar . Query . parse ( queryString )
199+ : queryString ;
184200} ;
185201
186202const getInitialPagination = (
@@ -393,7 +409,11 @@ export class EuiInMemoryTable<T extends object = object> extends Component<
393409 ...updatedPrevState . prevProps ,
394410 search : nextProps . search ,
395411 } ,
396- query : getQueryFromSearch ( nextProps . search , false ) ,
412+ query : getQueryFromSearch (
413+ nextProps . search ,
414+ false ,
415+ nextProps . searchFormat ?? 'eql'
416+ ) ,
397417 } ;
398418 }
399419 if ( updatedPrevState !== prevState ) {
@@ -418,7 +438,7 @@ export class EuiInMemoryTable<T extends object = object> extends Component<
418438 search,
419439 } ,
420440 search : search ,
421- query : getQueryFromSearch ( search , true ) ,
441+ query : getQueryFromSearch ( search , true , props . searchFormat ?? 'eql' ) ,
422442 pageIndex : pageIndex || 0 ,
423443 pageSize,
424444 pageSizeOptions,
@@ -537,13 +557,12 @@ export class EuiInMemoryTable<T extends object = object> extends Component<
537557 // search bar to ignore EQL syntax and only use the searchbar for plain text
538558 onPlainTextSearch = ( searchValue : string ) => {
539559 const escapedQueryText = searchValue . replace ( / [ " \\ ] / g, '\\$&' ) ;
540- const finalQuery = `"${ escapedQueryText } "` ;
541560 const { search } = this . props ;
542561
543562 if ( isEuiSearchBarProps ( search ) ) {
544563 if ( search . onChange ) {
545564 const shouldQueryInMemory = search . onChange ( {
546- query : EuiSearchBar . Query . parse ( finalQuery ) ,
565+ query : null ,
547566 queryText : escapedQueryText ,
548567 error : null ,
549568 } ) ;
@@ -554,7 +573,7 @@ export class EuiInMemoryTable<T extends object = object> extends Component<
554573 }
555574
556575 this . setState ( {
557- query : EuiSearchBar . Query . parse ( finalQuery ) ,
576+ query : `" ${ escapedQueryText } "` ,
558577 } ) ;
559578 } ;
560579
@@ -565,13 +584,37 @@ export class EuiInMemoryTable<T extends object = object> extends Component<
565584 let searchBar : ReactNode ;
566585
567586 if ( searchFormat === 'text' ) {
568- const _searchBoxProps = ( search as EuiSearchBarProps ) ?. box || { } ; // Work around | boolean type
569- const { schema, ...searchBoxProps } = _searchBoxProps ; // Destructure `schema` so it doesn't get rendered to DOM
587+ const { box = { } , query, defaultQuery } = search as EuiSearchBarProps ;
588+ const {
589+ schema, // destructure `schema` so it doesn't get rendered to DOM
590+ ...searchBoxProps
591+ } = box ;
592+
593+ // in the unexpected case a Query object is passed with searchFormat=text
594+ if ( process . env . NODE_ENV === 'development' ) {
595+ if ( query != null && ! isString ( query ) ) {
596+ console . warn (
597+ 'EuiInMemoryTable: `query` should be a string when using searchFormat="text". Query objects are only supported with searchFormat="eql".'
598+ ) ;
599+ }
600+ if ( defaultQuery != null && ! isString ( defaultQuery ) ) {
601+ console . warn (
602+ 'EuiInMemoryTable: `defaultQuery` should be a string when using searchFormat="text". Query objects are only supported with searchFormat="eql".'
603+ ) ;
604+ }
605+ }
606+
607+ // use only string values, ignore Query objects
608+ const displayQuery = isString ( query )
609+ ? query
610+ : isString ( defaultQuery )
611+ ? defaultQuery
612+ : '' ;
570613
571614 searchBar = (
572615 < EuiSearchBox
573- query = "" // Unused, passed to satisfy Typescript
574616 { ...searchBoxProps }
617+ query = { displayQuery }
575618 onSearch = { this . onPlainTextSearch }
576619 />
577620 ) ;
0 commit comments