UnitPicker.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import React, { PureComponent } from 'react';
  2. import Select from 'react-select';
  3. import UnitGroup from './UnitGroup';
  4. import UnitOption from './UnitOption';
  5. import ResetStyles from '../ResetStyles';
  6. import kbn from '../../../utils/kbn';
  7. interface Props {
  8. onSelected: (item: any) => {} | void;
  9. defaultValue?: string;
  10. }
  11. export default class UnitPicker extends PureComponent<Props> {
  12. render() {
  13. const { defaultValue, onSelected } = this.props;
  14. const unitGroups = kbn.getUnitFormats();
  15. // Need to transform the data structure to work well with Select
  16. const groupOptions = unitGroups.map(group => {
  17. const options = group.submenu.map(unit => {
  18. return {
  19. label: unit.text,
  20. value: unit.value,
  21. };
  22. });
  23. return {
  24. label: group.text,
  25. options,
  26. };
  27. });
  28. const styles = {
  29. ...ResetStyles,
  30. menu: () => ({
  31. maxHeight: '75%',
  32. overflow: 'scroll',
  33. }),
  34. menuList: () =>
  35. ({
  36. overflowY: 'auto',
  37. position: 'relative',
  38. } as React.CSSProperties),
  39. };
  40. const value = groupOptions.map(group => {
  41. return group.options.find(option => option.value === defaultValue);
  42. });
  43. return (
  44. <Select
  45. classNamePrefix="gf-form-select-box"
  46. className="width-20 gf-form-input--form-dropdown"
  47. defaultValue={value}
  48. isSearchable={true}
  49. menuShouldScrollIntoView={false}
  50. options={groupOptions}
  51. placeholder="Choose"
  52. onChange={onSelected}
  53. components={{
  54. Group: UnitGroup,
  55. Option: UnitOption,
  56. }}
  57. styles={styles}
  58. />
  59. );
  60. }
  61. }