DataSourcePicker.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 { Option, SingleValue } 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. imgUrl: ds.meta.info.logos.small,
  31. }));
  32. const value = {
  33. label: current.name,
  34. value: current.name,
  35. imgUrl: current.meta.info.logos.small,
  36. };
  37. return (
  38. <div className="gf-form-inline">
  39. <Select
  40. classNamePrefix={`gf-form-select-box`}
  41. isMulti={false}
  42. menuShouldScrollIntoView={false}
  43. isClearable={false}
  44. className="gf-form-input gf-form-input--form-dropdown ds-picker"
  45. onChange={item => this.onChange(item)}
  46. options={options}
  47. styles={ResetStyles}
  48. maxMenuHeight={500}
  49. placeholder="Select datasource"
  50. loadingMessage={() => 'Loading datasources...'}
  51. noOptionsMessage={() => 'No datasources found'}
  52. value={value}
  53. components={{
  54. Option,
  55. SingleValue,
  56. IndicatorsContainer,
  57. }}
  58. />
  59. </div>
  60. );
  61. }
  62. }
  63. export default DataSourcePicker;