2018-12-11 18:49:48 +01:00
|
|
|
// Libraries
|
2018-11-09 15:49:55 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
|
import _ from 'lodash';
|
2018-12-11 18:49:48 +01:00
|
|
|
|
|
|
|
|
// Components
|
2019-01-10 13:29:53 +01:00
|
|
|
import { Select } from '@grafana/ui';
|
2018-12-11 18:49:48 +01:00
|
|
|
|
|
|
|
|
// Types
|
2019-01-17 18:51:07 +01:00
|
|
|
import { DataSourceSelectItem } from '@grafana/ui/src/types';
|
2018-11-09 15:49:55 +01:00
|
|
|
|
2018-12-10 21:42:53 +01:00
|
|
|
export interface Props {
|
2018-12-14 14:05:47 +01:00
|
|
|
onChange: (ds: DataSourceSelectItem) => void;
|
2018-11-19 14:35:16 +01:00
|
|
|
datasources: DataSourceSelectItem[];
|
2018-12-11 17:03:38 +01:00
|
|
|
current: DataSourceSelectItem;
|
2018-12-12 08:33:49 +01:00
|
|
|
onBlur?: () => void;
|
|
|
|
|
autoFocus?: boolean;
|
2018-11-19 14:35:16 +01:00
|
|
|
}
|
2018-11-09 15:49:55 +01:00
|
|
|
|
2018-12-11 18:49:48 +01:00
|
|
|
export class DataSourcePicker extends PureComponent<Props> {
|
2018-12-12 08:33:49 +01:00
|
|
|
static defaultProps = {
|
|
|
|
|
autoFocus: false,
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-11 13:46:17 +01:00
|
|
|
searchInput: HTMLElement;
|
2018-11-09 15:49:55 +01:00
|
|
|
|
2018-12-11 13:46:17 +01:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
}
|
2018-11-09 15:49:55 +01:00
|
|
|
|
2018-12-11 18:49:48 +01:00
|
|
|
onChange = item => {
|
|
|
|
|
const ds = this.props.datasources.find(ds => ds.name === item.value);
|
2018-12-14 14:05:47 +01:00
|
|
|
this.props.onChange(ds);
|
2018-12-11 13:46:17 +01:00
|
|
|
};
|
2018-11-09 15:49:55 +01:00
|
|
|
|
2018-12-11 18:49:48 +01:00
|
|
|
render() {
|
2018-12-12 08:33:49 +01:00
|
|
|
const { datasources, current, autoFocus, onBlur } = this.props;
|
2018-11-09 15:49:55 +01:00
|
|
|
|
2018-12-11 18:49:48 +01:00
|
|
|
const options = datasources.map(ds => ({
|
|
|
|
|
value: ds.name,
|
|
|
|
|
label: ds.name,
|
2018-12-11 22:17:32 +01:00
|
|
|
imgUrl: ds.meta.info.logos.small,
|
2018-12-11 13:46:17 +01:00
|
|
|
}));
|
2018-11-09 15:49:55 +01:00
|
|
|
|
2018-12-12 08:33:49 +01:00
|
|
|
const value = current && {
|
2018-12-11 22:17:32 +01:00
|
|
|
label: current.name,
|
|
|
|
|
value: current.name,
|
|
|
|
|
imgUrl: current.meta.info.logos.small,
|
|
|
|
|
};
|
2018-12-11 18:49:48 +01:00
|
|
|
|
2018-12-11 13:46:17 +01:00
|
|
|
return (
|
2018-12-11 18:49:48 +01:00
|
|
|
<div className="gf-form-inline">
|
|
|
|
|
<Select
|
2018-12-14 16:27:48 +01:00
|
|
|
className="ds-picker"
|
2018-12-11 18:49:48 +01:00
|
|
|
isMulti={false}
|
|
|
|
|
isClearable={false}
|
2018-12-14 14:19:39 +01:00
|
|
|
backspaceRemovesValue={false}
|
2018-12-14 14:05:47 +01:00
|
|
|
onChange={this.onChange}
|
2018-12-11 18:49:48 +01:00
|
|
|
options={options}
|
2018-12-12 08:33:49 +01:00
|
|
|
autoFocus={autoFocus}
|
|
|
|
|
onBlur={onBlur}
|
|
|
|
|
openMenuOnFocus={true}
|
2018-12-11 18:49:48 +01:00
|
|
|
maxMenuHeight={500}
|
|
|
|
|
placeholder="Select datasource"
|
|
|
|
|
noOptionsMessage={() => 'No datasources found'}
|
|
|
|
|
value={value}
|
2018-12-11 13:46:17 +01:00
|
|
|
/>
|
2018-12-11 17:03:38 +01:00
|
|
|
</div>
|
2018-12-11 13:46:17 +01:00
|
|
|
);
|
2018-11-09 15:49:55 +01:00
|
|
|
}
|
2018-12-11 13:46:17 +01:00
|
|
|
}
|
2018-11-09 15:49:55 +01:00
|
|
|
|
2018-12-10 21:42:53 +01:00
|
|
|
export default DataSourcePicker;
|