StackdriverPicker.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import React from 'react';
  2. import _ from 'lodash';
  3. import Select from 'app/core/components/Select/Select';
  4. export interface Props {
  5. onChange: (value: string) => void;
  6. options: any[];
  7. searchable: boolean;
  8. selected: string;
  9. placeholder?: string;
  10. className?: string;
  11. groups?: boolean;
  12. }
  13. export class StackdriverPicker extends React.Component<Props, any> {
  14. constructor(props) {
  15. super(props);
  16. }
  17. extractOptions(options) {
  18. return options.length > 0 && options.every(o => o.options) ? _.flatten(options.map(o => o.options)) : options;
  19. }
  20. onChange = item => {
  21. const extractedOptions = this.extractOptions(this.props.options);
  22. const option = extractedOptions.find(option => option.value === item.value);
  23. this.props.onChange(option.value);
  24. };
  25. render() {
  26. const { options, selected, placeholder, className, searchable } = this.props;
  27. const extractedOptions = this.extractOptions(options);
  28. const selectedOption = extractedOptions.find(option => option.value === selected);
  29. return (
  30. <Select
  31. className={className}
  32. isMulti={false}
  33. isClearable={false}
  34. backspaceRemovesValue={false}
  35. onChange={this.onChange}
  36. options={options}
  37. autoFocus={false}
  38. isSearchable={searchable}
  39. openMenuOnFocus={true}
  40. maxMenuHeight={500}
  41. placeholder={placeholder}
  42. noOptionsMessage={() => 'No options found'}
  43. value={selectedOption}
  44. />
  45. );
  46. }
  47. }