forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Maps] Autocomplete for custom color palettes and custom icon palettes (
elastic#56446) * [Maps] type ahead for stop values for custom color maps and custom icon maps * use Popover to show type ahead suggestions * datalist version * use EuiComboBox * clean up * wire ColorStopsCategorical to use StopInput component for autocomplete * clean up * cast suggestion values to string so boolean fields work * review feedback * fix problem with stall suggestions from previous field Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
3194cd0
commit 22fd2a9
Showing
15 changed files
with
264 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
x-pack/legacy/plugins/maps/public/layers/styles/vector/components/stop_input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import _ from 'lodash'; | ||
import React, { Component } from 'react'; | ||
|
||
import { EuiComboBox, EuiFieldText } from '@elastic/eui'; | ||
|
||
export class StopInput extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
suggestions: [], | ||
isLoadingSuggestions: false, | ||
hasPrevFocus: false, | ||
fieldDataType: undefined, | ||
localFieldTextValue: props.value, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this._isMounted = true; | ||
this._loadFieldDataType(); | ||
} | ||
|
||
componentWillUnmount() { | ||
this._isMounted = false; | ||
this._loadSuggestions.cancel(); | ||
} | ||
|
||
async _loadFieldDataType() { | ||
const fieldDataType = await this.props.field.getDataType(); | ||
if (this._isMounted) { | ||
this.setState({ fieldDataType }); | ||
} | ||
} | ||
|
||
_onFocus = () => { | ||
if (!this.state.hasPrevFocus) { | ||
this.setState({ hasPrevFocus: true }); | ||
this._onSearchChange(''); | ||
} | ||
}; | ||
|
||
_onChange = selectedOptions => { | ||
this.props.onChange(_.get(selectedOptions, '[0].label', '')); | ||
}; | ||
|
||
_onCreateOption = newValue => { | ||
this.props.onChange(newValue); | ||
}; | ||
|
||
_onSearchChange = async searchValue => { | ||
this.setState( | ||
{ | ||
isLoadingSuggestions: true, | ||
searchValue, | ||
}, | ||
() => { | ||
this._loadSuggestions(searchValue); | ||
} | ||
); | ||
}; | ||
|
||
_loadSuggestions = _.debounce(async searchValue => { | ||
let suggestions = []; | ||
try { | ||
suggestions = await this.props.getValueSuggestions(searchValue); | ||
} catch (error) { | ||
// ignore suggestions error | ||
} | ||
|
||
if (this._isMounted && searchValue === this.state.searchValue) { | ||
this.setState({ | ||
isLoadingSuggestions: false, | ||
suggestions, | ||
}); | ||
} | ||
}, 300); | ||
|
||
_onFieldTextChange = event => { | ||
this.setState({ localFieldTextValue: event.target.value }); | ||
// onChange can cause UI lag, ensure smooth input typing by debouncing onChange | ||
this._debouncedOnFieldTextChange(); | ||
}; | ||
|
||
_debouncedOnFieldTextChange = _.debounce(() => { | ||
this.props.onChange(this.state.localFieldTextValue); | ||
}, 500); | ||
|
||
_renderSuggestionInput() { | ||
const suggestionOptions = this.state.suggestions.map(suggestion => { | ||
return { label: `${suggestion}` }; | ||
}); | ||
|
||
const selectedOptions = []; | ||
if (this.props.value) { | ||
let option = suggestionOptions.find(({ label }) => { | ||
return label === this.props.value; | ||
}); | ||
if (!option) { | ||
option = { label: this.props.value }; | ||
suggestionOptions.unshift(option); | ||
} | ||
selectedOptions.push(option); | ||
} | ||
|
||
return ( | ||
<EuiComboBox | ||
options={suggestionOptions} | ||
selectedOptions={selectedOptions} | ||
singleSelection={{ asPlainText: true }} | ||
onChange={this._onChange} | ||
onSearchChange={this._onSearchChange} | ||
onCreateOption={this._onCreateOption} | ||
isClearable={false} | ||
isLoading={this.state.isLoadingSuggestions} | ||
onFocus={this._onFocus} | ||
compressed | ||
/> | ||
); | ||
} | ||
|
||
_renderTextInput() { | ||
return ( | ||
<EuiFieldText | ||
value={this.state.localFieldTextValue} | ||
onChange={this._onFieldTextChange} | ||
compressed | ||
/> | ||
); | ||
} | ||
|
||
render() { | ||
if (!this.state.fieldDataType) { | ||
return null; | ||
} | ||
|
||
// autocomplete service can not provide suggestions for non string fields (and boolean) because it uses | ||
// term aggregation include parameter. Include paramerter uses a regular expressions that only supports string type | ||
return this.state.fieldDataType === 'string' || this.state.fieldDataType === 'boolean' | ||
? this._renderSuggestionInput() | ||
: this._renderTextInput(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.