TeamPicker.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import React, { Component } from 'react';
  2. import Select from 'react-select';
  3. import PickerOption from './PickerOption';
  4. import withPicker from './withPicker';
  5. import { debounce } from 'lodash';
  6. export interface IProps {
  7. backendSrv: any;
  8. isLoading: boolean;
  9. toggleLoading: any;
  10. handlePicked: (user) => void;
  11. value?: string;
  12. }
  13. export interface Team {
  14. id: number;
  15. label: string;
  16. name: string;
  17. avatarUrl: string;
  18. }
  19. class TeamPicker extends Component<IProps, any> {
  20. debouncedSearch: any;
  21. backendSrv: any;
  22. constructor(props) {
  23. super(props);
  24. this.state = {};
  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 { toggleLoading, backendSrv } = this.props;
  33. toggleLoading(true);
  34. return backendSrv.get(`/api/teams/search?perpage=10&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. toggleLoading(false);
  44. return { options: teams };
  45. });
  46. }
  47. render() {
  48. const AsyncComponent = this.state.creatable ? Select.AsyncCreatable : Select.Async;
  49. const { isLoading, handlePicked, value } = this.props;
  50. return (
  51. <div className="user-picker">
  52. <AsyncComponent
  53. valueKey="id"
  54. multi={false}
  55. labelKey="label"
  56. cache={false}
  57. isLoading={isLoading}
  58. loadOptions={this.debouncedSearch}
  59. loadingPlaceholder="Loading..."
  60. onChange={handlePicked}
  61. className="width-8 gf-form-input gf-form-input--form-dropdown"
  62. optionComponent={PickerOption}
  63. placeholder="Choose"
  64. value={value}
  65. />
  66. </div>
  67. );
  68. }
  69. }
  70. export default withPicker(TeamPicker);