TeamPicker.tsx 2.2 KB

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