UnitPicker.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import React, { PureComponent } from 'react';
  2. import Select from './Select';
  3. import kbn from 'app/core/utils/kbn';
  4. interface Props {
  5. onChange: (item: any) => void;
  6. defaultValue?: string;
  7. width?: number;
  8. }
  9. export default class UnitPicker extends PureComponent<Props> {
  10. static defaultProps = {
  11. width: 12,
  12. };
  13. render() {
  14. const { defaultValue, onChange, width } = this.props;
  15. const unitGroups = kbn.getUnitFormats();
  16. // Need to transform the data structure to work well with Select
  17. const groupOptions = unitGroups.map(group => {
  18. const options = group.submenu.map(unit => {
  19. return {
  20. label: unit.text,
  21. value: unit.value,
  22. };
  23. });
  24. return {
  25. label: group.text,
  26. options,
  27. };
  28. });
  29. const value = groupOptions.map(group => {
  30. return group.options.find(option => option.value === defaultValue);
  31. });
  32. return (
  33. <Select
  34. width={width}
  35. defaultValue={value}
  36. isSearchable={true}
  37. options={groupOptions}
  38. placeholder="Choose"
  39. onChange={onChange}
  40. />
  41. );
  42. }
  43. }