# All files / components/Accordion Accordion.js

18.18% Statements 2/11
0% Branches 0/6
0% Functions 0/3
18.18% Lines 2/11

    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

    4x

    4x

     import React from 'react';
    import PropTypes from 'prop-types';
    import { withProps } from 'recompose';

    import ExpandToggle from '../../behaviors/ExpandToggle';
    import AccordionLabel from './styles/AccordionLabel';
    import AccordionArrow from './styles/AccordionArrow';
    import AccordionBorder from './styles/AccordionBorder';
    import AccordionLabelText from './styles/AccordionLabelText';
    import AccordionContent from './styles/AccordionContent';
    import AccordionGroup from './AccordionGroup';

    /**
     * Accordion works like a controllable component. Provide the
     * isExpanded prop to control it, or don't to let it control itself.
     */
    class Accordion extends React.Component {
      static propTypes = {
        /**
         * Content to render when the accordion is collapsed, and in the header of the expanded state.
         */
        label: PropTypes.node.isRequired,
        /**
         * Content inside the collapsible part of the accordion.
         */
        children: PropTypes.node.isRequired,
        /**
         * Pass isExpanded to override the internal collapsing state
         */
        isExpanded: PropTypes.bool,
        /**
         * DEPRECATED: the negation of isExpanded, overrides internal collapse state
         */
        isCollapsed: PropTypes.bool,
        /**
         * Add a handler for when the accordion is collapsed or expanded.
         */
        onToggle: PropTypes.func,
        /**
         * Set a classname for the accordion container element.
         */
        className: PropTypes.string,
        /**
         * Set an id for the accordion container element.
         */
        id: PropTypes.string,
        /**
         * A component to render the border
         */
        Border: PropTypes.func,
        /**
         * A component to render the label
         */
        Label: PropTypes.func,
        /**
         * A component to render the arrow
         */
        Arrow: PropTypes.func,
        /**
         * A component to render the text inside the label
         */
        LabelText: PropTypes.func,
        /**
         * A component to render the content area
         */
        Content: PropTypes.func,
      };

      static defaultProps = {
        isExpanded: null,
        isCollapsed: null,
        onToggle: null,
        className: null,
        id: null,
        Border: AccordionBorder,
        Label: AccordionLabel,
        Arrow: AccordionArrow,
        LabelText: AccordionLabelText,
        Content: AccordionContent,
      };

      coalesceIsExpandedProps = () => {
        const { isExpanded, isCollapsed } = this.props;
        if (isExpanded === null && isCollapsed === null) {
          return null;
        }
        if (isExpanded === null) {
          return !isCollapsed;
        }
        return isExpanded;
      };

      renderLabel = isExpanded => (
        <this.props.Label onClick={this.handleToggle}>
          <this.props.Arrow isExpanded={isExpanded} name="forward" size={21} />
          <this.props.LabelText>{this.props.label}</this.props.LabelText>
        </this.props.Label>
      );

      render() {
        const { id, className, onToggle, children, Border, Content } = this.props;

        return (
          <Border>
            <ExpandToggle
              id={id}
              className={className}
              onToggle={onToggle}
              toggleContent={this.renderLabel}
              isExpanded={this.coalesceIsExpandedProps()}
            >
              <Content>{children}</Content>
            </ExpandToggle>
          </Border>
        );
      }
    }

    Accordion.Small = withProps({
      Border: AccordionBorder.Small,
      Arrow: AccordionArrow.Small,
      Label: AccordionLabel.Small,
      Content: AccordionContent.Small,
    })(Accordion);
    Accordion.Group = AccordionGroup;

    export default Accordion;