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

50.84% Statements 30/59
36.73% Branches 18/49
38.46% Functions 5/13
42.85% Lines 21/49

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
    1243x
    3x
    3x

    3x

    62x
    62x

    3x

    44x

    44x
    44x

    44x

    44x
    38x
    38x

    44x
    38x

    44x

    44x

    62x

    62x

    3x
     import { isFunction } from 'formik'
    import React from 'react'
    import { useListNavigation } from '../../_hooks/ListNavigation'
    import { DivAttributes } from '../../_utils/types'
    import { FileTokenEntry, FileTokenListProps } from './FileToken.types'
    import { StyledFileToken, StyledTokenList } from './FileTokenList.styles'

    function noop() {}

    function getProgress({ progress }: FileTokenEntry) {
      return progress ?? 100
    }

    function isUndefined(value: any): value is undefined {
      return typeof value === 'undefined'
    }

    export const FileTokenList = React.forwardRef<
      HTMLDivElement,
      FileTokenListProps & Omit<DivAttributes, 'onClick'>
    >(function FileTokenList(
      { tokens, onClick, onClose = noop, qa, onKeyDown, ...props },
      ref
    ) {
      const focusedTokenRef = React.useRef<HTMLDivElement>(null)
      const [isListFocused, setIsListFocused] = React.useState(false)

      const navigation = useListNavigation({
        circular: false,
        initialIndex: 0,
        size: tokens.length,
      })

      React.useEffect(() => {
        navigation.setSize(tokens.length)
        if (tokens.length === 0) setIsListFocused(false)
      }, [navigation, tokens.length])

      React.useEffect(() => {
        Iif (focusedTokenRef.current && isListFocused) {
          focusedTokenRef.current.focus()
        }
      }, [isListFocused, navigation])

      const onKeyDownHandler = React.useCallback(
        (e: React.KeyboardEvent<HTMLDivElement>) => {
          Iif (!isListFocused) return

          const token = tokens[navigation.index]

          switch (e.key) {
            case 'Backspace':
            case 'Delete':
              e.preventDefault()
              Iif (!e.repeat) {
                onClose(token)
                Iif (tokens.length >= 1) {
                  navigation.set(Math.max(0, navigation.index - 1))
                }
              }
              break
            case 'Space Bar':
            case ' ':
              e.preventDefault()
              const { error } = token

              Iif (isUndefined(error) && getProgress(token) === 100) {
                onClick?.(token)
              }
              break
            case 'ArrowUp':
            case 'Up':
            case 'ArrowLeft':
            case 'Left':
              e.preventDefault()
              navigation.decrement()
              break
            case 'ArrowDown':
            case 'Down':
            case 'ArrowRight':
            case 'Right':
              e.preventDefault()
              navigation.increment()
              break
            default:
          }
          onKeyDown && onKeyDown(e)
        },
        [isListFocused, navigation, onClick, onClose, onKeyDown, tokens]
      )

      return (
        <StyledTokenList
          {...props}
          ref={ref}
          onFocus={() => setIsListFocused(true)}
          onBlur={() => setIsListFocused(false)}
          onKeyDown={onKeyDownHandler}
        >
          {tokens.map((token, index) => {
            const isFocused = navigation.index === index

            return (
              <StyledFileToken
                data-qa={qa?.fileToken?.(token.id)}
                qa={qa}
                key={token.id}
                ref={isFocused ? focusedTokenRef : null}
                tabIndex={isFocused ? 0 : -1}
                progressValue={getProgress(token)}
                errorMessage={token.error}
                fileName={token.name}
                onFocus={() => navigation.set(index)}
                onLabelClick={isFunction(onClick) ? () => onClick(token) : onClick}
                onClose={() => onClose(token)}
              />
            )
          })}
        </StyledTokenList>
      )
    })

    FileTokenList.displayName = 'FileTokenList'