@@ -14,9 +14,8 @@ import {
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 : 500 } ;
18
18
const { height, width} = Dimensions . get ( 'window' ) ;
19
- const OPACITY = 0.6 ;
20
19
21
20
export default class Toast extends Component {
22
21
@@ -25,65 +24,70 @@ export default class Toast extends Component {
25
24
this . state = {
26
25
isShow : false ,
27
26
text : '' ,
28
- opacityValue : new Animated . Value ( OPACITY ) ,
27
+ opacityValue : new Animated . Value ( this . props . opacity ) ,
29
28
}
30
29
}
31
30
show ( text , duration ) {
32
31
this . duration = duration || DURATION . LENGTH_SHORT ;
32
+
33
33
this . setState ( {
34
34
isShow : true ,
35
35
text : text ,
36
36
} ) ;
37
- this . isShow = true ;
38
- this . state . opacityValue . setValue ( OPACITY )
39
- this . close ( ) ;
40
- }
41
37
42
- close ( instant ) {
43
- var animationDuration = 500 , closeDuration = this . duration ;
44
- if ( instant == true ) {
45
- animationDuration = 0 ;
46
- closeDuration = 0 ;
47
- }
38
+ Animated . timing (
39
+ this . state . opacityValue ,
40
+ {
41
+ toValue : this . props . opacity ,
42
+ duration : this . props . fadeInDuration ,
43
+ }
44
+ ) . start ( ( ) => {
45
+ this . isShow = true ;
46
+ this . close ( ) ;
47
+ } ) ;
48
+ }
48
49
50
+ close ( ) {
51
+ let delay = this . duration ;
52
+
49
53
if ( ! this . isShow ) return ;
50
54
this . timer && clearTimeout ( this . timer ) ;
51
55
this . timer = setTimeout ( ( ) => {
52
56
Animated . timing (
53
57
this . state . opacityValue ,
54
58
{
55
59
toValue : 0.0 ,
56
- duration : animationDuration ,
60
+ duration : this . props . fadeOutDuration ,
57
61
}
58
62
) . start ( ( ) => {
59
63
this . setState ( {
60
64
isShow : false ,
61
65
} ) ;
62
66
this . isShow = false ;
63
67
} ) ;
64
- } , closeDuration ) ;
68
+ } , delay ) ;
65
69
}
66
70
67
71
componentWillUnmount ( ) {
68
72
this . timer && clearTimeout ( this . timer ) ;
69
73
}
70
74
71
75
render ( ) {
72
- let top ;
76
+ let pos ;
73
77
switch ( this . props . position ) {
74
78
case 'top' :
75
- top = 120 ;
79
+ pos = this . props . positionValue ;
76
80
break ;
77
81
case 'center' :
78
- top = height / 2 ;
82
+ pos = height / 2 ;
79
83
break ;
80
84
case 'bottom' :
81
- top = height - 160 ;
85
+ pos = height - this . props . positionValue ;
82
86
break ;
83
87
}
84
88
let view = this . state . isShow ?
85
89
< View
86
- style = { [ styles . container , { top : top } ] }
90
+ style = { [ styles . container , { top : pos } ] }
87
91
pointerEvents = "none"
88
92
>
89
93
< Animated . View
@@ -105,7 +109,6 @@ const styles = StyleSheet.create({
105
109
} ,
106
110
content : {
107
111
backgroundColor : 'black' ,
108
- opacity : OPACITY ,
109
112
borderRadius : 5 ,
110
113
padding : 10 ,
111
114
} ,
@@ -121,10 +124,18 @@ Toast.propTypes = {
121
124
'center' ,
122
125
'bottom' ,
123
126
] ) ,
124
- textStyle : Text . propTypes . style
127
+ textStyle : Text . propTypes . style ,
128
+ positionValue : React . PropTypes . number ,
129
+ fadeInDuration : React . PropTypes . number ,
130
+ fadeOutDuration : React . PropTypes . number ,
131
+ opacity : React . PropTypes . number
125
132
}
126
133
127
134
Toast . defaultProps = {
128
135
position : 'bottom' ,
129
- textStyle : styles . text
136
+ textStyle : styles . text ,
137
+ positionValue : 120 ,
138
+ fadeInDuration : 500 ,
139
+ fadeOutDuration : 500 ,
140
+ opacity : 1
130
141
}
0 commit comments