DataSourcePicker.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React, { PureComponent } from 'react';
  2. import classNames from 'classnames';
  3. import _ from 'lodash';
  4. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  5. import { DataSourceSelectItem } from 'app/types';
  6. interface Props {}
  7. interface State {
  8. datasources: DataSourceSelectItem[];
  9. searchQuery: string;
  10. }
  11. export class DataSourcePicker extends PureComponent<Props, State> {
  12. searchInput: HTMLElement;
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. datasources: getDatasourceSrv().getMetricSources(),
  17. searchQuery: '',
  18. };
  19. }
  20. getDataSources() {
  21. const { datasources, searchQuery } = this.state;
  22. const regex = new RegExp(searchQuery, 'i');
  23. const filtered = datasources.filter(item => {
  24. return regex.test(item.name) || regex.test(item.meta.name);
  25. });
  26. return _.sortBy(filtered, 'sort');
  27. }
  28. renderDataSource = (ds: DataSourceSelectItem, index) => {
  29. const cssClass = classNames({
  30. 'ds-picker-list__item': true,
  31. });
  32. return (
  33. <div key={index} className={cssClass} title={ds.name}>
  34. <img className="ds-picker-list__img" src={ds.meta.info.logos.small} />
  35. <div className="ds-picker-list__name">{ds.name}</div>
  36. </div>
  37. );
  38. };
  39. componentDidMount() {
  40. setTimeout(() => {
  41. this.searchInput.focus();
  42. }, 300);
  43. }
  44. renderFilters() {
  45. return (
  46. <>
  47. <label className="gf-form--has-input-icon">
  48. <input
  49. type="text"
  50. className="gf-form-input width-13"
  51. placeholder=""
  52. ref={elem => (this.searchInput = elem)}
  53. />
  54. <i className="gf-form-input-icon fa fa-search" />
  55. </label>
  56. <div className="p-l-1">
  57. <button className="btn toggle-btn gf-form-btn active">All</button>
  58. <button className="btn toggle-btn gf-form-btn">Favorites</button>
  59. </div>
  60. </>
  61. );
  62. }
  63. render() {
  64. return (
  65. <>
  66. <div className="cta-form__bar">
  67. {this.renderFilters()}
  68. <div className="gf-form--grow" />
  69. </div>
  70. <div className="ds-picker-list">{this.getDataSources().map(this.renderDataSource)}</div>
  71. </>
  72. );
  73. }
  74. }