# All files / src/Table/components Cell.tsx

100% Statements 20/20
66.67% Branches 2/3
100% Functions 1/1
100% Lines 19/19

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

    2x
    2x

    2x

    2x

    2x

    32x

    32x
    32x

    32x
    32x

    32x

    2x

    2x

    2x

    2x
     import * as React from 'react'
    import * as PropTypes from 'prop-types'
    import { createUseStyles } from '@fluent-ui/styles'
    import { styles } from '../Table.styled'
    import { Theme } from '../../styles'
    import { TableCellProps, TableClassProps, TextAlign } from '../Table.type'
    import { TableContext } from './TableContext'
    import classNames from 'classnames'

    export const name = 'TableCell'

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

    const Cell: React.FC<TableCellProps> = React.forwardRef<HTMLTableCellElement, TableCellProps>(
      (props, ref): React.ReactElement => {
        const { className: classNameProp, textAlign = 'left', children, ...rest } = props

        const tableContext = React.useContext(TableContext)
        const Component = tableContext.variant === 'head' ? 'th' : 'td'

        const classes = useStyles(props)
        const className = classNames(
          {
            [classes.cellRoot]: tableContext.variant !== 'head',
            [classes.headCellRoot]: tableContext.variant === 'head',
            [classes.cellTextAlignLeft]: textAlign === 'left',
            [classes.cellTextAlignRight]: textAlign === 'right',
            [classes.cellTextAlignCenter]: textAlign === 'center',
            [classes.cellTextAlignJustify]: textAlign === 'justify'
          },
          classNameProp
        )
        return (
          <Component className={className} ref={ref} {...rest}>
            {children}
          </Component>
        )
      }
    )

    Cell.displayName = `F${name}`

    Cell.propTypes = {
      className: PropTypes.string,
      children: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.element]).isRequired,
      textAlign: PropTypes.oneOf<TextAlign>(['inherit', 'left', 'right', 'center', 'justify'])
    }

    Cell.defaultProps = {
      textAlign: 'left'
    }

    export default Cell