66 * @flow
77 */
88
9- import React , { Component , PropTypes } from 'react' ;
9+ import React , { Component } from 'react' ;
1010import {
1111 StyleSheet ,
1212 View ,
1313 Animated ,
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 : 1000 } ;
1818const { height, width} = Dimensions . get ( 'window' ) ;
19- const OPACITY = 0.6 ;
19+ const OPACITY = 0.6 ;
2020
2121export 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+
3323 constructor ( props ) {
3424 super ( props ) ;
3525 this . state = {
3626 isShow : false ,
3727 text : '' ,
38- opacityValue :new Animated . Value ( OPACITY ) ,
28+ opacityValue : new Animated . Value ( OPACITY ) ,
3929 }
4030 }
4131 show ( text , duration ) {
42- this . duration = duration || DURATION . LENGTH_SHORT ;
32+ this . duration = duration || DURATION . LENGTH_SHORT ;
4333 this . setState ( {
4434 isShow : true ,
4535 text : text ,
4636 } ) ;
47- this . isShow = true ;
37+ this . isShow = true ;
4838 this . state . opacityValue . setValue ( OPACITY )
4939 this . close ( ) ;
5040 }
5141
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 ;
5450 this . timer && clearTimeout ( this . timer ) ;
5551 this . timer = setTimeout ( ( ) => {
5652 Animated . timing (
5753 this . state . opacityValue ,
5854 {
5955 toValue : 0.0 ,
60- duration :500 ,
56+ duration : animationDuration ,
6157 }
62- ) . start ( ( ) => {
58+ ) . start ( ( ) => {
6359 this . setState ( {
6460 isShow : false ,
6561 } ) ;
66- this . isShow = false ;
62+ this . isShow = false ;
6763 } ) ;
68- } , this . duration ) ;
64+ } , closeDuration ) ;
6965 }
66+
7067 componentWillUnmount ( ) {
7168 this . timer && clearTimeout ( this . timer ) ;
7269 }
7370
7471 render ( ) {
7572 let top ;
76- switch ( this . props . position ) {
73+ switch ( this . props . position ) {
7774 case 'top' :
78- top = 120 ;
75+ top = 120 ;
7976 break ;
8077 case 'center' :
81- top = height / 2 ;
78+ top = height / 2 ;
8279 break ;
8380 case 'bottom' :
84- top = height - 160 ;
81+ top = height - 160 ;
8582 break ;
8683 }
8784 let view = this . state . isShow ?
8885 < View
89- style = { [ styles . container , { top :top } ] }
86+ style = { [ styles . container , { top : top } ] }
9087 pointerEvents = "none"
91- >
92- < Animated . View
93- style = { [ styles . content , { opacity :this . state . opacityValue } , this . props . style ] }
9488 >
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 >
9693 </ Animated . View >
9794 </ View > : null ;
9895 return view ;
9996 }
10097}
98+
10199const styles = StyleSheet . create ( {
102100 container : {
103101 position : 'absolute' ,
@@ -111,7 +109,22 @@ const styles = StyleSheet.create({
111109 borderRadius : 5 ,
112110 padding : 10 ,
113111 } ,
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