# All files / src/Table Table.tsx

100% Statements 28/28
100% Branches 4/4
100% Functions 5/5
100% Lines 27/27

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
    692x
    2x
    2x

    2x
    2x

    2x
    2x
    2x
    2x

    1x

    4x

    1x

    3x

    12x
    12x
    12x

    1x

    2x

    2x

    2x
    2x

    2x
    2x
    2x

    2x

    2x

    2x
     import * as React from 'react'
    import classNames from 'classnames'
    import { createUseStyles } from '@fluent-ui/styles'
    import { Theme } from '../styles'
    import { styles } from './Table.styled'
    import { TableProps, ColumnsType, DataType, TablePropTypes, TableClassProps } from './Table.type'

    import Head from './components/Head'
    import Body from './components/Body'
    import Row from './components/Row'
    import Cell from './components/Cell'

    function getColumnsChildren<T extends DataType>(
      columns: ColumnsType<T>[],
      data: T[]
    ): React.ReactFragment {
      const theHeadChildren = columns.map(
        (cell): React.ReactElement => {
          return <Cell key={cell.field}>{cell.title}</Cell>
        }
      )
      const theBodyChildren = data.map(
        (row, index): React.ReactElement => (
          <Row key={row.key}>
            {columns.map(
              (cell): React.ReactElement => {
                const text = row[cell.field]
                const content = typeof cell.render === 'function' ? cell.render(text, row, index) : text
                return <Cell key={text}>{content}</Cell>
              }
            )}
          </Row>
        )
      )
      return (
        <>
          <Head>
            <Row>{theHeadChildren}</Row>
          </Head>
          <Body>{theBodyChildren}</Body>
        </>
      )
    }

    export const name = 'Table'

    const useStyles = createUseStyles<Theme, TableClassProps>(styles, { name })

    function Table<T extends DataType>(props: TableProps<T>): React.ReactElement {
      const { className: classNameProp, data, columns, children, ...rest }: TableProps<T> = props
      const theChildren = columns
        ? getColumnsChildren<T>(columns as ColumnsType<T>[], data as T[])
        : children

      const classes = useStyles(props)
      const className = classNames(classes.root, classNameProp)
      return (
        <table className={className} {...rest}>
          {theChildren}
        </table>
      )
    }

    Table.displayName = `F${name}`

    Table.propTypes = TablePropTypes

    export default Table