OptionGroupPicker.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import React from 'react';
  2. import Select from 'react-select';
  3. import _ from 'lodash';
  4. import GroupHeading from 'app/core/components/Picker/GroupHeading';
  5. import DescriptionOption from 'app/core/components/Picker/DescriptionOption';
  6. import IndicatorsContainer from 'app/core/components/Picker/IndicatorsContainer';
  7. import ResetStyles from 'app/core/components/Picker/ResetStyles';
  8. import NoOptionsMessage from 'app/core/components/Picker/NoOptionsMessage';
  9. export interface Props {
  10. onChange: (value: string) => void;
  11. groups: any[];
  12. searchable: boolean;
  13. selected: string;
  14. placeholder?: string;
  15. className?: string;
  16. }
  17. export class OptionGroupPicker extends React.Component<Props, any> {
  18. constructor(props) {
  19. super(props);
  20. }
  21. render() {
  22. const { onChange, groups, selected, placeholder, className, searchable } = this.props;
  23. const options = _.flatten(groups.map(o => o.options));
  24. const selectedOption = options.find(option => option.value === selected);
  25. return (
  26. <Select
  27. placeholder={placeholder}
  28. classNamePrefix={`gf-form-select-box`}
  29. className={className}
  30. options={groups}
  31. components={{
  32. GroupHeading,
  33. Option: DescriptionOption,
  34. IndicatorsContainer,
  35. NoOptionsMessage,
  36. }}
  37. styles={ResetStyles}
  38. isSearchable={searchable}
  39. maxMenuHeight={50}
  40. onChange={option => onChange(option.value)}
  41. getOptionValue={i => i.value}
  42. getOptionLabel={i => i.label}
  43. value={selectedOption}
  44. noOptionsMessage={() => 'No metrics found'}
  45. />
  46. );
  47. }
  48. }