DataSourcePicker.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import _ from 'lodash';
  4. // Components
  5. import { Select } from '@grafana/ui';
  6. // Types
  7. import { DataSourceSelectItem } from '@grafana/ui/src/types';
  8. export interface Props {
  9. onChange: (ds: DataSourceSelectItem) => void;
  10. datasources: DataSourceSelectItem[];
  11. current: DataSourceSelectItem;
  12. onBlur?: () => void;
  13. autoFocus?: boolean;
  14. }
  15. export class DataSourcePicker extends PureComponent<Props> {
  16. static defaultProps = {
  17. autoFocus: false,
  18. };
  19. searchInput: HTMLElement;
  20. constructor(props) {
  21. super(props);
  22. }
  23. onChange = item => {
  24. const ds = this.props.datasources.find(ds => ds.name === item.value);
  25. this.props.onChange(ds);
  26. };
  27. render() {
  28. const { datasources, current, autoFocus, onBlur } = this.props;
  29. const options = datasources.map(ds => ({
  30. value: ds.name,
  31. label: ds.name,
  32. imgUrl: ds.meta.info.logos.small,
  33. }));
  34. const value = current && {
  35. label: current.name,
  36. value: current.name,
  37. imgUrl: current.meta.info.logos.small,
  38. };
  39. return (
  40. <div className="gf-form-inline">
  41. <Select
  42. className="ds-picker"
  43. isMulti={false}
  44. isClearable={false}
  45. backspaceRemovesValue={false}
  46. onChange={this.onChange}
  47. options={options}
  48. autoFocus={autoFocus}
  49. onBlur={onBlur}
  50. openMenuOnFocus={true}
  51. maxMenuHeight={500}
  52. placeholder="Select datasource"
  53. noOptionsMessage={() => 'No datasources found'}
  54. value={value}
  55. />
  56. </div>
  57. );
  58. }
  59. }
  60. export default DataSourcePicker;