Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const React = require('react');
  2. const ReactNative = require('react-native');
  3. const {
  4.   StyleSheet,
  5.   Text,
  6.   View,
  7.   Animated,
  8. } = ReactNative;
  9. const Button = require('./Button');
  10.  
  11. const DefaultTabBar = React.createClass({
  12.   propTypes: {
  13.     goToPage: React.PropTypes.func,
  14.     activeTab: React.PropTypes.number,
  15.     tabs: React.PropTypes.array,
  16.     backgroundColor: React.PropTypes.string,
  17.     activeTextColor: React.PropTypes.string,
  18.     inactiveTextColor: React.PropTypes.string,
  19.     textStyle: Text.propTypes.style,
  20.     tabStyle: View.propTypes.style,
  21.     renderTab: React.PropTypes.func,
  22.     underlineStyle: View.propTypes.style,
  23.   },
  24.  
  25.   getDefaultProps() {
  26.     return {
  27.       activeTextColor: 'navy',
  28.       inactiveTextColor: 'black',
  29.       backgroundColor: null,
  30.       activeBackgroundColor: null,
  31.     };
  32.   },
  33.  
  34.   renderTabOption(name, page) {
  35.   },
  36.  
  37.   renderTab(name, page, isTabActive, onPressHandler) {
  38.     const { activeTextColor, inactiveTextColor, textStyle, activeBackgroundColor } = this.props;
  39.     const textColor = isTabActive ? activeTextColor : inactiveTextColor;
  40.     const fontWeight = isTabActive ? 'bold' : 'normal';
  41.     const backgroundColor = isTabActive ? activeBackgroundColor : backgroundColor;
  42.  
  43.     return <Button
  44.       style={styles.flexOne}
  45.       key={name}
  46.       accessible={true}
  47.       accessibilityLabel={name}
  48.       accessibilityTraits='button'
  49.       onPress={() => onPressHandler(page)}
  50.     >
  51.       <View style={[styles.tab, this.props.tabStyle, { backgroundColor } ]}>
  52.         <Text style={[{color: textColor, fontWeight }, textStyle, ]}>
  53.           {name}
  54.         </Text>
  55.       </View>
  56.     </Button>;
  57.   },
  58.  
  59.   render() {
  60.     const containerWidth = this.props.containerWidth;
  61.     const numberOfTabs = this.props.tabs.length;
  62.     const tabUnderlineStyle = {
  63.       position: 'absolute',
  64.       width: containerWidth / numberOfTabs,
  65.       height: 4,
  66.       backgroundColor: 'navy',
  67.       bottom: 0,
  68.     };
  69.  
  70.     const left = this.props.scrollValue.interpolate({
  71.       inputRange: [0, 1, ], outputRange: [0,  containerWidth / numberOfTabs, ],
  72.     });
  73.     return (
  74.       <View style={[styles.tabs, {backgroundColor: this.props.backgroundColor, }, this.props.style, ]}>
  75.         {this.props.tabs.map((name, page) => {
  76.           const isTabActive = this.props.activeTab === page;
  77.           const renderTab = this.props.renderTab || this.renderTab;
  78.           return renderTab(name, page, isTabActive, this.props.goToPage);
  79.         })}
  80.         <Animated.View style={[tabUnderlineStyle, { left, }, this.props.underlineStyle, ]} />
  81.       </View>
  82.     );
  83.   },
  84. });
  85.  
  86. const styles = StyleSheet.create({
  87.   tab: {
  88.     flex: 1,
  89.     alignItems: 'center',
  90.     justifyContent: 'center',
  91.     paddingBottom: 10,
  92.   },
  93.   flexOne: {
  94.     flex: 1,
  95.   },
  96.   tabs: {
  97.     height: 50,
  98.     flexDirection: 'row',
  99.     justifyContent: 'space-around',
  100.     borderWidth: 1,
  101.     borderTopWidth: 0,
  102.     borderLeftWidth: 0,
  103.     borderRightWidth: 0,
  104.     borderColor: '#ccc',
  105.   },
  106. });
  107.  
  108. module.exports = DefaultTabBar;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement