diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternselectprops.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternselectprops.md index 5cfd5e1bc9929..e7d58f538c8ce 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternselectprops.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternselectprops.md @@ -10,7 +10,6 @@ export declare type IndexPatternSelectProps = Required, 'isLoading' | 'onSearchChange' | 'options' | 'selectedOptions' | 'onChange'>, 'placeholder'> & { onChange: (indexPatternId?: string) => void; indexPatternId: string; - fieldTypes?: string[]; onNoIndexPatterns?: () => void; }; ``` diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index 264428a40c438..aee1aebbdddb9 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -1564,7 +1564,6 @@ export type IndexPatternsContract = PublicMethodsOf; export type IndexPatternSelectProps = Required, 'isLoading' | 'onSearchChange' | 'options' | 'selectedOptions' | 'onChange'>, 'placeholder'> & { onChange: (indexPatternId?: string) => void; indexPatternId: string; - fieldTypes?: string[]; onNoIndexPatterns?: () => void; }; diff --git a/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx b/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx index 04bdb7a690268..2b45d8efc4d92 100644 --- a/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx +++ b/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx @@ -23,7 +23,6 @@ export type IndexPatternSelectProps = Required< > & { onChange: (indexPatternId?: string) => void; indexPatternId: string; - fieldTypes?: string[]; onNoIndexPatterns?: () => void; }; @@ -102,49 +101,18 @@ export default class IndexPatternSelect extends Component { - const isCurrentSearch = () => { - return this.isMounted && searchValue === this.state.searchValue; - }; - const idsAndTitles = await this.props.indexPatternService.getIdsWithTitle(); - if (!isCurrentSearch()) { + if (!this.isMounted || searchValue !== this.state.searchValue) { return; } const options = []; for (let i = 0; i < idsAndTitles.length; i++) { - if (!idsAndTitles[i].title.toLowerCase().includes(searchValue.toLowerCase())) { - // index pattern excluded due to title not matching search - continue; - } - - if (this.props.fieldTypes) { - try { - const indexPattern = await this.props.indexPatternService.get(idsAndTitles[i].id); - if (!isCurrentSearch()) { - return; - } - const hasRequiredFieldTypes = indexPattern.fields.some((field) => { - return this.props.fieldTypes!.includes(field.type); - }); - if (!hasRequiredFieldTypes) { - continue; - } - } catch (err) { - // could not load index pattern, exclude it from list. - continue; - } - } - - options.push({ - label: idsAndTitles[i].title, - value: idsAndTitles[i].id, - }); - - // Loading each index pattern object requires a network call so just find small number of matching index patterns - // Users can use 'searchValue' to further refine the list and locate their index pattern. - if (options.length > 15) { - break; + if (idsAndTitles[i].title.toLowerCase().includes(searchValue.toLowerCase())) { + options.push({ + label: idsAndTitles[i].title, + value: idsAndTitles[i].id, + }); } } @@ -174,7 +142,6 @@ export default class IndexPatternSelect extends Component

-

@@ -79,22 +71,19 @@ exports[`should render no index pattern warning when there are no matching index { test('should render no index pattern warning when there are no matching index patterns', async () => { const component = shallow( {}} value={'indexPatternId'} />); - component.setState({ noGeoIndexPatternsExist: true }); + component.setState({ noIndexPatternsExist: true }); expect(component).toMatchSnapshot(); }); diff --git a/x-pack/plugins/maps/public/components/geo_index_pattern_select.tsx b/x-pack/plugins/maps/public/components/geo_index_pattern_select.tsx index 81e06e4ea9bbc..3db7dd322ae1c 100644 --- a/x-pack/plugins/maps/public/components/geo_index_pattern_select.tsx +++ b/x-pack/plugins/maps/public/components/geo_index_pattern_select.tsx @@ -24,14 +24,16 @@ interface Props { } interface State { - noGeoIndexPatternsExist: boolean; + doesIndexPatternHaveGeoField: boolean; + noIndexPatternsExist: boolean; } export class GeoIndexPatternSelect extends Component { private _isMounted: boolean = false; state = { - noGeoIndexPatternsExist: false, + doesIndexPatternHaveGeoField: false, + noIndexPatternsExist: false, }; componentWillUnmount() { @@ -57,16 +59,23 @@ export class GeoIndexPatternSelect extends Component { // method may be called again before 'get' returns // ignore response when fetched index pattern does not match active index pattern if (this._isMounted && indexPattern.id === indexPatternId) { + this.setState({ + doesIndexPatternHaveGeoField: indexPattern.fields.some((field) => { + return this.props?.isGeoPointsOnly + ? (ES_GEO_FIELD_TYPE.GEO_POINT as string) === field.type + : ES_GEO_FIELD_TYPES.includes(field.type); + }), + }); this.props.onChange(indexPattern); } }; _onNoIndexPatterns = () => { - this.setState({ noGeoIndexPatternsExist: true }); + this.setState({ noIndexPatternsExist: true }); }; _renderNoIndexPatternWarning() { - if (!this.state.noGeoIndexPatternsExist) { + if (!this.state.noIndexPatternsExist) { return null; } @@ -74,7 +83,7 @@ export class GeoIndexPatternSelect extends Component { <> @@ -86,18 +95,14 @@ export class GeoIndexPatternSelect extends Component { -

{ render() { const IndexPatternSelect = getIndexPatternSelectComponent(); + const isIndexPatternInvalid = !!this.props.value && !this.state.doesIndexPatternHaveGeoField; + const error = isIndexPatternInvalid + ? i18n.translate('xpack.maps.noGeoFieldInIndexPattern.message', { + defaultMessage: 'Index pattern does not contain any geospatial fields', + }) + : ''; return ( <> {this._renderNoIndexPatternWarning()} @@ -122,17 +133,17 @@ export class GeoIndexPatternSelect extends Component { label={i18n.translate('xpack.maps.indexPatternSelectLabel', { defaultMessage: 'Index pattern', })} + isInvalid={isIndexPatternInvalid} + error={error} > diff --git a/x-pack/plugins/maps/public/components/single_field_select.tsx b/x-pack/plugins/maps/public/components/single_field_select.tsx index 32f4f0e7115c0..6727de35b1be7 100644 --- a/x-pack/plugins/maps/public/components/single_field_select.tsx +++ b/x-pack/plugins/maps/public/components/single_field_select.tsx @@ -114,7 +114,7 @@ export function SingleFieldSelect({ options={fieldsToOptions(fields, isFieldDisabled)} selectedOptions={selectedOptions} onChange={onSelection} - isDisabled={!fields} + isDisabled={!fields || fields.length === 0} renderOption={renderOption} {...rest} /> diff --git a/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/util_components/geo_index_pattern_select.tsx b/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/util_components/geo_index_pattern_select.tsx index 8d7528d6810bd..9bb29b65e7454 100644 --- a/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/util_components/geo_index_pattern_select.tsx +++ b/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/util_components/geo_index_pattern_select.tsx @@ -23,14 +23,16 @@ interface Props { } interface State { - noGeoIndexPatternsExist: boolean; + doesIndexPatternHaveGeoField: boolean; + noIndexPatternsExist: boolean; } export class GeoIndexPatternSelect extends Component { private _isMounted: boolean = false; state = { - noGeoIndexPatternsExist: false, + doesIndexPatternHaveGeoField: false, + noIndexPatternsExist: false, }; componentWillUnmount() { @@ -56,16 +58,21 @@ export class GeoIndexPatternSelect extends Component { // method may be called again before 'get' returns // ignore response when fetched index pattern does not match active index pattern if (this._isMounted && indexPattern.id === indexPatternId) { + this.setState({ + doesIndexPatternHaveGeoField: indexPattern.fields.some((field) => { + return this.props.includedGeoTypes.includes(field.type); + }), + }); this.props.onChange(indexPattern); } }; _onNoIndexPatterns = () => { - this.setState({ noGeoIndexPatternsExist: true }); + this.setState({ noIndexPatternsExist: true }); }; _renderNoIndexPatternWarning() { - if (!this.state.noGeoIndexPatternsExist) { + if (!this.state.noIndexPatternsExist) { return null; } @@ -73,7 +80,7 @@ export class GeoIndexPatternSelect extends Component { <> @@ -87,18 +94,14 @@ export class GeoIndexPatternSelect extends Component { > -

{ render() { const IndexPatternSelectComponent = this.props.IndexPatternSelectComponent; + const isIndexPatternInvalid = !!this.props.value && !this.state.doesIndexPatternHaveGeoField; + const error = isIndexPatternInvalid + ? i18n.translate('xpack.stackAlerts.geoContainment.noGeoFieldInIndexPattern.message', { + defaultMessage: 'Index pattern does not contain any geospatial fields', + }) + : ''; return ( <> {this._renderNoIndexPatternWarning()} @@ -125,10 +134,13 @@ export class GeoIndexPatternSelect extends Component { label={i18n.translate('xpack.stackAlerts.geoContainment.indexPatternSelectLabel', { defaultMessage: 'Index pattern', })} + isInvalid={isIndexPatternInvalid} + error={error} > {IndexPatternSelectComponent ? (