# All files / react/Accordion index.jsx

18.75% Statements 3/16
100% Branches 0/0
0% Functions 0/7
21.43% Lines 3/14

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

    1x

    1x

    1x

     import React, { Component } from 'react'
    import PropTypes from 'prop-types'
    import cx from 'classnames'
    import Icon from '../Icon'
    import styles from './styles.styl'

    class AccordionItem extends Component {
      constructor(props) {
        super(props)
        this.state = { selected: false }
      }

      render() {
        const { children, label, className } = this.props

        const { selected } = this.state

        const removeSpaceCharacters = str => str.replace(/\s+/g, '')

        const bodyName = removeSpaceCharacters(label)

        return (
          <div className={cx(styles['c-accordion-item'], className)}>
            <div
              className={cx(
                styles['c-accordion-title'],
                {
                  [styles['is-active']]: selected
                },
                className
              )}
              aria-selected={selected}
              aria-controls={bodyName}
              role="tab"
              tabIndex="0"
              onClick={() => this.toggleSelection()}
            >
              <Icon icon="forward" />
              {label}
            </div>
            <div
              className={cx(
                styles['c-accordion-body'],
                {
                  [styles['is-active']]: selected
                },
                className
              )}
              aria-hidden={!selected}
              aria-labelledby={bodyName}
              role="tabpanel"
            >
              {children}
            </div>
          </div>
        )
      }

      toggleSelection = () => {
        this.setState(previousState => ({ selected: !previousState.selected }))
      }
    }

    class Accordion extends Component {
      render() {
        const { children, className } = this.props
        return (
          <div className={cx(styles['c-accordion'], className)}>{children}</div>
        )
      }
    }

    Accordion.propTypes = {
      className: PropTypes.string
    }

    AccordionItem.propTypes = {
      label: PropTypes.string.isRequired,
      className: PropTypes.string
    }

    Object.assign(Accordion, { AccordionItem })

    export default Accordion

    export { AccordionItem }