All files / packages/design-system/ui/components/vertical-tabs index.jsx

94.11% Statements 16/17
92.85% Branches 13/14
66.66% Functions 2/3
94.11% Lines 16/17

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 88 89 90 91 92 93 94 95 96 97 98 99 100                3x   3x           3x 3x   3x                 9x   9x 9x 9x 9x 9x   9x                                                                         9x   9x 9x   9x                                          
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license
 
import React from 'react';
import classNames from 'classnames';
 
class VerticalTabs extends React.Component {
  constructor(props) {
    super(props);
 
    this.state = {
      currentTabIndex: 0
    };
  }
 
  render() {
    const { currentTabIndex } = this.state;
    const { itemFocused, tabs } = this.props;
 
    return (
      <div className="slds-vertical-tabs">
        <ul
          className="slds-vertical-tabs__nav"
          role="tablist"
          aria-orientation="vertical"
        >
          {tabs.map((tab, index) => {
            const isActive =
              index === currentTabIndex && !itemFocused ? 'slds-is-active' : '';
            const hasFocus =
              index === currentTabIndex && itemFocused ? 'slds-has-focus' : '';
            const tabIndex = index === currentTabIndex ? 0 : -1;
            const isAriaSelected = index === currentTabIndex ? 'true' : 'false';
            const ariaControlId = `slds-vertical-tabs-${index}`;
            const tabNavId = `slds-vertical-tabs-${index}__nav`;
 
            return (
              <li
                className={classNames(
                  'slds-vertical-tabs__nav-item',
                  isActive,
                  hasFocus
                )}
                title={tab.label}
                role="presentation"
                key={tabNavId}
              >
                <a
                  className="slds-vertical-tabs__link"
                  href="#"
                  role="tab"
                  tabIndex={tabIndex}
                  aria-selected={isAriaSelected}
                  aria-controls={ariaControlId}
                  id={tabNavId}
                  onClick={e => e.preventDefault()}
                >
                  <span className="slds-vertical-tabs__left-icon">
                    {tab.leftIcon}
                  </span>
                  <span className="slds-truncate" title={tab.label}>
                    {tab.label}
                  </span>
                  <span className="slds-vertical-tabs__right-icon">
                    {tab.rightIcon}
                  </span>
                </a>
              </li>
            );
          })}
        </ul>
 
        {tabs.map((tab, index) => {
          const tabContentId = `slds-vertical-tabs-${index}`;
          const showHideClass =
            index === currentTabIndex ? 'slds-show' : 'slds-hide';
          const ariaLabelledBy = `slds-vertical-tabs-${index}__nav`;
 
          return (
            <div
              className={classNames(
                'slds-vertical-tabs__content',
                showHideClass
              )}
              id={tabContentId}
              role="tabpanel"
              aria-labelledby={ariaLabelledBy}
              key={tabContentId}
            >
              {tabs[currentTabIndex].content}
            </div>
          );
        })}
      </div>
    );
  }
}
 
export default VerticalTabs;