All files / header Header.js

91.67% Statements 22/24
84.62% Branches 22/26
100% Functions 4/4
91.67% Lines 22/24
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 150 151 152 153 154                    1x           1x         18x 3x 15x 3x                                 12x       6x   6x 6x           6x   6x   6x     1x                         13x   13x   13x 6x                     13x                                 1x                               1x       1x                                                    
import React from 'react';
import PropTypes from 'prop-types';
import { Platform, StatusBar, StyleSheet, View } from 'react-native';
import isEmpty from 'lodash.isempty';
import DummyNavButton from './DummyNavButton';
import NavButton from './NavButton';
import Title from './Title';
import { colors, ViewPropTypes, getStatusBarHeight } from '../config';
 
function alignStyle(placement) {
  switch (placement) {
    case 'left':
      return 'flex-start';
    case 'right':
      return 'flex-end';
    default:
      return 'center';
  }
}
 
function generateChild(value, type, placement, centerComponentStyle) {
  if (React.isValidElement(value)) {
    return <View key={type}>{value}</View>;
  } else if (typeof value === 'object' && !isEmpty(value)) {
    return type === 'center' ? (
      <View
        key={type}
        style={[
          styles.centerComponent,
          centerComponentStyle,
          {
            alignItems: alignStyle(placement),
          },
        ]}
      >
        <Title {...value} />
      </View>
    ) : (
      <NavButton {...value} key={type} />
    );
  }
  return type === 'center' ? null : <DummyNavButton key={type} />;
}
 
function populateChildren(propChildren, placement, centerComponentStyle) {
  const childrenArray = [];
 
  const leftComponent = generateChild(propChildren.leftComponent, 'left');
  const centerComponent = generateChild(
    propChildren.centerComponent,
    'center',
    placement,
    centerComponentStyle
  );
  const rightComponent = generateChild(propChildren.rightComponent, 'right');
 
  childrenArray.push(leftComponent, centerComponent, rightComponent);
 
  return childrenArray;
}
 
const Header = props => {
  const {
    children,
    statusBarProps,
    leftComponent,
    centerComponent,
    centerComponentStyle,
    rightComponent,
    backgroundColor,
    outerContainerStyles,
    innerContainerStyles,
    placement,
    ...attributes
  } = props;
 
  let propChildren = [];
 
  if (leftComponent || centerComponent || rightComponent) {
    propChildren = populateChildren(
      {
        leftComponent,
        centerComponent,
        rightComponent,
      },
      placement,
      centerComponentStyle
    );
  }
 
  return (
    <View
      {...attributes}
      style={[
        styles.outerContainer,
        backgroundColor && { backgroundColor },
        outerContainerStyles,
      ]}
    >
      <StatusBar barStyle="light-content" {...statusBarProps} />
      <View style={[styles.innerContainer, innerContainerStyles]}>
        {propChildren.length > 0 ? propChildren : children}
      </View>
    </View>
  );
};
 
Header.propTypes = {
  placement: PropTypes.oneOf(['left', 'center', 'right']),
  leftComponent: PropTypes.object,
  centerComponent: PropTypes.object,
  rightComponent: PropTypes.object,
  backgroundColor: PropTypes.string,
  outerContainerStyles: ViewPropTypes.style,
  innerContainerStyles: ViewPropTypes.style,
  centerComponentStyle: ViewPropTypes.style,
  children: PropTypes.oneOfType([
    PropTypes.element,
    PropTypes.arrayOf(PropTypes.element),
  ]),
  statusBarProps: PropTypes.object,
};
 
Header.defaultProps = {
  placement: 'center',
};
 
const styles = StyleSheet.create({
  innerContainer: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
  },
  outerContainer: {
    backgroundColor: colors.primary,
    borderBottomColor: '#f2f2f2',
    borderBottomWidth: 1,
    paddingHorizontal: 10,
    height: Platform.OS === 'ios' ? 70 : 70 - getStatusBarHeight(),
    ...Platform.select({
      ios: {
        paddingTop: getStatusBarHeight(),
      },
    }),
  },
  centerComponent: {
    flex: 1,
    marginHorizontal: Platform.OS === 'ios' ? 15 : 16,
  },
});
 
export default Header;