# All files / src/FileList FileList.tsx

97.72% Statements 43/44
82.6% Branches 19/23
100% Functions 6/6
97.36% Lines 37/38

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
    131
    132
    133
    1343x
    3x
    3x
    3x
    3x
    3x
    3x

    3x

    3x
    38x

    3x
    72x

    3x
    48x

    3x

    8x
    8x
    8x
    8x
    8x
    8x
    8x

    8x
    8x

    8x

    8x

    8x

    48x
    48x
    48x

    3x
    23x
    23x
    23x
    23x

    23x

    23x

     import { Archive, Download } from '@procore/core-icons/dist'
    import React from 'react'
    import { Box } from '../Box/Box'
    import { Link } from '../Link/Link'
    import { Tooltip } from '../Tooltip/Tooltip'
    import { Typography } from '../Typography/Typography'
    import { useI18nContext } from '../_hooks/I18n'
    import { DivAttributes } from '../_utils/types'
    import {
      StyledFileListDownloadAll,
      StyledFileListItem,
      StyledFileListItemDownloadIcon,
      StyledFileListItemTextInactive,
    } from './FileList.styles'
    import { FileListItemProps, FileListProps } from './FileList.types'

    /**

     @since 10.19.0

     @see [Storybook](https://procore.github.io/core/core-react/latest/?path=/story/demos-filelist--demo)

     @see [Design Guidelines](https://design.procore.com/file-list)

     */

    export const defaultGetViewUrl = (item: FileListItemProps) => {
      return item.viewUrl
    }

    export const defaultGetDownloadUrl = (item: FileListItemProps) => {
      return item.downloadUrl
    }

    export const defaultGetName = (item: FileListItemProps) => {
      return item.name
    }

    export const FileList = React.forwardRef<
      HTMLDivElement,
      FileListProps & DivAttributes
    >(function FileList(
      {
        items,
        downloadAllUrl,
        getDownloadUrl = defaultGetDownloadUrl,
        getViewUrl = defaultGetViewUrl,
        getName = defaultGetName,
        qa,
        ...props
      },
      ref
    ) {
      const I18n = useI18nContext()
      const { length } = items

      const isDownloadAllVisible = length > 1 && downloadAllUrl

      Iif (length === 0) {
        return null
      }

      return (
        <Box ref={ref} {...props}>
          {isDownloadAllVisible && (
            <StyledFileListDownloadAll
              variant="tertiary"
              size="sm"
              icon={<Archive />}
              href={downloadAllUrl}
              tabIndex={0}
            >
              {I18n.t('core.fileList.downloadAll')}
            </StyledFileListDownloadAll>
          )}
          <Box as="ul">
            {items.map((item, i) => {
              const name = getName(item)
              const downloadUrl = getDownloadUrl(item)
              return (
                <Item
                  name={name}
                  downloadUrl={getDownloadUrl(item)}
                  viewUrl={getViewUrl(item)}
                  data-qa={qa?.item?.(item)}
                  key={`${name}-${downloadUrl}`}
                />
              )
            })}
          </Box>
        </Box>
      )
    })

    export function Item({
      name,
      viewUrl,
      downloadUrl,
      ...props
    }: FileListItemProps) {
      const I18n = useI18nContext()

      return (
        <StyledFileListItem {...props}>
          {viewUrl ? (
            <Typography intent="body" weight="semibold">
              <Link href={viewUrl} target="_blank" tabIndex={0}>
                {name}
              </Link>
            </Typography>
          ) : (
            <Tooltip overlay={I18n.t('core.fileList.noPreview')}>
              <Typography intent="body" color="gray15">
                <StyledFileListItemTextInactive tabIndex={0}>
                  {name}
                </StyledFileListItemTextInactive>
              </Typography>
            </Tooltip>
          )}
          {downloadUrl && (
            <Tooltip overlay={I18n.t('core.fileList.download')}>
              <StyledFileListItemDownloadIcon
                aria-label={I18n.t('core.fileList.download')}
                href={downloadUrl}
                icon={<Download size="sm" />}
                size="sm"
                variant="tertiary"
              />
            </Tooltip>
          )}
        </StyledFileListItem>
      )
    }