All files / src/components BottomNavigation.react.js

50% Statements 11/22
23.08% Branches 3/13
50% Functions 3/6
50% Lines 11/22
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149                                                              1x                                                                                         1x   1x                     2x       2x       2x             2x               2x                                       1x   1x                         1x  
// @flow
 
import React from 'react';
import PropTypes from 'prop-types';
 
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: string,
  icon: Element | string,
  value: string | number,
  targetId?: string,
  iconClassName?: string,
}
type Props = {
  navItems: Array<T_NAV_ITEM>,
 
  setProps?: (props: { selectedIndex: number }) => void,
  selectedIndex?: number,
  selectedStyle?: Object,
};
type State = {
  selectedIndex: number,
};
 
const propTypes = {
  /**
   * The ID used to identify this compnent in Dash callbacks
   */
  id: PropTypes.string.isRequired,
 
  /** Array of navigation item props to pass to BottomNavigationItem */
  navItems: PropTypes.arrayOf(PropTypes.shape({
    label: PropTypes.string,
    icon: PropTypes.oneOfType([PropTypes.element, PropTypes.string]),
    value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    /** ID of component to jump to when this option is selected */
    targetId: PropTypes.string,
    /** Class to apply to the icon span */
    iconClassName: PropTypes.string,
  })).isRequired,
 
  /** Initial selected index for the BottomNavigation */
  selectedIndex: PropTypes.number,
 
  /** Style to apply to the selected icon */
  selectedStyle: PropTypes.objectOf(PropTypes.any),
 
  /**
   * Dash-assigned callback that should be called whenever any of the
   * properties change
   */
  setProps: PropTypes.func.isRequired,
};
 
/**
 * BottomNavigationItem is an item in a BottomNavigation component
 */
export default class BottomNavigation extends React.Component<Props, State> {
  props: Props;
  state: State;
 
  static defaultProps = {
    selectedIndex: 0,
    setProps: () => {
    },
    selectedStyle: {},
  };
 
  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});
 
          if (typeof navItem.targetId === 'string') {
            const targetElement = document.getElementById(navItem.targetId);
            targetElement.scrollIntoView();
          }
 
          if (typeof this.props.setProps === 'function')
            this.props.setProps({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.propTypes = propTypes;