@@ -14,9 +14,8 @@ import {
1414 Dimensions ,
1515 Text ,
1616} from 'react-native'
17- export const DURATION = { LENGTH_LONG : 2000 , LENGTH_SHORT : 1000 } ;
17+ export const DURATION = { LENGTH_LONG : 2000 , LENGTH_SHORT : 500 } ;
1818const { height, width} = Dimensions . get ( 'window' ) ;
19- const OPACITY = 0.6 ;
2019
2120export default class Toast extends Component {
2221
@@ -25,65 +24,70 @@ export default class Toast extends Component {
2524 this . state = {
2625 isShow : false ,
2726 text : '' ,
28- opacityValue : new Animated . Value ( OPACITY ) ,
27+ opacityValue : new Animated . Value ( this . props . opacity ) ,
2928 }
3029 }
3130 show ( text , duration ) {
3231 this . duration = duration || DURATION . LENGTH_SHORT ;
32+
3333 this . setState ( {
3434 isShow : true ,
3535 text : text ,
3636 } ) ;
37- this . isShow = true ;
38- this . state . opacityValue . setValue ( OPACITY )
39- this . close ( ) ;
40- }
4137
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+ }
4849
50+ close ( ) {
51+ let delay = this . duration ;
52+
4953 if ( ! this . isShow ) return ;
5054 this . timer && clearTimeout ( this . timer ) ;
5155 this . timer = setTimeout ( ( ) => {
5256 Animated . timing (
5357 this . state . opacityValue ,
5458 {
5559 toValue : 0.0 ,
56- duration : animationDuration ,
60+ duration : this . props . fadeOutDuration ,
5761 }
5862 ) . start ( ( ) => {
5963 this . setState ( {
6064 isShow : false ,
6165 } ) ;
6266 this . isShow = false ;
6367 } ) ;
64- } , closeDuration ) ;
68+ } , delay ) ;
6569 }
6670
6771 componentWillUnmount ( ) {
6872 this . timer && clearTimeout ( this . timer ) ;
6973 }
7074
7175 render ( ) {
72- let top ;
76+ let pos ;
7377 switch ( this . props . position ) {
7478 case 'top' :
75- top = 120 ;
79+ pos = this . props . positionValue ;
7680 break ;
7781 case 'center' :
78- top = height / 2 ;
82+ pos = height / 2 ;
7983 break ;
8084 case 'bottom' :
81- top = height - 160 ;
85+ pos = height - this . props . positionValue ;
8286 break ;
8387 }
8488 let view = this . state . isShow ?
8589 < View
86- style = { [ styles . container , { top : top } ] }
90+ style = { [ styles . container , { top : pos } ] }
8791 pointerEvents = "none"
8892 >
8993 < Animated . View
@@ -105,7 +109,6 @@ const styles = StyleSheet.create({
105109 } ,
106110 content : {
107111 backgroundColor : 'black' ,
108- opacity : OPACITY ,
109112 borderRadius : 5 ,
110113 padding : 10 ,
111114 } ,
@@ -121,10 +124,18 @@ Toast.propTypes = {
121124 'center' ,
122125 'bottom' ,
123126 ] ) ,
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
125132}
126133
127134Toast . defaultProps = {
128135 position : 'bottom' ,
129- textStyle : styles . text
136+ textStyle : styles . text ,
137+ positionValue : 120 ,
138+ fadeInDuration : 500 ,
139+ fadeOutDuration : 500 ,
140+ opacity : 1
130141}
0 commit comments