All files / lib/AccordionGroup index.js

100% Statements 12/12
100% Branches 15/15
100% Functions 5/5
100% Lines 12/12
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                                    37x             12x 1x           12x 12x 12x                 39x   39x 78x             39x                               89x                                     89x                      
/**
 * @category layout
 * @component accordion
 * @variations collab-ui-react
 */
 
import React from 'react';
import PropTypes from 'prop-types';
 
class AccordionGroup extends React.Component {
  static displayName = 'AccordionGroup';
 
  static childContextTypes = {
    onClick: PropTypes.func,
    onKeyDown: PropTypes.func,
  };
 
  getChildContext = () => {
    return {
      onClick: this.props.onClick,
      onKeyDown: this.props.onKeyDown,
    };
  }
 
  componentWillMount () {
    if(!this.verifyChildren()) {
      throw new Error('AccordionGroup should contain 2 children ' +
        'AccordionHeader and AccordionContent respectively.');
    }
  }
 
  verifyChildren = () => {
    const { children } = this.props;
    const childrenArr = React.Children.toArray(children);
    return (
      children &&
      childrenArr.length === 2 &&
      childrenArr[0].type.displayName === 'AccordionHeader' &&
      childrenArr[1].type.displayName === 'AccordionContent'
    );
  }
 
  render() {
    const { children, className, isExpanded, disabled, focus, showSeparator } = this.props;
 
    const setGroupContent = React.Children.map(children, (child) => {
      return React.cloneElement(child, {
        disabled,
        focus,
        showSeparator
      });
    });
 
    return (
      <div
        aria-expanded={isExpanded}
        className={
          `cui-accordion__group` +
          `${(disabled && ' cui-accordion__group--disabled') || ''}` +
          `${(isExpanded && ` cui-accordion__group--active`) || ''}` +
          `${(className && ` ${className}`) || ''}`
        }
      >
        {setGroupContent}
      </div>
    );
  }
}
 
AccordionGroup.propTypes = {
  /** @prop Children nodes to render inside Accordion | null  */
  children: PropTypes.node,
  /** @prop Set accordionGroup to be expanded | false  */
  isExpanded: PropTypes.bool,
  /** @prop Handler to be called when the user taps the AccordionGroup | null */
  onClick: PropTypes.func,
  /** @prop Handler to be called when the user presses a key | null */
  onKeyDown: PropTypes.func,
  /** @prop Optional css class string | '' */
  className: PropTypes.string,
  /** @prop Set the attribute disabled to the accordionGroup | false */
  disabled: PropTypes.bool,
  /** @prop Specifies if AccordionGroup show automatically get focus when page loads | false  */
  focus: PropTypes.bool,
  /** @prop Optional underline under Accordion menu item | false */
  showSeparator: PropTypes.bool,
};
 
AccordionGroup.defaultProps = {
  children: null,
  isExpanded: false,
  onClick: null,
  onKeyDown: null,
  className: '',
  disabled: false,
  focus: false,
  showSeparator: false,
};
 
export default AccordionGroup;