selectorItems.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import {Item} from 'sentry/components/dropdownAutoComplete/types';
  2. import {DEFAULT_RELATIVE_PERIODS} from 'sentry/constants';
  3. import {t} from 'sentry/locale';
  4. import TimeRangeItemLabel from './timeRangeItemLabel';
  5. type Props = {
  6. children: (items: Item[]) => React.ReactElement;
  7. handleSelectRelative: (value: string) => void;
  8. relativePeriods?: Record<string, React.ReactNode>;
  9. shouldShowAbsolute?: boolean;
  10. shouldShowRelative?: boolean;
  11. };
  12. const SelectorItems = ({
  13. children,
  14. relativePeriods,
  15. shouldShowRelative,
  16. shouldShowAbsolute,
  17. }: Props) => {
  18. const relativeArr = Object.entries(relativePeriods ?? DEFAULT_RELATIVE_PERIODS);
  19. const items: Item[] = [
  20. ...(shouldShowRelative
  21. ? relativeArr.map(([value, itemLabel], index) => ({
  22. index,
  23. value,
  24. searchKey: typeof itemLabel === 'string' ? itemLabel : value,
  25. label: <TimeRangeItemLabel>{itemLabel}</TimeRangeItemLabel>,
  26. 'data-test-id': value,
  27. }))
  28. : []),
  29. ...(shouldShowAbsolute
  30. ? [
  31. {
  32. index: relativeArr.length,
  33. value: 'absolute',
  34. searchKey: 'absolute',
  35. label: <TimeRangeItemLabel>{t('Absolute date')}</TimeRangeItemLabel>,
  36. 'data-test-id': 'absolute',
  37. },
  38. ]
  39. : []),
  40. ];
  41. return children(items);
  42. };
  43. export default SelectorItems;