TeamPicker.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import React, { Component } from 'react';
  2. import Select from 'react-select';
  3. import UserPickerOption from './UserPickerOption';
  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. }
  12. export interface Team {
  13. id: number;
  14. label: string;
  15. name: string;
  16. avatarUrl: string;
  17. }
  18. class TeamPicker extends Component<IProps, any> {
  19. debouncedSearch: any;
  20. backendSrv: any;
  21. constructor(props) {
  22. super(props);
  23. this.state = {};
  24. this.search = this.search.bind(this);
  25. // this.handleChange = this.handleChange.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 { text: ug.name, value: ug };
  37. return {
  38. id: team.id,
  39. label: team.name,
  40. name: team.name,
  41. avatarUrl: team.avatarUrl,
  42. };
  43. });
  44. toggleLoading(false);
  45. return { options: teams };
  46. });
  47. }
  48. render() {
  49. const AsyncComponent = this.state.creatable ? Select.AsyncCreatable : Select.Async;
  50. const { isLoading, handlePicked } = this.props;
  51. return (
  52. <div className="user-picker">
  53. <AsyncComponent
  54. valueKey="id"
  55. multi={this.state.multi}
  56. labelKey="label"
  57. cache={false}
  58. isLoading={isLoading}
  59. loadOptions={this.debouncedSearch}
  60. loadingPlaceholder="Loading..."
  61. onChange={handlePicked}
  62. className="width-8 gf-form-input gf-form-input--form-dropdown"
  63. optionComponent={UserPickerOption}
  64. placeholder="Choose"
  65. />
  66. </div>
  67. );
  68. }
  69. }
  70. export default withPicker(TeamPicker);