All files / lib/AccordionHeader index.js

100% Statements 8/8
100% Branches 14/14
100% Functions 3/3
100% Lines 8/8
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                                    28x 28x                   44x   44x   44x     66x                                   89x                             89x                  
/**
 * @category layout
 * @component accordion
 * @variations collab-ui-react
 */
 
import React from 'react';
import PropTypes from 'prop-types';
 
class AccordionHeader extends React.Component {
  static displayName = 'AccordionHeader';
 
  static contextTypes = {
    onClick: PropTypes.func,
    onKeyDown: PropTypes.func
  };
 
  componentDidUpdate(prevProps) {
    const { focus } = this.props;
    !prevProps.focus && focus && this.headerRef.focus();
  }
 
  render() {
    const {
      children,
      className,
      disabled,
      showSeparator,
      height,
    } = this.props;
 
    const { onClick, onKeyDown } = this.context;
 
    return (
      <div
        role='button'
        ref={ref => this.headerRef = ref}
        onClick={onClick}
        onKeyDown={onKeyDown}
        tabIndex={!disabled ? 0 : -1}
        className={
          'cui-accordion__header' +
          `${(showSeparator && ` cui-accordion__header--separator`) || ''}` +
          `${(height && ` cui-accordion__header--${height}`) || ''}` +
          `${(className && ` ${className}`) || ''}`
        }
      >
          {children}
          <span className="cui-accordion__header-icon"/>
      </div>
    );
  }
}
 
AccordionHeader.propTypes = {
  /** @prop Children nodes to render inside AccordionHeader | null  */
  children: PropTypes.node,
  /** @prop Optional css class string | '' */
  className: PropTypes.string,
  /** @prop Set the attribute disabled to the AccordionHeader | false */
  disabled: PropTypes.bool,
  /** @prop Specifies if AccordionHeader automatically gets focus when page loads | false  */
  focus: PropTypes.bool,
  /** @prop Optional underline under Accordion menu item | false */
  showSeparator: PropTypes.bool,
  /** @prop Set the height of the AccordionHeader to either the default or 56px | '' */
  height: PropTypes.oneOf(['', 56]),
};
 
AccordionHeader.defaultProps = {
  children: null,
  className: '',
  disabled: false,
  focus: false,
  showSeparator: true,
  height: '',
};
 
export default AccordionHeader;