DataSourcePicker.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. onBlur?: () => void;
  16. autoFocus?: boolean;
  17. }
  18. export class DataSourcePicker extends PureComponent<Props> {
  19. static defaultProps = {
  20. autoFocus: false,
  21. };
  22. searchInput: HTMLElement;
  23. constructor(props) {
  24. super(props);
  25. }
  26. onChange = item => {
  27. const ds = this.props.datasources.find(ds => ds.name === item.value);
  28. this.props.onChangeDataSource(ds);
  29. };
  30. render() {
  31. const { datasources, current, autoFocus, onBlur } = this.props;
  32. const options = datasources.map(ds => ({
  33. value: ds.name,
  34. label: ds.name,
  35. imgUrl: ds.meta.info.logos.small,
  36. }));
  37. const value = current && {
  38. label: current.name,
  39. value: current.name,
  40. imgUrl: current.meta.info.logos.small,
  41. };
  42. return (
  43. <div className="gf-form-inline">
  44. <Select
  45. classNamePrefix={`gf-form-select-box`}
  46. isMulti={false}
  47. menuShouldScrollIntoView={false}
  48. isClearable={false}
  49. className="gf-form-input gf-form-input--form-dropdown ds-picker"
  50. onChange={item => this.onChange(item)}
  51. options={options}
  52. styles={ResetStyles}
  53. autoFocus={autoFocus}
  54. onBlur={onBlur}
  55. openMenuOnFocus={true}
  56. maxMenuHeight={500}
  57. placeholder="Select datasource"
  58. loadingMessage={() => 'Loading datasources...'}
  59. noOptionsMessage={() => 'No datasources found'}
  60. value={value}
  61. components={{
  62. Option,
  63. SingleValue,
  64. IndicatorsContainer,
  65. }}
  66. />
  67. </div>
  68. );
  69. }
  70. }
  71. export default DataSourcePicker;