1
+ const React = require ( 'react-native' )
2
+
3
+
4
+ const {
5
+ Animated,
6
+ ActivityIndicatorIOS,
7
+ Component,
8
+ View,
9
+ Text,
10
+ StyleSheet,
11
+ TouchableHighlight
12
+ } = React
13
+
14
+
15
+ class InnerButtonView extends Component {
16
+
17
+ render ( ) {
18
+ return (
19
+ < View style = { styles . insideView } >
20
+ { this . props . currentStateObject . spinner ? < ActivityIndicatorIOS color = "white" style = { styles . activityIndicator } /> : null }
21
+ < Text style = { this . props . currentStateObject . labelStyle } > { this . props . currentStateObject . text } </ Text >
22
+ </ View >
23
+ )
24
+ }
25
+
26
+ }
27
+
28
+
29
+ class ButtonView extends Component {
30
+
31
+ shadeColor1 ( color , percent ) {
32
+ const num = parseInt ( color . slice ( 1 ) , 16 ) , amt = Math . round ( 2.55 * percent ) , R = ( num >> 16 ) + amt , G = ( num >> 8 & 0x00FF ) + amt , B = ( num & 0x0000FF ) + amt ;
33
+ return "#" + ( 0x1000000 + ( R < 255 ?R < 1 ?0 :R :255 ) * 0x10000 + ( G < 255 ?G < 1 ?0 :G :255 ) * 0x100 + ( B < 255 ?B < 1 ?0 :B :255 ) ) . toString ( 16 ) . slice ( 1 ) ;
34
+ }
35
+
36
+ render ( ) {
37
+ if ( this . props . states . hasOwnProperty ( this . props . buttonState ) ) {
38
+ const currentStateObject = this . props . states [ this . props . buttonState ]
39
+ } else {
40
+ const currentStateObject = {
41
+ text : 'Button state not recognized' ,
42
+ backgroundStyle : {
43
+ backgroundColor : 'black' ,
44
+ } ,
45
+ labelStyle : {
46
+ color : 'white' ,
47
+ }
48
+ }
49
+ }
50
+ if ( currentStateObject . touchable ) {
51
+ return (
52
+ < TouchableHighlight style = { [ this . props . style , currentStateObject . backgroundStyle ] }
53
+ underlayColor = { this . shadeColor1 ( currentStateObject . backgroundStyle . backgroundColor , - 5 ) }
54
+ onPress = { currentStateObject . onPress } >
55
+ < View style = { [ styles . container , this . props . style ] } >
56
+ < InnerButtonView currentStateObject = { currentStateObject } />
57
+ </ View >
58
+ </ TouchableHighlight >
59
+ )
60
+ } else {
61
+ return (
62
+ < View style = { [ this . props . style , currentStateObject . backgroundStyle ] } >
63
+ < InnerButtonView currentStateObject = { currentStateObject } />
64
+ </ View >
65
+ )
66
+ }
67
+ }
68
+
69
+ }
70
+
71
+
72
+ const AnimatedButton = Animated . createAnimatedComponent ( ButtonView )
73
+
74
+
75
+ class AwesomeButton extends Component {
76
+
77
+ constructor ( props : any ) {
78
+ super ( props ) ;
79
+ this . state = {
80
+ opacityValue : new Animated . Value ( 0 ) ,
81
+ view1 : this . props . buttonState ,
82
+ }
83
+ }
84
+
85
+ startAnimation ( ) {
86
+ Animated . timing ( this . state . opacityValue ,
87
+ {
88
+ toValue : 1 ,
89
+ duration : 300 ,
90
+ delay : 50
91
+ } ) . start ( )
92
+ }
93
+
94
+ componentWillReceiveProps ( nextProps ) {
95
+ this . setState ( { view1 : this . props . buttonState , view2 : nextProps . buttonState } )
96
+ this . startAnimation ( )
97
+ }
98
+
99
+ render ( ) {
100
+ return (
101
+ < View >
102
+ < ButtonView { ...this . props } buttonState = { this . state . view1 } />
103
+ < AnimatedButton { ...this . props } buttonState = { this . state . view2 } style = { [ this . props . style , { position : 'absolute' , top : 0 , left : 0 , opacity : this . state . opacityValue } ] } />
104
+ </ View >
105
+ )
106
+ }
107
+
108
+ }
109
+
110
+
111
+ const styles = StyleSheet . create ( {
112
+ insideView : {
113
+ flex : 1 ,
114
+ flexDirection : 'row' ,
115
+ alignItems : 'center' ,
116
+ alignSelf : 'center'
117
+ } ,
118
+ activityIndicator : {
119
+ marginRight : 8 ,
120
+ } ,
121
+ container : {
122
+ flex : 1 ,
123
+ }
124
+ } )
125
+
126
+
127
+ module . exports = AwesomeButton
0 commit comments