convertFromSelect2Choices.tsx 958 B

1234567891011121314151617181920212223242526
  1. import {Choices, SelectValue} from 'sentry/types';
  2. type Input = undefined | string[] | Choices;
  3. function isStringList(maybe: string[] | Choices): maybe is string[] {
  4. return typeof maybe[0] === 'string';
  5. }
  6. /**
  7. * Converts arg from a `select2` choices array to a `react-select` `options` array
  8. * This contains some any hacks as this is creates type errors with the generics
  9. * used in SelectControl as the generics conflict with the concrete types here.
  10. */
  11. const convertFromSelect2Choices = (choices: Input): SelectValue<any>[] | undefined => {
  12. // TODO(ts): This is to make sure that this function is backwards compatible, ideally,
  13. // this function only accepts arrays
  14. if (!Array.isArray(choices)) {
  15. return undefined;
  16. }
  17. if (isStringList(choices)) {
  18. return choices.map(choice => ({value: choice, label: choice}));
  19. }
  20. return choices.map(choice => ({value: choice[0], label: choice[1]}));
  21. };
  22. export default convertFromSelect2Choices;