2018-12-18 11:25:13 +01:00
|
|
|
import React from 'react';
|
|
|
|
import _ from 'lodash';
|
2018-12-19 21:34:13 +01:00
|
|
|
|
2020-04-02 10:57:35 +02:00
|
|
|
import { LegacyForms } from '@grafana/ui';
|
2019-07-16 11:40:23 -07:00
|
|
|
import { SelectableValue } from '@grafana/data';
|
2018-12-19 21:34:13 +01:00
|
|
|
import { Variable } from 'app/types/templates';
|
2020-04-02 10:57:35 +02:00
|
|
|
const { Select } = LegacyForms;
|
2018-12-18 11:25:13 +01:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
onChange: (value: string) => void;
|
2019-07-16 11:40:23 -07:00
|
|
|
options: Array<SelectableValue<string>>;
|
2018-12-19 21:14:55 +01:00
|
|
|
isSearchable: boolean;
|
2018-12-19 21:19:43 +01:00
|
|
|
value: string;
|
2018-12-18 11:25:13 +01:00
|
|
|
placeholder?: string;
|
|
|
|
className?: string;
|
2018-12-19 21:34:13 +01:00
|
|
|
variables?: Variable[];
|
2018-12-18 11:25:13 +01:00
|
|
|
}
|
|
|
|
|
2018-12-18 16:01:12 +01:00
|
|
|
interface State {
|
2019-07-16 11:40:23 -07:00
|
|
|
options: Array<SelectableValue<string>>;
|
2018-12-18 16:01:12 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 21:44:38 +01:00
|
|
|
export class MetricSelect extends React.Component<Props, State> {
|
2019-05-12 14:15:23 +02:00
|
|
|
static defaultProps: Partial<Props> = {
|
2018-12-19 21:23:05 +01:00
|
|
|
variables: [],
|
2018-12-18 16:01:12 +01:00
|
|
|
options: [],
|
2019-01-02 12:18:15 +01:00
|
|
|
isSearchable: true,
|
2018-12-18 16:01:12 +01:00
|
|
|
};
|
|
|
|
|
2019-05-12 14:15:23 +02:00
|
|
|
constructor(props: Props) {
|
2018-12-18 11:25:13 +01:00
|
|
|
super(props);
|
2018-12-18 16:01:12 +01:00
|
|
|
this.state = { options: [] };
|
2018-12-18 11:25:13 +01:00
|
|
|
}
|
|
|
|
|
2018-12-18 16:01:12 +01:00
|
|
|
componentDidMount() {
|
|
|
|
this.setState({ options: this.buildOptions(this.props) });
|
2018-12-18 11:25:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-13 01:08:33 -07:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps: Props) {
|
2018-12-19 21:23:05 +01:00
|
|
|
if (nextProps.options.length > 0 || nextProps.variables.length) {
|
2018-12-18 16:01:12 +01:00
|
|
|
this.setState({ options: this.buildOptions(nextProps) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:09:32 +01:00
|
|
|
shouldComponentUpdate(nextProps: Props) {
|
2018-12-18 16:56:16 +01:00
|
|
|
const nextOptions = this.buildOptions(nextProps);
|
2018-12-19 21:19:43 +01:00
|
|
|
return nextProps.value !== this.props.value || !_.isEqual(nextOptions, this.state.options);
|
2018-12-18 16:01:12 +01:00
|
|
|
}
|
|
|
|
|
2019-05-12 14:15:23 +02:00
|
|
|
buildOptions({ variables = [], options }: Props) {
|
2019-01-02 12:21:30 +01:00
|
|
|
return variables.length > 0 ? [this.getVariablesGroup(), ...options] : options;
|
2018-12-18 16:01:12 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 21:23:05 +01:00
|
|
|
getVariablesGroup() {
|
2018-12-18 16:01:12 +01:00
|
|
|
return {
|
|
|
|
label: 'Template Variables',
|
2018-12-19 21:23:05 +01:00
|
|
|
options: this.props.variables.map(v => ({
|
2018-12-18 16:01:12 +01:00
|
|
|
label: `$${v.name}`,
|
|
|
|
value: `$${v.name}`,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getSelectedOption() {
|
|
|
|
const { options } = this.state;
|
|
|
|
const allOptions = options.every(o => o.options) ? _.flatten(options.map(o => o.options)) : options;
|
2018-12-19 21:19:43 +01:00
|
|
|
return allOptions.find(option => option.value === this.props.value);
|
2018-12-18 16:01:12 +01:00
|
|
|
}
|
2018-12-18 11:25:13 +01:00
|
|
|
|
|
|
|
render() {
|
2018-12-19 21:14:55 +01:00
|
|
|
const { placeholder, className, isSearchable, onChange } = this.props;
|
2018-12-18 16:01:12 +01:00
|
|
|
const { options } = this.state;
|
|
|
|
const selectedOption = this.getSelectedOption();
|
2018-12-18 11:25:13 +01:00
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
className={className}
|
|
|
|
isMulti={false}
|
|
|
|
isClearable={false}
|
|
|
|
backspaceRemovesValue={false}
|
2018-12-18 16:01:12 +01:00
|
|
|
onChange={item => onChange(item.value)}
|
2018-12-18 11:25:13 +01:00
|
|
|
options={options}
|
2018-12-19 21:14:55 +01:00
|
|
|
isSearchable={isSearchable}
|
2018-12-18 11:25:13 +01:00
|
|
|
maxMenuHeight={500}
|
|
|
|
placeholder={placeholder}
|
|
|
|
noOptionsMessage={() => 'No options found'}
|
|
|
|
value={selectedOption}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|