6
6
* @flow
7
7
*/
8
8
9
- import React , { Component , PropTypes } from 'react' ;
9
+ import React , { Component } from 'react' ;
10
10
import {
11
11
StyleSheet ,
12
12
View ,
13
13
Animated ,
14
14
Dimensions ,
15
15
Text ,
16
16
} from 'react-native'
17
- export const DURATION = { LENGTH_LONG : 2000 , LENGTH_SHORT : 1000 } ;
17
+ export const DURATION = { LENGTH_LONG : 2000 , LENGTH_SHORT : 1000 } ;
18
18
const { height, width} = Dimensions . get ( 'window' ) ;
19
- const OPACITY = 0.6 ;
19
+ const OPACITY = 0.6 ;
20
20
21
21
export default class Toast extends Component {
22
- static propTypes = {
23
- style :View . propTypes . style ,
24
- position : PropTypes . oneOf ( [
25
- 'top' ,
26
- 'center' ,
27
- 'bottom' ,
28
- ] ) ,
29
- }
30
- static defaultProps = {
31
- position :'bottom' ,
32
- }
22
+
33
23
constructor ( props ) {
34
24
super ( props ) ;
35
25
this . state = {
36
26
isShow : false ,
37
27
text : '' ,
38
- opacityValue :new Animated . Value ( OPACITY ) ,
28
+ opacityValue : new Animated . Value ( OPACITY ) ,
39
29
}
40
30
}
41
31
show ( text , duration ) {
42
- this . duration = duration || DURATION . LENGTH_SHORT ;
32
+ this . duration = duration || DURATION . LENGTH_SHORT ;
43
33
this . setState ( {
44
34
isShow : true ,
45
35
text : text ,
46
36
} ) ;
47
- this . isShow = true ;
37
+ this . isShow = true ;
48
38
this . state . opacityValue . setValue ( OPACITY )
49
39
this . close ( ) ;
50
40
}
51
41
52
- close ( ) {
53
- if ( ! this . isShow ) return ;
42
+ close ( instant ) {
43
+ var animationDuration = 500 , closeDuration = this . duration ;
44
+ if ( instant == true ) {
45
+ animationDuration = 0 ;
46
+ closeDuration = 0 ;
47
+ }
48
+
49
+ if ( ! this . isShow ) return ;
54
50
this . timer && clearTimeout ( this . timer ) ;
55
51
this . timer = setTimeout ( ( ) => {
56
52
Animated . timing (
57
53
this . state . opacityValue ,
58
54
{
59
55
toValue : 0.0 ,
60
- duration :500 ,
56
+ duration : animationDuration ,
61
57
}
62
- ) . start ( ( ) => {
58
+ ) . start ( ( ) => {
63
59
this . setState ( {
64
60
isShow : false ,
65
61
} ) ;
66
- this . isShow = false ;
62
+ this . isShow = false ;
67
63
} ) ;
68
- } , this . duration ) ;
64
+ } , closeDuration ) ;
69
65
}
66
+
70
67
componentWillUnmount ( ) {
71
68
this . timer && clearTimeout ( this . timer ) ;
72
69
}
73
70
74
71
render ( ) {
75
72
let top ;
76
- switch ( this . props . position ) {
73
+ switch ( this . props . position ) {
77
74
case 'top' :
78
- top = 120 ;
75
+ top = 120 ;
79
76
break ;
80
77
case 'center' :
81
- top = height / 2 ;
78
+ top = height / 2 ;
82
79
break ;
83
80
case 'bottom' :
84
- top = height - 160 ;
81
+ top = height - 160 ;
85
82
break ;
86
83
}
87
84
let view = this . state . isShow ?
88
85
< View
89
- style = { [ styles . container , { top :top } ] }
86
+ style = { [ styles . container , { top : top } ] }
90
87
pointerEvents = "none"
91
- >
92
- < Animated . View
93
- style = { [ styles . content , { opacity :this . state . opacityValue } , this . props . style ] }
94
88
>
95
- < Text style = { styles . text } > { this . state . text } </ Text >
89
+ < Animated . View
90
+ style = { [ styles . content , { opacity : this . state . opacityValue } , this . props . style ] }
91
+ >
92
+ < Text style = { this . props . textStyle } > { this . state . text } </ Text >
96
93
</ Animated . View >
97
94
</ View > : null ;
98
95
return view ;
99
96
}
100
97
}
98
+
101
99
const styles = StyleSheet . create ( {
102
100
container : {
103
101
position : 'absolute' ,
@@ -111,7 +109,22 @@ const styles = StyleSheet.create({
111
109
borderRadius : 5 ,
112
110
padding : 10 ,
113
111
} ,
114
- text :{
115
- color :'white'
116
- } ,
117
- } )
112
+ text : {
113
+ color : 'white'
114
+ }
115
+ } ) ;
116
+
117
+ Toast . propTypes = {
118
+ style : View . propTypes . style ,
119
+ position : React . PropTypes . oneOf ( [
120
+ 'top' ,
121
+ 'center' ,
122
+ 'bottom' ,
123
+ ] ) ,
124
+ textStyle : Text . propTypes . style
125
+ }
126
+
127
+ Toast . defaultProps = {
128
+ position : 'bottom' ,
129
+ textStyle : styles . text
130
+ }
0 commit comments