# All files / src/Tabs Tabs.tsx

100% Statements 29/29
100% Branches 1/1
100% Functions 7/7
100% Lines 28/28

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

    2x
    2x
    2x
    2x
    2x

    2x

    2x

    2x

    2x

    2x

    2x

    4x

    2x

    2x

    4x

    2x
    2x

    2x

    4x

    2x

    4x

    2x

    2x

    2x
     import * as React from 'react'
    import classNames from 'classnames'
    import { createUseStyles } from '@fluent-ui/styles'
    import { styles } from './Tabs.styled'
    import { Theme } from '../styles'
    import { TabsProps, TabsType, TabsPropTypes, TabsClassProps } from './Tabs.type'
    import Panel from './components/Panel'
    import Navigation from '../Navigation'
    import Item from '../Item'
    import Box from '../Box'

    interface Values {
      value: string | number
      title: string
    }

    export const name = 'Tabs'

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

    const Tabs: React.FC<TabsProps> = React.forwardRef<HTMLDivElement, TabsProps>(
      (props, ref): React.ReactElement => {
        const { as = 'div', className: classNameProp, value, onChange, children, ...rest } = props

        const values = React.useMemo(
          (): Values[] =>
            React.Children.map(
              children,
              (child: React.ReactElement): Values => ({
                value: child.props.value,
                title: child.props.title
              })
            ),
          [children]
        )
        const theChildren = React.useMemo(
          (): React.ReactElement[] =>
            React.Children.map(
              children,
              (child: React.ReactElement): React.ReactElement =>
                React.cloneElement(child, { hidden: child.props.value !== value })
            ),
          [children, value]
        )

        const classes = useStyles(props)
        const className = classNames(classes.root, classNameProp)

        return (
          <Box className={className} ref={ref} {...rest}>
            <Navigation horizontal value={value} onChange={onChange} boxShadow="2" {...rest}>
              {values.map(
                ({ value, title }): React.ReactElement => (
                  <Item value={value} key={value}>
                    {title}
                  </Item>
                )
              )}
            </Navigation>
            <Box className={classes.panel}>{theChildren}</Box>
          </Box>
        )
      }
    )

    Object.defineProperty(Tabs, 'Panel', {
      get(): typeof Panel {
        return Panel
      }
    })

    Tabs.displayName = `F${name}`

    Tabs.propTypes = TabsPropTypes

    export default Tabs as TabsType