All files / buttons Button.js

28.57% Statements 2/7
0% Branches 0/38
0% Functions 0/2
28.57% Lines 2/7
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171                                                                                                                                                                                                            1x                                             1x                                                                                          
import PropTypes from 'prop-types';
import React, { Component } from 'react';
 
import {
  StyleSheet,
  View,
  Text,
  TouchableNativeFeedback,
  TouchableOpacity,
  ActivityIndicator,
  Platform,
} from 'react-native';
 
import ViewPropTypes from '../config/ViewPropTypes';
 
class Button extends Component {
  log() {
    console.log('Please attach a method to this component');
  }
 
  render() {
    const {
      containerStyle,
      onPress,
      buttonStyle,
      clear,
      loading,
      loadingStyle,
      loadingProps,
      text,
      textStyle,
      textProps,
      icon,
      iconContainerStyle,
      iconRight,
      linearGradientProps,
      ...attributes
    } = this.props;
 
    // this is what RN Button does by default
    // https://github.com/facebook/react-native/blob/master/Libraries/Components/Button.js#L118
    const Touchable =
      Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
    const ButtonContainer = linearGradientProps
      ? require('expo').LinearGradient
      : View;
 
    return (
      <View style={[styles.container, containerStyle]}>
        <Touchable
          onPress={onPress || this.log.bind(this)}
          underlayColor={clear && 'transparent'}
          activeOpacity={clear && 0}
          style={{
            borderRadius:
              (buttonStyle &&
                buttonStyle.borderRadius &&
                buttonStyle.borderRadius) ||
              3,
          }}
          {...attributes}
        >
          <ButtonContainer
            {...linearGradientProps}
            style={[
              styles.button,
              clear && { backgroundColor: 'transparent', elevation: 0 },
              buttonStyle,
              linearGradientProps && { backgroundColor: 'transparent' },
            ]}
          >
            {loading &&
              <ActivityIndicator
                animating={true}
                style={[styles.loading, loadingStyle]}
                color={loadingProps && loadingProps.color || 'white'}
                size={loadingProps && loadingProps.size || 'small'}
                {...loadingProps}
              />}
            {!loading &&
              icon &&
              !iconRight &&
              <View style={[styles.iconContainer, iconContainerStyle]}>
                {icon}
              </View>}
            {!loading &&
              <Text style={[styles.text, textStyle]} {...textProps}>
                {text || 'Welcome to\nReact Native Elements'}
              </Text>}
            {!loading &&
              icon &&
              iconRight &&
              <View style={[styles.iconContainer, iconContainerStyle]}>
                {icon}
              </View>}
          </ButtonContainer>
        </Touchable>
      </View>
    );
  }
}
 
Button.propTypes = {
  text: PropTypes.string,
  textStyle: Text.propTypes.style,
  textProps: PropTypes.object,
 
  buttonStyle: ViewPropTypes.style,
 
  clear: PropTypes.bool,
 
  loading: PropTypes.bool,
  loadingStyle: ViewPropTypes.style,
  loadingProps: PropTypes.object,
 
  onPress: PropTypes.any,
  containerStyle: ViewPropTypes.style,
 
  icon: PropTypes.object,
  iconContainerStyle: ViewPropTypes.style,
  iconRight: PropTypes.bool,
 
  linearGradientProps: PropTypes.object,
};
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    borderRadius: 30,
  },
  button: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    borderRadius: 3,
    ...Platform.select({
      ios: {
        // iOS blue from https://developer.apple.com/ios/human-interface-guidelines/visual-design/color/
        backgroundColor: '#007AFF',
      },
      android: {
        elevation: 4,
        // Material design blue from https://material.google.com/style/color.html#color-color-palette
        backgroundColor: '#2196F3',
        borderRadius: 2,
      },
    }),
  },
  text: {
    color: 'white',
    fontSize: 16,
    textAlign: 'center',
    padding: 8,
    ...Platform.select({
      ios: {
        fontSize: 18,
      },
      android: {
        fontWeight: '500',
      },
    }),
  },
  iconContainer: {
    marginHorizontal: 5,
  },
});
 
export default Button;