UnitPicker.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import React, { PureComponent } from 'react';
  2. import Select from 'react-select';
  3. import ResetStyles from 'app/core/components/Picker/ResetStyles';
  4. import DescriptionOption from './DescriptionOption';
  5. import kbn from '../../utils/kbn';
  6. interface Props {
  7. width: number;
  8. className?: string;
  9. onSelected: (item: any) => {} | void;
  10. placeholder?: string;
  11. }
  12. export class UnitPicker extends PureComponent<Props> {
  13. formatGroupLabel = data => {
  14. console.log('format', data);
  15. const groupStyles = {
  16. display: 'flex',
  17. alignItems: 'center',
  18. justifyContent: 'space-between',
  19. };
  20. const groupBadgeStyles = {
  21. backgroundColor: '#EBECF0',
  22. borderRadius: '2em',
  23. color: '#172B4D',
  24. display: 'inline-block',
  25. fontSize: 12,
  26. fontWeight: 400,
  27. lineHeight: '1',
  28. minWidth: 1,
  29. padding: '0.16666666666667em 0.5em',
  30. textAlign: 'center',
  31. } as React.CSSProperties;
  32. return (
  33. <div style={groupStyles}>
  34. <span>{data.label}</span>
  35. <span style={groupBadgeStyles}>{data.submenu.length}</span>
  36. </div>
  37. );
  38. };
  39. render() {
  40. const { className, onSelected, placeholder, width } = this.props;
  41. const options = kbn.getUnitFormats();
  42. return (
  43. <Select
  44. classNamePrefix="gf-form-select-box"
  45. className={`width-${width} gf-form-input gf-form-input--form-dropdown ${className || ''}`}
  46. components={{
  47. Option: DescriptionOption,
  48. }}
  49. getOptionLabel={i => i.text}
  50. getOptionValue={i => i.value}
  51. isSearchable={false}
  52. onChange={onSelected}
  53. options={options}
  54. placeholder={placeholder || 'Choose'}
  55. styles={ResetStyles}
  56. formatGroupLabel={this.formatGroupLabel}
  57. />
  58. );
  59. }
  60. }