All files / tabs tabs.js

93.33% Statements 28/30
75% Branches 12/16
88.89% Functions 8/9
100% Lines 28/28

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                                    5x 5x       10x   10x 30x   30x 30x           5x         10x                   10x 10x 10x         10x 30x 30x 30x 30x       30x           30x                     10x       10x 10x             10x 10x 10x 10x 10x   10x                    
/*
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 {
  Root as StyledRoot,
  TabBar as StyledTabBar,
  TabContent as StyledTabContent,
} from './styled-components.js';
import type {TabsPropsT, SharedStylePropsArgT} from './types.js';
import {ORIENTATION} from './constants.js';
 
export default class Tabs extends React.Component<TabsPropsT> {
  static defaultProps: $Shape<TabsPropsT> = {
    disabled: false,
    onChange: () => {},
    overrides: {},
    orientation: ORIENTATION.horizontal,
  };
 
  onChange({activeKey}: {activeKey: string}) {
    const {onChange} = this.props;
    typeof onChange === 'function' && onChange({activeKey});
  }
 
  getTabs() {
    const {activeKey, disabled, orientation, children} = this.props;
    // eslint-disable-next-line flowtype/no-weak-types
    const tabs = React.Children.map(children, (child: any, index) => {
      Iif (!child) return;
 
      const key = child.key || String(index);
      return React.cloneElement(child, {
        key,
        id: key, // for aria-labelledby
        active: key === activeKey,
        disabled: disabled || child.props.disabled,
        $orientation: orientation,
        onSelect: () => this.onChange({activeKey: key}),
        children: child.props.title,
      });
    });
 
    return tabs;
  }
 
  getPanels() {
    const {
      activeKey,
      disabled,
      orientation,
      children,
      overrides = {},
    } = this.props;
    const {TabContent: TabContentOverride} = overrides;
    const [TabContent, tabContentProps] = getOverrides(
      TabContentOverride,
      StyledTabContent,
    );
    // eslint-disable-next-line flowtype/no-weak-types
    const tabs = React.Children.map(children, (child: any, index) => {
      Iif (!child) return;
      const key = child.key || String(index);
      const isActive = key === activeKey;
      const props = {
        key,
        'aria-labelledby': key,
      };
      const sharedProps = {
        $active: isActive,
        $disabled: disabled,
        $orientation: orientation,
      };
 
      return (
        <TabContent
          role="tabpanel"
          {...sharedProps}
          {...tabContentProps}
          {...props}
        >
          {isActive ? child.props.children : null}
        </TabContent>
      );
    });
    return tabs;
  }
 
  getSharedProps(): SharedStylePropsArgT {
    const {disabled, orientation} = this.props;
    return {
      $disabled: disabled,
      $orientation: orientation,
    };
  }
 
  render() {
    const sharedProps = this.getSharedProps();
    const {overrides = {}} = this.props;
    const {Root: RootOverride, TabBar: TabBarOverride} = overrides;
    const [Root, rootProps] = getOverrides(RootOverride, StyledRoot);
    const [TabBar, tabBarProps] = getOverrides(TabBarOverride, StyledTabBar);
 
    return (
      <Root data-baseweb="tabs" {...sharedProps} {...rootProps}>
        <TabBar role="tablist" {...sharedProps} {...tabBarProps}>
          {this.getTabs()}
        </TabBar>
        {this.getPanels()}
      </Root>
    );
  }
}