All files / tabs tab.js

100% Statements 20/20
95.24% Branches 20/21
71.43% Functions 5/7
100% Lines 20/20

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                                                7x 7x 1x   6x 6x 6x       4x 4x 2x   2x   2x 2x   2x       37x 37x               37x 37x 37x 37x   37x                                      
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
import {getOverrides} from '../helpers/overrides.js';
import {Tab as StyledTab} from './styled-components.js';
 
import type {TabPropsT, SharedStylePropsArgT} from './types.js';
 
class TabComponent extends React.Component<TabPropsT> {
  static defaultProps = {
    disabled: false,
    expanded: false,
    onSelect: () => {},
    onClick: () => {},
    onKeyDown: () => {},
    title: '',
  };
 
  onClick = (e: Event) => {
    const {disabled, onSelect, onClick} = this.props;
    if (disabled) {
      return;
    }
    typeof onClick === 'function' && onClick(e);
    typeof onSelect === 'function' && onSelect();
    return;
  };
 
  onKeyDown = (e: KeyboardEvent) => {
    const {disabled, onSelect, onKeyDown} = this.props;
    if (disabled) {
      return;
    }
    typeof onKeyDown === 'function' && onKeyDown(e);
    // toggle on Enter or Space button pressed
    Eif (e.key === 'Enter' || e.which === 32) {
      typeof onSelect === 'function' && onSelect();
    }
    return;
  };
 
  getSharedProps(): SharedStylePropsArgT {
    const {disabled, active, $orientation} = this.props;
    return {
      $disabled: disabled,
      $active: active,
      $orientation: $orientation,
    };
  }
 
  render() {
    const {active, disabled, id, overrides = {}, children} = this.props;
    const sharedProps = this.getSharedProps();
    const {Tab: TabOverride} = overrides;
    const [Tab, tabProps] = getOverrides(TabOverride, StyledTab);
 
    return (
      <Tab
        tabIndex={disabled ? -1 : 0}
        role="tab"
        id={id}
        aria-selected={active}
        aria-disabled={disabled || null}
        {...sharedProps}
        {...tabProps}
        onClick={this.onClick}
        onKeyDown={this.onKeyDown}
      >
        {children}
      </Tab>
    );
  }
}
 
export default TabComponent;