row.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import {memo, useEffect, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import AutoComplete from 'sentry/components/autoComplete';
  4. import space from 'sentry/styles/space';
  5. import {Item} from './types';
  6. type ItemSize = 'zero' | 'small';
  7. type AutoCompleteChildrenArgs<T> = Parameters<AutoComplete<T>['props']['children']>[0];
  8. type Props<T> = Pick<
  9. AutoCompleteChildrenArgs<T>,
  10. 'getItemProps' | 'registerVisibleItem' | 'inputValue'
  11. > &
  12. Omit<Parameters<AutoCompleteChildrenArgs<T>['getItemProps']>[0], 'index'> & {
  13. /**
  14. * Is the row 'active'
  15. */
  16. isHighlighted: boolean;
  17. /**
  18. * Size for dropdown items
  19. */
  20. itemSize?: ItemSize;
  21. /**
  22. * Style is used by react-virtualized for alignment
  23. */
  24. style?: React.CSSProperties;
  25. };
  26. function scrollIntoView(element: HTMLDivElement) {
  27. element?.scrollIntoView?.({block: 'nearest'});
  28. }
  29. function Row<T extends Item>({
  30. item,
  31. style,
  32. itemSize,
  33. isHighlighted,
  34. inputValue,
  35. getItemProps,
  36. registerVisibleItem,
  37. }: Props<T>) {
  38. const {index} = item;
  39. useEffect(() => registerVisibleItem(item.index, item), [registerVisibleItem, item]);
  40. const itemProps = useMemo(
  41. () => getItemProps({item, index}),
  42. [getItemProps, item, index]
  43. );
  44. if (item.groupLabel) {
  45. return (
  46. <LabelWithBorder style={style}>
  47. {item.label && <GroupLabel>{item.label}</GroupLabel>}
  48. </LabelWithBorder>
  49. );
  50. }
  51. return (
  52. <AutoCompleteItem
  53. itemSize={itemSize}
  54. disabled={item.disabled}
  55. isHighlighted={isHighlighted}
  56. style={style}
  57. ref={isHighlighted ? scrollIntoView : undefined}
  58. {...itemProps}
  59. >
  60. {typeof item.label === 'function' ? item.label({inputValue}) : item.label}
  61. </AutoCompleteItem>
  62. );
  63. }
  64. // XXX(epurkhiser): We memoize the row component since there will be many of
  65. // them, we do not want them re-rendering every time we change the
  66. // highlightedIndex in the parent List.
  67. export default memo(Row);
  68. const getItemPaddingForSize = (itemSize?: ItemSize) => {
  69. if (itemSize === 'small') {
  70. return `${space(0.5)} ${space(1)}`;
  71. }
  72. if (itemSize === 'zero') {
  73. return '0';
  74. }
  75. return space(1);
  76. };
  77. const LabelWithBorder = styled('div')`
  78. background-color: ${p => p.theme.backgroundSecondary};
  79. border-bottom: 1px solid ${p => p.theme.innerBorder};
  80. border-width: 1px 0;
  81. color: ${p => p.theme.subText};
  82. font-size: ${p => p.theme.fontSizeMedium};
  83. :first-child {
  84. border-top: none;
  85. }
  86. :last-child {
  87. border-bottom: none;
  88. }
  89. `;
  90. const GroupLabel = styled('div')`
  91. padding: ${space(0.25)} ${space(1)};
  92. `;
  93. const AutoCompleteItem = styled('div')<{
  94. isHighlighted: boolean;
  95. disabled?: boolean;
  96. itemSize?: ItemSize;
  97. }>`
  98. position: relative;
  99. /* needed for virtualized lists that do not fill parent height */
  100. /* e.g. breadcrumbs (org height > project, but want same fixed height for both) */
  101. display: flex;
  102. flex-direction: column;
  103. justify-content: center;
  104. scroll-margin: 20px 0;
  105. font-size: ${p => p.theme.fontSizeMedium};
  106. background-color: ${p => (p.isHighlighted ? p.theme.hover : 'transparent')};
  107. color: ${p => (p.isHighlighted ? p.theme.textColor : 'inherit')};
  108. padding: ${p => getItemPaddingForSize(p.itemSize)};
  109. cursor: ${p => (p.disabled ? 'not-allowed' : 'pointer')};
  110. border-bottom: 1px solid ${p => p.theme.innerBorder};
  111. :last-child {
  112. border-bottom: none;
  113. }
  114. :hover {
  115. color: ${p => p.theme.textColor};
  116. background-color: ${p => p.theme.hover};
  117. }
  118. `;