Button.js 2.84 KB
'use strict'

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {
    View,
    Text,
    TouchableWithoutFeedback,
    StyleSheet,
} from 'react-native';

export default class Button extends Component {

	constructor(props) {
        super(props);

        this.state = {
            helighted: false,
        }

        this._onPress = this._onPress.bind(this);
        this._onPressIn = this._onPressIn.bind(this);
        this._onPressOut = this._onPressOut.bind(this);
        this._helightedTextStyle = this._helightedTextStyle.bind(this);
        this._selectedTextStyle = this._selectedTextStyle.bind(this);

    }

    render() {

        return (
        	<TouchableWithoutFeedback
                onPress={()=>{this._onPress()}}
                onPressIn={()=>{this._onPressIn()}}
                onPressOut={()=>{this._onPressOut()}} >
                <View style={this.props.containerStyle || styles.container} >
                    <Text style={[this._selectedTextStyle(), this._helightedTextStyle()]} >
                        {this.props.title}
                    </Text>
                </View>
        	</TouchableWithoutFeedback>
        );

    }

    _helightedTextStyle() {
        if(this.state.helighted) {
            if(this.props.helightedTitleStyle) {
                return this.props.helightedTitleStyle;
            }

            if(this.props.selected) {
                return this.props.normalTitleStyle || styles.titleNormal;
            } else {
                return this.props.selectedTitleStyle || styles.titleSelected;
            }
        } else {
            return this._selectedTextStyle();
        }
    }

    _selectedTextStyle() {
        if(this.props.selected) {
            return this.props.selectedTitleStyle || styles.titleSelected;
        } else {
            return this.props.normalTitleStyle || styles.titleNormal;
        }
    }

    _onPressIn() {
        this.setState({
            helighted: !this.state.helighted,
        });
    }

    _onPressOut() {
        this.setState({
            helighted: !this.state.helighted,
        });
    }

    _onPress() {

        if(!this.props.onPress) {
            return;
        }

    	this.props.onPress();
    }

};

Button.propTypes = {
    title: PropTypes.string,
    selected: PropTypes.bool,
    onPress: PropTypes.func,
    selectedTitleStyle: PropTypes.any,
    normalTitleStyle: PropTypes.any,
    helightedTitleStyle: PropTypes.any,
    containerStyle: PropTypes.any,
};

let styles = StyleSheet.create({
    container: {
        width: 44,
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    },
    titleHelighted: {
    	color: 'black',
        fontSize: 15,
    },
    titleNormal: {
        color: '#b0b0b0',
        fontSize: 15,
    },
    titleSelected: {
        color: 'black',
        fontSize: 15,
    },
});