All files / lib/Tab index.js

100% Statements 7/7
100% Branches 13/13
100% Functions 3/3
100% Lines 7/7
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                          21x                         37x           37x                 53x                                 89x                                         89x                     89x    
import React from 'react';
import PropTypes from 'prop-types';
 
/**
 * Tab is supplemental, clickable component used to help navigate content
 * @param props
 * @returns {XML}
 * @constructor
 */
 
class Tab extends React.PureComponent {
 
  componentDidUpdate() {
    this.props.focus && this.tabLink.focus();
  }
 
  render() {
    const {
      heading,
      active,
      onPress,
      onKeyDown,
      role,
      focus,
      className,
      disabled
      } = this.props;
 
    /* Due to Collab UI must keep anchor element instead of button
     eventhough accessibility would like the anchor element to be switched
     to a button. */
    /* eslint-disable */
    return (
      <li className={
      'cui-tab__item' +
      `${(className && ` ${className}`) || ''}` +
      `${active ? ' active' : ''}` +
      `${disabled ? ' disabled' : ''}`}
        {...(!disabled ? {tabIndex: '-1'} : {})}
        >
        <a
          ref={ref => (this.tabLink = ref)}
          role={role}
          href='javascript:void(0)'
          onKeyDown={onKeyDown}
          onClick={onPress}
          tabIndex={focus ? 0 : -1}
          aria-current={active}
          >
          {heading}
        </a>
      </li>
    );
  }
}
 
/* eslint-enable */
 
Tab.propTypes = {
  /** @prop Set Tab with an active state | false */
  active: PropTypes.bool,
  /** @prop Optional CSS class name */
  className: PropTypes.string,
  /** @prop Sets the attribute disabled to the Tab | false */
  disabled: PropTypes.bool,
  /** @prop Specifies if Tab should automatically get focus when page loads | false */
  focus: PropTypes.bool,
  /** @prop Tab Heading Text */
  heading: PropTypes.string.isRequired,
  /** @prop Currently unused prop myKey | 0 */
  myKey: PropTypes.number,
  /** @prop Callback function invoked when user presses a key | null */
  onKeyDown: PropTypes.func,
  /** @prop Callback function invoked when user presses on the Tab | null */
  onPress: PropTypes.func,
  /** @prop Tab's anchor role type | 'tab' */
  role: PropTypes.string,
};
 
Tab.defaultProps = {
  active: false,
  className: '',
  disabled: false,
  focus: false,
  myKey: 0,
  onKeyDown: null,
  onPress: null,
  role: 'tab',
};
 
Tab.displayName = 'Tab';
 
export default Tab;