-
Notifications
You must be signed in to change notification settings - Fork 8.5k
handle EuiSearchBar query parse failures #25235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
2a48f85
534de7e
90bb21b
386c5f6
8481ba0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,26 @@ class TableUI extends PureComponent { | |
| onShowRelationships: PropTypes.func.isRequired, | ||
| }; | ||
|
|
||
| state = { | ||
| isSearchTextValid: true, | ||
| } | ||
|
|
||
| onChange = ({ query, error }) => { | ||
| if (error) { | ||
| this.setState({ | ||
| isSearchTextValid: false, | ||
| parseErrorMessage: error.message, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| this.setState({ | ||
| isSearchTextValid: true, | ||
| parseErrorMessage: null, | ||
| }); | ||
| this.props.onQueryChange({ query }); | ||
| } | ||
|
|
||
| render() { | ||
| const { | ||
| pageIndex, | ||
|
|
@@ -74,7 +94,6 @@ class TableUI extends PureComponent { | |
| onDelete, | ||
| onExport, | ||
| selectedSavedObjects, | ||
| onQueryChange, | ||
| onTableChange, | ||
| goInApp, | ||
| getEditUrl, | ||
|
|
@@ -182,11 +201,25 @@ class TableUI extends PureComponent { | |
| }, | ||
| ]; | ||
|
|
||
| let queryParseError; | ||
| if (!this.state.isSearchTextValid) { | ||
| const parseErrorMsg = intl.formatMessage({ | ||
| id: 'kbn.management.objects.objectsTable.table.searchBarUnableToParseQuery', | ||
|
||
| defaultMessage: 'Unable to parse query', | ||
| }); | ||
| queryParseError = ( | ||
| <div className="euiFormErrorText euiFormRow__text"> | ||
|
||
| {`${parseErrorMsg}. ${this.state.parseErrorMessage}`} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <Fragment> | ||
| <EuiSearchBar | ||
| box={{ ['data-test-subj']: 'savedObjectSearchBar' }} | ||
|
||
| filters={filters} | ||
| onChange={onQueryChange} | ||
| onChange={this.onChange} | ||
| toolsRight={[ | ||
| <EuiButton | ||
| key="deleteSO" | ||
|
|
@@ -213,6 +246,7 @@ class TableUI extends PureComponent { | |
| </EuiButton>, | ||
| ]} | ||
| /> | ||
| {queryParseError} | ||
| <EuiSpacer size="s" /> | ||
| <div data-test-subj="savedObjectsTable"> | ||
| <EuiBasicTable | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,58 +1,60 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`Search should render normally 1`] = ` | ||
| <EuiSearchBar | ||
| box={ | ||
| Object { | ||
| "aria-label": "Search advanced settings", | ||
| "incremental": true, | ||
| } | ||
| } | ||
| filters={ | ||
| Array [ | ||
| <React.Fragment> | ||
| <EuiSearchBar | ||
| box={ | ||
| Object { | ||
| "field": "category", | ||
| "multiSelect": "or", | ||
| "name": "Category", | ||
| "options": Array [ | ||
| Object { | ||
| "name": "General", | ||
| "value": "general", | ||
| }, | ||
| Object { | ||
| "name": "Dashboard", | ||
| "value": "dashboard", | ||
| }, | ||
| Object { | ||
| "name": "HiddenCategory", | ||
| "value": "hiddenCategory", | ||
| }, | ||
| Object { | ||
| "name": "X-pack", | ||
| "value": "x-pack", | ||
| "aria-label": "Search advanced settings", | ||
| "incremental": true, | ||
| } | ||
| } | ||
| filters={ | ||
| Array [ | ||
| Object { | ||
| "field": "category", | ||
| "multiSelect": "or", | ||
| "name": "Category", | ||
| "options": Array [ | ||
| Object { | ||
| "name": "General", | ||
| "value": "general", | ||
| }, | ||
| Object { | ||
| "name": "Dashboard", | ||
| "value": "dashboard", | ||
| }, | ||
| Object { | ||
| "name": "HiddenCategory", | ||
| "value": "hiddenCategory", | ||
| }, | ||
| Object { | ||
| "name": "X-pack", | ||
| "value": "x-pack", | ||
| }, | ||
| ], | ||
| "type": "field_value_selection", | ||
| }, | ||
| ] | ||
| } | ||
| onChange={[Function]} | ||
| query={ | ||
| Query { | ||
| "ast": _AST { | ||
| "_clauses": Array [], | ||
| "_indexedClauses": Object { | ||
| "field": Object {}, | ||
| "is": Object {}, | ||
| "term": Array [], | ||
| }, | ||
| ], | ||
| "type": "field_value_selection", | ||
| }, | ||
| ] | ||
| } | ||
| onChange={[Function]} | ||
| query={ | ||
| Query { | ||
| "ast": _AST { | ||
| "_clauses": Array [], | ||
| "_indexedClauses": Object { | ||
| "field": Object {}, | ||
| "is": Object {}, | ||
| "term": Array [], | ||
| }, | ||
| }, | ||
| "syntax": Object { | ||
| "parse": [Function], | ||
| "print": [Function], | ||
| }, | ||
| "text": "", | ||
| "syntax": Object { | ||
| "parse": [Function], | ||
| "print": [Function], | ||
| }, | ||
| "text": "", | ||
| } | ||
| } | ||
| } | ||
| /> | ||
| /> | ||
| </React.Fragment> | ||
| `; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
| * under the License. | ||
| */ | ||
|
|
||
| import React, { PureComponent } from 'react'; | ||
| import React, { Fragment, PureComponent } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { injectI18n } from '@kbn/i18n/react'; | ||
|
|
||
|
|
@@ -46,8 +46,28 @@ class SearchUI extends PureComponent { | |
| }); | ||
| } | ||
|
|
||
| state = { | ||
| isSearchTextValid: true, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same suggestion here as in the other component. |
||
| } | ||
|
|
||
| onChange = ({ query, error }) => { | ||
| if (error) { | ||
| this.setState({ | ||
| isSearchTextValid: false, | ||
| parseErrorMessage: error.message, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| this.setState({ | ||
| isSearchTextValid: true, | ||
| parseErrorMessage: null, | ||
| }); | ||
| this.props.onQueryChange({ query }); | ||
| } | ||
|
|
||
| render() { | ||
| const { query, onQueryChange, intl } = this.props; | ||
| const { query, intl } = this.props; | ||
|
|
||
| const box = { | ||
| incremental: true, | ||
|
|
@@ -71,14 +91,29 @@ class SearchUI extends PureComponent { | |
| } | ||
| ]; | ||
|
|
||
| return ( | ||
| <EuiSearchBar | ||
| box={box} | ||
| filters={filters} | ||
| onChange={onQueryChange} | ||
| query={query} | ||
| /> | ||
| let queryParseError; | ||
| if (!this.state.isSearchTextValid) { | ||
| const parseErrorMsg = intl.formatMessage({ | ||
| id: 'kbn.management.settings.searchBarUnableToParseQuery', | ||
| defaultMessage: 'Unable to parse query', | ||
| }); | ||
| queryParseError = ( | ||
| <div className="euiFormErrorText euiFormRow__text"> | ||
|
||
| {`${parseErrorMsg}. ${this.state.parseErrorMessage}`} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <Fragment> | ||
| <EuiSearchBar | ||
| box={box} | ||
| filters={filters} | ||
| onChange={this.onChange} | ||
| query={query} | ||
| /> | ||
| {queryParseError} | ||
| </Fragment> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,4 +55,27 @@ describe('Search', () => { | |
| component.find('input').simulate('keyup', { target: { value: 'new filter' } }); | ||
| expect(onQueryChange).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should handle query parse error', async () => { | ||
| const onQueryChangeMock = jest.fn(); | ||
| const component = mountWithIntl( | ||
| <Search.WrappedComponent | ||
| query={query} | ||
| categories={categories} | ||
| onQueryChange={onQueryChangeMock} | ||
| /> | ||
| ); | ||
|
|
||
| // Send invalid query | ||
| component.find('input').simulate('keyup', { target: { value: '?' } }); | ||
|
||
| expect(onQueryChangeMock).toHaveBeenCalledTimes(0); | ||
| expect(component.state().isSearchTextValid).toBe(false); | ||
|
|
||
| onQueryChangeMock.mockReset(); | ||
|
|
||
| // Send valid query to ensure component can recover from invalid query | ||
| component.find('input').simulate('keyup', { target: { value: 'dateFormat' } }); | ||
| expect(onQueryChangeMock).toHaveBeenCalledTimes(1); | ||
| expect(component.state().isSearchTextValid).toBe(true); | ||
| }); | ||
| }); | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe set
parseErrorMessage: nullhere, to give people one place to see all of the state properties in use?