all files / react-native-combined-button/ combined-button.js

81.48% Statements 66/81
65.52% Branches 38/58
90.91% Functions 10/11
95.24% Lines 20/21
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                                         36× 27×                                                                                                
import React, { Component, PropTypes } from 'react';IEE
import {EIII
    View,
    TouchableOpacity,
    Text,
    Image,
    InteractionManager,
    StyleSheet
} from 'react-native';
 
const FLEX_DIRECTION ={ 'top': 'column',
                      'right': 'row',
                      'bottom': 'column',
                      'left': 'row' };
 
const EVENT_HANDLER_PROP_NAMES = ['onPress', 'onPressIn', 'onPressOut'];
 
export default class CombinedButton extends Component {
    static propTypes = {
        text: PropTypes.string,
        iconPosition: PropTypes.oneOf(['top', 'left', 'bottom', 'right']),
        style: PropTypes.any
    };
 
    render() {
        const { style, textStyle, iconStyle, text, icon, iconPosition: iconPosition='top' } = this.props;
 
        let eventHandlers = {};
        for (ehProp of EVENT_HANDLER_PROP_NAMES) {E
            if ( this.props[ehProp]) {
                const handler = this.props[ehProp];
                eventHandlers[ehProp] = handler;
            }
        }
 
        let btnStyle = [styles.container];
I        if ( typeof style == 'Array' ) {
            btnStyle = btnStyle.concat(style);
        } else {
            btnStyle.push(style);
        }
        btnStyle.push( { 'flexDirection': FLEX_DIRECTION[iconPosition]});
 
        const containerProps = Object.assign({}, this.props, eventHandlers, { style: btnStyle });
 
        return (
            <TouchableOpacity {...containerProps} >
              {( 'top' == iconPosition || 'left' == iconPosition) && icon && 
                  <Image style={[styles.iconStyle, iconStyle]}
                     source={icon} /> }
              {text && text != '' && 
                  <Text style={[styles.textStyle, textStyle]}>{this.props.text}</Text>
              }
              {('bottom' == iconPosition || 'right' == iconPosition) && icon &&
                  <Image style={[styles.iconStyle, iconStyle]}
                     source={icon} /> }
            </TouchableOpacity>
        );
    }; 
}
 
const styles = StyleSheet.create({
    container: {
        flex:1,
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#CCC',
        borderColor: '#CCC',
        borderWidth: 1,
        borderRadius: 5,
        padding: 8 
    },
 
    iconStyle: {
        flex: 0,
        width: 24,
        height: 24
    },
 
    textStyle: {
        fontSize: 18,
        flex: 1,
        textAlign: 'center'
    }
 
});