DataSourcePicker.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import _ from 'lodash';
  4. // Components
  5. import ResetStyles from 'app/core/components/Picker/ResetStyles';
  6. import PickerOption from 'app/core/components/Picker/PickerOption';
  7. import IndicatorsContainer from 'app/core/components/Picker/IndicatorsContainer';
  8. import Select from 'react-select';
  9. // Types
  10. import { DataSourceSelectItem } from 'app/types';
  11. export interface Props {
  12. onChangeDataSource: (ds: DataSourceSelectItem) => void;
  13. datasources: DataSourceSelectItem[];
  14. current: DataSourceSelectItem;
  15. }
  16. export class DataSourcePicker extends PureComponent<Props> {
  17. searchInput: HTMLElement;
  18. constructor(props) {
  19. super(props);
  20. }
  21. onChange = item => {
  22. const ds = this.props.datasources.find(ds => ds.name === item.value);
  23. this.props.onChangeDataSource(ds);
  24. };
  25. render() {
  26. const { datasources, current, onChangeDatasource } = this.props;
  27. const options = datasources.map(ds => ({
  28. value: ds.name,
  29. label: ds.name,
  30. avatarUrl: ds.meta.info.logos.small,
  31. }));
  32. const value = { label: current.name, label: current.name };
  33. return (
  34. <div className="gf-form-inline">
  35. <Select
  36. classNamePrefix={`gf-form-select-box`}
  37. isMulti={false}
  38. menuShouldScrollIntoView={false}
  39. isClearable={false}
  40. className="gf-form-input gf-form-input--form-dropdown ds-picker"
  41. onChange={item => this.onChange(item)}
  42. options={options}
  43. styles={ResetStyles}
  44. maxMenuHeight={500}
  45. placeholder="Select datasource"
  46. loadingMessage={() => 'Loading datasources...'}
  47. noOptionsMessage={() => 'No datasources found'}
  48. value={value}
  49. components={{
  50. Option: PickerOption,
  51. IndicatorsContainer,
  52. }}
  53. />
  54. </div>
  55. );
  56. }
  57. }
  58. export default DataSourcePicker;