All files / src/components/BottomNavigation BottomNavigation.react.js

50% Statements 11/22
23.08% Branches 3/13
50% Functions 3/6
50% Lines 11/22

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 120 121 122 123 124 125 126 127 128 129 130 131                                                                                        1x                           1x   1x                     2x       2x       2x             2x               2x                                       1x   1x                         1x  
// @flow
 
import React, { Component } from 'react';
 
import {
  BottomNavigationItem,
  BottomNavigation as MUIBottomNavigation,
} from 'material-ui/BottomNavigation';
import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
 
type T_NAV_ITEM = {
  /** Label to use for the navigation item */
  label: string,
  /** Icon to display for the navigation item */
  icon: Node | string,
  /** Value for the navigation item */
  value: string | number,
  /** ID of component to jump to when this option is selected */
  targetId?: string,
  /** Class to apply to the icon span */
  iconClassName?: string,
}
type Props = {
  /** The ID used to identify this component in Dash callbacks */
  id: string,
  /** Array of navigation item props to pass to BottomNavigationItem */
  navItems: Array<T_NAV_ITEM>,
  /** Initial selected index for the BottomNavigation */
  selectedIndex?: number,
  /** Style to apply to the selected icon */
  selectedStyle?: Object,
 
  /**
   * Dash-assigned callback that should be called whenever any of the
   * properties change
   */
  setProps?: (props: { selectedIndex: number }) => void,
};
type State = {
  selectedIndex: number,
};
 
const defaultProps = {
  selectedIndex: 0,
  setProps: () => {},
  selectedStyle: {},
};
 
/**
 * BottomNavigationItem is an item in a BottomNavigation component
 */
export default class BottomNavigation extends Component<Props, State> {
  props: Props;
  state: State;
 
  constructor(props: Props) {
    super(props);
 
    this.state = {
      selectedIndex: props.selectedIndex,
    };
  }
 
  componentWillReceiveProps(nextProps: Props): void {
    if (nextProps.selectedIndex !== this.props.selectedIndex)
      this.setState({selectedIndex: nextProps.selectedIndex});
  }
 
  buildBottomNavigationItem = (navItem: T_NAV_ITEM, selectedIndex: number) => {
    const {selectedStyle} = this.props;
 
    let navItemIcon;
 
    switch (typeof navItem.icon) {
      case 'string':
      case 'number':
      case 'undefined':
        navItemIcon = (
          <span
            style={selectedIndex === this.state.selectedIndex ? selectedStyle : {}}
            className={navItem.iconClassName}
          >
            {navItem.icon}
          </span>);
        break;
      case 'object':
        navItemIcon = navItem.icon;
        break;
      default:
        break;
    }
 
    return (
      <BottomNavigationItem
        key={navItem.label}
        label={navItem.label}
        icon={navItemIcon}
        onClick={() => {
          this.setState({selectedIndex: selectedIndex});
 
          if (typeof navItem.targetId === 'string') {
            const targetElement = document.getElementById(navItem.targetId);
            targetElement.scrollIntoView();
          }
 
          if (typeof this.props.setProps === 'function')
            this.props.setProps({selectedIndex: selectedIndex});
        }}
      />);
  };
 
  render() {
    const {id, navItems} = this.props;
 
    return (
      <div id={id}>
        <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
          <MUIBottomNavigation selectedIndex={this.state.selectedIndex}>
            {
              navItems.map(this.buildBottomNavigationItem)
            }
          </MUIBottomNavigation>
        </MuiThemeProvider>
      </div>);
  }
}
 
BottomNavigation.defaultProps = defaultProps;