TeamPicker.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { Component } from 'react';
  2. import Select from 'react-select';
  3. import PickerOption from './PickerOption';
  4. import { debounce } from 'lodash';
  5. import { getBackendSrv } from 'app/core/services/backend_srv';
  6. export interface Props {
  7. onSelected: (team: Team) => void;
  8. value?: string;
  9. className?: string;
  10. }
  11. export interface State {
  12. isLoading;
  13. }
  14. export interface Team {
  15. id: number;
  16. label: string;
  17. name: string;
  18. avatarUrl: string;
  19. }
  20. export class TeamPicker extends Component<Props, State> {
  21. debouncedSearch: any;
  22. constructor(props) {
  23. super(props);
  24. this.state = { isLoading: false };
  25. this.search = this.search.bind(this);
  26. this.debouncedSearch = debounce(this.search, 300, {
  27. leading: true,
  28. trailing: false,
  29. });
  30. }
  31. search(query?: string) {
  32. const backendSrv = getBackendSrv();
  33. this.setState({ isLoading: true });
  34. return backendSrv.get(`/api/teams/search?perpage=50&page=1&query=${query}`).then(result => {
  35. const teams = result.teams.map(team => {
  36. return {
  37. id: team.id,
  38. label: team.name,
  39. name: team.name,
  40. avatarUrl: team.avatarUrl,
  41. };
  42. });
  43. this.setState({ isLoading: false });
  44. return { options: teams };
  45. });
  46. }
  47. render() {
  48. const { onSelected, value, className } = this.props;
  49. const { isLoading } = this.state;
  50. return (
  51. <div className="user-picker">
  52. <Select.Async
  53. valueKey="id"
  54. multi={false}
  55. labelKey="label"
  56. cache={false}
  57. isLoading={isLoading}
  58. loadOptions={this.debouncedSearch}
  59. loadingPlaceholder="Loading..."
  60. noResultsText="No teams found"
  61. onChange={onSelected}
  62. className={`gf-form-input gf-form-input--form-dropdown ${className || ''}`}
  63. optionComponent={PickerOption}
  64. placeholder="Select a team"
  65. value={value}
  66. autosize={true}
  67. />
  68. </div>
  69. );
  70. }
  71. }