1
1
'use strict' ;
2
2
3
- import React , { Component } from 'react' ;
3
+ import React , { Component , PropTypes } from 'react' ;
4
4
import {
5
5
StyleSheet ,
6
6
Platform ,
@@ -22,43 +22,110 @@ const duration_values = {
22
22
exit : 195
23
23
}
24
24
25
- class FAB extends Component {
25
+ export default class FAB extends Component {
26
26
27
- constructor ( props ) {
28
- super ( props ) ;
29
- this . state = {
30
- translateValue : new Animated . Value ( 0 )
31
- } ;
27
+ static propTypes = {
28
+ buttonColor : PropTypes . string ,
29
+ iconTextColor : PropTypes . string ,
30
+ onClickAction : PropTypes . func ,
31
+ iconTextComponent : PropTypes . element ,
32
+ visible : PropTypes . bool
33
+ }
34
+
35
+ static defaultProps = {
36
+ buttonColor : 'red' ,
37
+ iconTextColor : '#FFFFFF' ,
38
+ onClickAction : ( ) => { } ,
39
+ iconTextComponent : < Text > +</ Text > ,
40
+ visible : true
41
+ } ;
42
+
43
+ state = {
44
+ translateValue : new Animated . Value ( 0 )
45
+ } ;
46
+
47
+ componentDidMount ( ) {
48
+ const { translateValue } = this . state ;
49
+ const { visible } = this . props ;
50
+
51
+ if ( visible ) {
52
+ translateValue . setValue ( 1 ) ;
53
+ }
54
+ else {
55
+ translateValue . setValue ( 0 ) ;
56
+ }
57
+ }
58
+
59
+ componentWillReceiveProps ( nextProps ) {
60
+ const { translateValue } = this . state ;
61
+ const { visible } = this . props ;
62
+
63
+ if ( ( nextProps . visible ) && ( ! visible ) ) {
64
+ Animated . timing (
65
+ translateValue ,
66
+ {
67
+ duration : duration_values . entry ,
68
+ toValue : 1 ,
69
+ easing : sharp_easing_values . entry
70
+ }
71
+ ) . start ( ) ;
72
+ }
73
+ else if ( ( ! nextProps . visible ) && ( visible ) ) {
74
+ Animated . timing (
75
+ translateValue ,
76
+ {
77
+ duration : duration_values . exit ,
78
+ toValue : 0 ,
79
+ easing : sharp_easing_values . exit
80
+ }
81
+ ) . start ( ) ;
82
+ }
32
83
}
33
84
34
85
render ( ) {
86
+ const {
87
+ translateValue,
88
+ } = this . state ;
89
+ const {
90
+ onClickAction,
91
+ buttonColor,
92
+ iconTextComponent,
93
+ iconTextColor,
94
+ } = this . props ;
95
+
96
+ const dimensionInterpolate = translateValue . interpolate ( {
97
+ inputRange : [ 0 , 1 ] ,
98
+ outputRange : [ 0 , 56 ] ,
99
+ } ) ;
100
+
101
+ const rotateInterpolate = translateValue . interpolate ( {
102
+ inputRange : [ 0 , 1 ] ,
103
+ outputRange : [ '-90deg' , '0deg' ] ,
104
+ } ) ;
105
+
35
106
if ( Platform . OS === 'ios' ) {
36
107
return (
37
108
< View style = { styles . fab_box } >
38
109
< Animated . View
39
110
style = { [
40
111
styles . addButton ,
41
112
{
42
- height : this . state . translateValue . interpolate ( { inputRange : [ 0 , 1 ] , outputRange : [ 0 , 56 ] } ) ,
43
- width : this . state . translateValue . interpolate ( { inputRange : [ 0 , 1 ] , outputRange : [ 0 , 56 ] } )
113
+ height : dimensionInterpolate ,
114
+ width : dimensionInterpolate
44
115
}
45
116
] }
46
117
>
47
- < TouchableOpacity onPress = { ( ) => { this . props . onClickAction ( ) } } style = { [ styles . addButtonInnerView , { backgroundColor : this . props . buttonColor } ] } >
118
+ < TouchableOpacity onPress = { ( ) => { onClickAction ( ) } } style = { [ styles . addButtonInnerView , { backgroundColor : buttonColor } ] } >
48
119
< Animated . Text style = { {
49
120
transform : [
50
- { scaleX : this . state . translateValue } ,
51
- { scaleY : this . state . translateValue } ,
52
- { rotate : this . state . translateValue . interpolate ( {
53
- inputRange : [ 0 , 1 ] ,
54
- outputRange : [ '-90deg' , '0deg' ]
55
- } ) }
121
+ { scale : translateValue } ,
122
+ { rotate : rotateInterpolate }
56
123
] ,
57
124
fontSize : 24
58
125
} } >
59
- { React . cloneElement ( this . props . iconTextComponent , { style : {
126
+ { React . cloneElement ( iconTextComponent , { style : {
60
127
fontSize : 24 ,
61
- color : this . props . iconTextColor
128
+ color : iconTextColor
62
129
} } ) }
63
130
</ Animated . Text >
64
131
</ TouchableOpacity >
@@ -73,27 +140,23 @@ class FAB extends Component {
73
140
style = { [
74
141
styles . addButton ,
75
142
{
76
- height : this . state . translateValue . interpolate ( { inputRange : [ 0 , 1 ] , outputRange : [ 0 , 56 ] } ) ,
77
- width : this . state . translateValue . interpolate ( { inputRange : [ 0 , 1 ] , outputRange : [ 0 , 56 ] } )
143
+ height : dimensionInterpolate ,
144
+ width : dimensionInterpolate
78
145
}
79
146
] }
80
147
>
81
- < TouchableNativeFeedback background = { TouchableNativeFeedback . SelectableBackgroundBorderless ( ) } onPress = { ( ) => { this . props . onClickAction ( ) } } >
82
- < View style = { [ styles . addButtonInnerView , { backgroundColor : this . props . buttonColor } ] } >
148
+ < TouchableNativeFeedback background = { TouchableNativeFeedback . SelectableBackgroundBorderless ( ) } onPress = { ( ) => { onClickAction ( ) } } >
149
+ < View style = { [ styles . addButtonInnerView , { backgroundColor : buttonColor } ] } >
83
150
< Animated . Text style = { {
84
151
transform : [
85
- { scaleX : this . state . translateValue } ,
86
- { scaleY : this . state . translateValue } ,
87
- { rotate : this . state . translateValue . interpolate ( {
88
- inputRange : [ 0 , 1 ] ,
89
- outputRange : [ '-90deg' , '0deg' ]
90
- } ) }
152
+ { scaleX : translateValue } ,
153
+ { rotate : rotateInterpolate }
91
154
] ,
92
155
fontSize : 24
93
156
} } >
94
- { React . cloneElement ( this . props . iconTextComponent , { style : {
157
+ { React . cloneElement ( iconTextComponent , { style : {
95
158
fontSize : 24 ,
96
- color : this . props . iconTextColor
159
+ color : iconTextColor
97
160
} } ) }
98
161
</ Animated . Text >
99
162
</ View >
@@ -103,49 +166,8 @@ class FAB extends Component {
103
166
) ;
104
167
}
105
168
}
106
-
107
- componentDidMount ( ) {
108
- if ( this . props . visible ) {
109
- this . state . translateValue . setValue ( 1 ) ;
110
- }
111
- else {
112
- this . state . translateValue . setValue ( 0 ) ;
113
- }
114
- }
115
-
116
- componentWillReceiveProps ( nextProps ) {
117
- if ( ( nextProps . visible ) && ( ! this . props . visible ) ) {
118
- Animated . timing (
119
- this . state . translateValue ,
120
- {
121
- duration : duration_values . entry ,
122
- toValue : 1 ,
123
- easing : sharp_easing_values . entry
124
- }
125
- ) . start ( ) ;
126
- }
127
- else if ( ( ! nextProps . visible ) && ( this . props . visible ) ) {
128
- Animated . timing (
129
- this . state . translateValue ,
130
- {
131
- duration : duration_values . exit ,
132
- toValue : 0 ,
133
- easing : sharp_easing_values . exit
134
- }
135
- ) . start ( ) ;
136
- }
137
- }
138
-
139
169
}
140
170
141
- FAB . defaultProps = {
142
- buttonColor : 'red' ,
143
- iconTextColor : '#FFFFFF' ,
144
- onClickAction : ( ) => { } ,
145
- iconTextComponent : < Text > +</ Text > ,
146
- visible : true
147
- } ;
148
-
149
171
const styles = StyleSheet . create ( {
150
172
addButton : {
151
173
borderRadius : 50 ,
@@ -176,13 +198,3 @@ const styles = StyleSheet.create({
176
198
justifyContent : 'center'
177
199
}
178
200
} ) ;
179
-
180
- export default FAB ;
181
-
182
- FAB . propTypes = {
183
- buttonColor : React . PropTypes . string ,
184
- iconTextColor : React . PropTypes . string ,
185
- onClickAction : React . PropTypes . func ,
186
- iconTextComponent : React . PropTypes . element ,
187
- visible : React . PropTypes . bool
188
- }
0 commit comments