DataSourcePicker.tsx 2.3 KB

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