2019-10-17 05:38:09 -07:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
InputGroup,
|
|
|
|
InputGroupAddon,
|
|
|
|
InputGroupText,
|
|
|
|
Input,
|
|
|
|
} from 'reactstrap';
|
|
|
|
|
2019-10-20 13:52:29 -07:00
|
|
|
import Downshift, { ControllerStateAndHelpers } from 'downshift';
|
2019-10-17 05:38:09 -07:00
|
|
|
import fuzzy from 'fuzzy';
|
2019-10-17 11:52:24 -07:00
|
|
|
import SanitizeHTML from './components/SanitizeHTML';
|
2019-10-17 05:38:09 -07:00
|
|
|
|
|
|
|
import { library } from '@fortawesome/fontawesome-svg-core';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faSearch, faSpinner } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
|
|
|
|
library.add(faSearch, faSpinner);
|
|
|
|
|
|
|
|
interface ExpressionInputProps {
|
|
|
|
value: string;
|
|
|
|
metricNames: string[];
|
|
|
|
executeQuery: (expr: string) => void;
|
|
|
|
loading: boolean;
|
|
|
|
}
|
|
|
|
|
2019-10-20 13:52:29 -07:00
|
|
|
interface ExpressionInputState {
|
|
|
|
height: number | string;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ExpressionInput extends Component<ExpressionInputProps, ExpressionInputState> {
|
|
|
|
private prevNoMatchValue: string | null = null;
|
2019-10-17 05:38:09 -07:00
|
|
|
private exprInputRef = React.createRef<HTMLInputElement>();
|
|
|
|
|
2019-10-20 13:52:29 -07:00
|
|
|
constructor(props: ExpressionInputProps) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
value: props.value,
|
|
|
|
height: 'auto'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.setHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setHeight = () => {
|
|
|
|
const { offsetHeight, clientHeight, scrollHeight } = this.exprInputRef.current!;
|
|
|
|
const offset = offsetHeight - clientHeight; // Needed in order for the height to be more accurate.
|
|
|
|
this.setState({ height: scrollHeight + offset });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInput = () => {
|
|
|
|
this.setState({
|
|
|
|
height: 'auto',
|
|
|
|
value: this.exprInputRef.current!.value
|
|
|
|
}, this.setHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDropdownSelection = (value: string) => this.setState({ value });
|
|
|
|
|
2019-10-17 05:38:09 -07:00
|
|
|
handleKeyPress = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
if (event.key === 'Enter' && !event.shiftKey) {
|
|
|
|
this.props.executeQuery(this.exprInputRef.current!.value);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 13:52:29 -07:00
|
|
|
executeQuery = () => this.props.executeQuery(this.exprInputRef.current!.value)
|
2019-10-17 05:38:09 -07:00
|
|
|
|
2019-10-20 13:52:29 -07:00
|
|
|
renderAutosuggest = (downshift: ControllerStateAndHelpers<any>) => {
|
|
|
|
const { inputValue } = downshift
|
|
|
|
if (!inputValue || (this.prevNoMatchValue && inputValue.includes(this.prevNoMatchValue))) {
|
|
|
|
downshift.closeMenu();
|
2019-10-17 05:38:09 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-10-20 13:52:29 -07:00
|
|
|
const matches = fuzzy.filter(inputValue.replace(/ /g, ''), this.props.metricNames, {
|
2019-10-17 05:38:09 -07:00
|
|
|
pre: "<strong>",
|
|
|
|
post: "</strong>",
|
|
|
|
});
|
|
|
|
|
|
|
|
if (matches.length === 0) {
|
2019-10-20 13:52:29 -07:00
|
|
|
this.prevNoMatchValue = inputValue;
|
|
|
|
downshift.closeMenu();
|
2019-10-17 05:38:09 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className="autosuggest-dropdown" {...downshift.getMenuProps()}>
|
|
|
|
{
|
|
|
|
matches
|
|
|
|
.slice(0, 200) // Limit DOM rendering to 100 results, as DOM rendering is sloooow.
|
|
|
|
.map((item, index) => (
|
|
|
|
<li
|
|
|
|
{...downshift.getItemProps({
|
|
|
|
key: item.original,
|
|
|
|
index,
|
|
|
|
item: item.original,
|
|
|
|
style: {
|
|
|
|
backgroundColor:
|
|
|
|
downshift.highlightedIndex === index ? 'lightgray' : 'white',
|
|
|
|
fontWeight: downshift.selectedItem === item ? 'bold' : 'normal',
|
|
|
|
},
|
|
|
|
})}
|
|
|
|
>
|
2019-10-17 11:52:24 -07:00
|
|
|
<SanitizeHTML inline={true} allowedTags={['strong']}>
|
|
|
|
{item.string}
|
|
|
|
</SanitizeHTML>
|
2019-10-17 05:38:09 -07:00
|
|
|
</li>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-10-20 13:52:29 -07:00
|
|
|
const { value, height } = this.state;
|
2019-10-17 05:38:09 -07:00
|
|
|
return (
|
2019-10-17 11:52:24 -07:00
|
|
|
<Downshift
|
2019-10-20 13:52:29 -07:00
|
|
|
onChange={this.handleDropdownSelection}
|
|
|
|
inputValue={value}
|
2019-10-17 11:52:24 -07:00
|
|
|
>
|
|
|
|
{(downshift) => (
|
|
|
|
<div>
|
|
|
|
<InputGroup className="expression-input">
|
|
|
|
<InputGroupAddon addonType="prepend">
|
|
|
|
<InputGroupText>
|
|
|
|
{this.props.loading ? <FontAwesomeIcon icon="spinner" spin/> : <FontAwesomeIcon icon="search"/>}
|
|
|
|
</InputGroupText>
|
|
|
|
</InputGroupAddon>
|
|
|
|
<Input
|
2019-10-20 13:52:29 -07:00
|
|
|
onInput={this.handleInput}
|
|
|
|
style={{ height }}
|
2019-10-17 11:52:24 -07:00
|
|
|
autoFocus
|
|
|
|
type="textarea"
|
|
|
|
rows="1"
|
|
|
|
onKeyPress={this.handleKeyPress}
|
|
|
|
placeholder="Expression (press Shift+Enter for newlines)"
|
|
|
|
innerRef={this.exprInputRef}
|
|
|
|
{...downshift.getInputProps({
|
|
|
|
onKeyDown: (event: React.KeyboardEvent): void => {
|
|
|
|
switch (event.key) {
|
|
|
|
case 'Home':
|
|
|
|
case 'End':
|
|
|
|
// We want to be able to jump to the beginning/end of the input field.
|
|
|
|
// By default, Downshift otherwise jumps to the first/last suggestion item instead.
|
|
|
|
(event.nativeEvent as any).preventDownshiftDefault = true;
|
|
|
|
break;
|
|
|
|
case 'ArrowUp':
|
|
|
|
case 'ArrowDown':
|
|
|
|
if (!downshift.isOpen) {
|
2019-10-17 05:38:09 -07:00
|
|
|
(event.nativeEvent as any).preventDownshiftDefault = true;
|
2019-10-17 11:52:24 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Enter':
|
|
|
|
downshift.closeMenu();
|
|
|
|
break;
|
|
|
|
case 'Escape':
|
|
|
|
if (!downshift.isOpen) {
|
|
|
|
this.exprInputRef.current!.blur();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2019-10-17 05:38:09 -07:00
|
|
|
}
|
2019-10-17 11:52:24 -07:00
|
|
|
}
|
|
|
|
} as any)}
|
|
|
|
/>
|
|
|
|
<InputGroupAddon addonType="append">
|
|
|
|
<Button
|
|
|
|
className="execute-btn"
|
|
|
|
color="primary"
|
2019-10-20 13:52:29 -07:00
|
|
|
onClick={this.executeQuery}
|
2019-10-17 11:52:24 -07:00
|
|
|
>
|
|
|
|
Execute
|
|
|
|
</Button>
|
|
|
|
</InputGroupAddon>
|
|
|
|
</InputGroup>
|
2019-10-20 13:52:29 -07:00
|
|
|
{downshift.isOpen && this.renderAutosuggest(downshift)}
|
2019-10-17 11:52:24 -07:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Downshift>
|
2019-10-17 05:38:09 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ExpressionInput;
|