Button.js
2.84 KB
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
'use strict'
import React, {Component} from 'react';
import {
View,
Text,
TouchableWithoutFeedback,
StyleSheet,
} from 'react-native';
import PropTypes from 'prop-types';
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,
},
});