Files
grafana/public/app/core/components/Select/DataSourcePicker.tsx

75 lines
1.8 KiB
TypeScript
Raw Normal View History

2018-12-11 18:49:48 +01:00
// Libraries
2018-11-09 15:49:55 +01:00
import React, { PureComponent } from 'react';
2018-12-11 18:49:48 +01:00
// Components
import { Select } from '@grafana/ui';
import { SelectableValue } from '@grafana/data';
2018-12-11 18:49:48 +01:00
// Types
import { DataSourceSelectItem } from '@grafana/ui';
2018-11-09 15:49:55 +01:00
export interface Props {
2018-12-14 14:05:47 +01:00
onChange: (ds: DataSourceSelectItem) => void;
datasources: DataSourceSelectItem[];
2018-12-11 17:03:38 +01:00
current: DataSourceSelectItem;
2018-12-12 08:33:49 +01:00
onBlur?: () => void;
autoFocus?: boolean;
openMenuOnFocus?: boolean;
}
2018-11-09 15:49:55 +01:00
2018-12-11 18:49:48 +01:00
export class DataSourcePicker extends PureComponent<Props> {
static defaultProps: Partial<Props> = {
2018-12-12 08:33:49 +01:00
autoFocus: false,
openMenuOnFocus: false,
2018-12-12 08:33:49 +01:00
};
searchInput: HTMLElement;
2018-11-09 15:49:55 +01:00
constructor(props: Props) {
super(props);
}
2018-11-09 15:49:55 +01:00
onChange = (item: SelectableValue<string>) => {
2018-12-11 18:49:48 +01:00
const ds = this.props.datasources.find(ds => ds.name === item.value);
2018-12-14 14:05:47 +01:00
this.props.onChange(ds);
};
2018-11-09 15:49:55 +01:00
2018-12-11 18:49:48 +01:00
render() {
const { datasources, current, autoFocus, onBlur, openMenuOnFocus } = 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-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
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={openMenuOnFocus}
2018-12-11 18:49:48 +01:00
maxMenuHeight={500}
placeholder="Select datasource"
noOptionsMessage={() => 'No datasources found'}
value={value}
/>
2018-12-11 17:03:38 +01:00
</div>
);
2018-11-09 15:49:55 +01:00
}
}
2018-11-09 15:49:55 +01:00
export default DataSourcePicker;