# All files / src/FileSelect/SourceItem SourceItem.tsx

100% Statements 46/46
96.42% Branches 27/28
100% Functions 4/4
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    1315x
    5x
    5x
    5x
    5x

    5x

    46x
    43x
    3x
    2x

    1x

    5x

    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x

    46x

    46x

    46x
    1x

    45x
    1x

    44x

    46x
    46x
    46x

    46x

    46x

    46x

     import { Error } from '@procore/core-icons/dist'
    import React from 'react'
    import { Spinner } from '../../Spinner/Spinner'
    import { Tooltip } from '../../Tooltip/Tooltip'
    import { OverflowObserver } from '../../_hooks/OverflowObserver'
    import { DivAttributes, Props } from '../../_utils/types'
    import { StyledIcon, StyledLabel, StyledSourceItem } from './SourceItem.styles'

    export interface SourceItemProps extends Props {
      /**
       * @since 10.19.0
       */
      icon?: React.ReactNode
      /**
       * @since 10.19.0
       */
      count?: number
      /**
       * @since 10.19.0
       */
      countTruncationThreshold?: number
      /**
       * @since 10.19.0
       */
      loading?: boolean
      /**
       * @since 10.19.0
       */
      error?: boolean
      /**
       * @since 10.19.0
       */
      selected?: boolean
      /**
       * @since 10.19.0
       */
      disabled?: boolean
      /**
       * @since 10.19.0
       */
      focused?: boolean
    }

    function formatCount(count: number, truncationThreshold: number): string {
      if (count === 0) {
        return ''
      } else if (count > truncationThreshold) {
        return `(${truncationThreshold}+)`
      } else {
        return `(${count})`
      }
    }

    export const SourceItem = React.forwardRef<
      HTMLDivElement,
      DivAttributes & SourceItemProps
    >(function SourceItem(
      {
        icon = '',
        count = 0,
        countTruncationThreshold = 99,
        loading = false,
        error = false,
        selected = false,
        disabled = false,
        focused = false,
        children,
        tabIndex = 0,
        ...props
      },
      ref
    ) {
      const tabIndexAttribute = !disabled ? { tabIndex } : {}

      const iconElement = (
        <StyledIcon>
          {(() => {
            if (error) {
              return <Error />
            }

            if (loading) {
              return <Spinner size="xs" />
            }

            return icon
          })()}
        </StyledIcon>
      )

      const labelElement = (
        <OverflowObserver>
          {({ isOverflowing, ref }: any) => {
            const item = <StyledLabel ref={ref}>{children}</StyledLabel>

            return isOverflowing ? (
              <Tooltip
                showDelay={500}
                trigger="hover"
                overlay={<Tooltip.Content>{children}</Tooltip.Content>}
              >
                {item}
              </Tooltip>
            ) : (
              item
            )
          }}
        </OverflowObserver>
      )

      const countElement = (
        <div>&nbsp;{formatCount(count, countTruncationThreshold)}</div>
      )

      return (
        <StyledSourceItem
          ref={ref}
          {...tabIndexAttribute}
          {...props}
          error={error}
          selected={selected}
          disabled={disabled}
          focused={focused}
        >
          {iconElement}
          {labelElement}
          {countElement}
        </StyledSourceItem>
      )
    })