@@ -14,21 +14,29 @@ import {
1414 Dimensions ,
1515 Text ,
1616} from 'react-native'
17- export const DURATION = { LENGTH_LONG : 2000 , LENGTH_SHORT : 500 } ;
17+
18+ export const DURATION = {
19+ LENGTH_LONG : 2000 ,
20+ LENGTH_SHORT : 500 ,
21+ FOREVER : 0 ,
22+ } ;
23+
1824const { height, width} = Dimensions . get ( 'window' ) ;
1925
2026export default class Toast extends Component {
2127
2228 constructor ( props ) {
2329 super ( props ) ;
30+
2431 this . state = {
2532 isShow : false ,
2633 text : '' ,
2734 opacityValue : new Animated . Value ( this . props . opacity ) ,
2835 }
2936 }
37+
3038 show ( text , duration ) {
31- this . duration = duration || DURATION . LENGTH_SHORT ;
39+ this . duration = typeof duration === 'number' ? duration : DURATION . LENGTH_SHORT ;
3240
3341 this . setState ( {
3442 isShow : true ,
@@ -43,14 +51,16 @@ export default class Toast extends Component {
4351 }
4452 ) . start ( ( ) => {
4553 this . isShow = true ;
46- this . close ( ) ;
54+ if ( duration !== DURATION . FOREVER ) this . close ( ) ;
4755 } ) ;
4856 }
4957
50- close ( ) {
51- let delay = this . duration ;
52-
53- if ( ! this . isShow ) return ;
58+ close ( duration ) {
59+ let delay = typeof duration === 'undefined' ? this . duration : duration ;
60+
61+ if ( delay === DURATION . FOREVER ) delay = this . props . defaultCloseDelay || 250 ;
62+
63+ if ( ! this . isShow && ! this . state . isShow ) return ;
5464 this . timer && clearTimeout ( this . timer ) ;
5565 this . timer = setTimeout ( ( ) => {
5666 Animated . timing (
@@ -85,7 +95,8 @@ export default class Toast extends Component {
8595 pos = height - this . props . positionValue ;
8696 break ;
8797 }
88- let view = this . state . isShow ?
98+
99+ const view = this . state . isShow ?
89100 < View
90101 style = { [ styles . container , { top : pos } ] }
91102 pointerEvents = "none"
0 commit comments