11/*
22 * A smart countdown component for react-native apps.
33 * You may use it to handle different status when request a verification code.
4- * https://github.com/ljunb/react-native -countdown/
4+ * https://github.com/ljunb/rn -countdown/
55 * Released under the MIT license
66 * Copyright (c) 2017 ljunb <cookiejlim@gmail.com>
77 */
88
9- import React , { Component , PropTypes } from 'react' ;
9+ import React , { PureComponent , PropTypes } from 'react' ;
1010import {
1111 StyleSheet ,
1212 Text ,
@@ -19,15 +19,15 @@ import {
1919 * The status of countdown view.
2020 * None:default
2121 * Counting:counting down
22- * Over:countdown ends
22+ * Over:countdown over
2323 * */
2424const CountdownStatus = {
2525 None : 0 ,
2626 Counting : 1 ,
2727 Over : 2
2828} ;
2929
30- export default class Countdown extends Component {
30+ export default class Countdown extends PureComponent {
3131 static propTypes = {
3232 style : ViewPropTypes . style ,
3333 title : PropTypes . string ,
@@ -79,28 +79,38 @@ export default class Countdown extends Component {
7979 this . clearTimer ( ) ;
8080 } else if ( nextAppState === 'active' ) {
8181 if ( this . state . status !== CountdownStatus . Counting ) return ;
82+ this . turnsOnTimer ( ) ;
83+ }
84+ } ;
8285
83- const now = new Date ( ) ;
84- const diff = Math . round ( ( now - this . recodTime ) / 1000 ) ;
85- if ( this . state . second - diff <= 0 ) {
86- this . setState ( { status : CountdownStatus . Over , second : this . props . time } ) ;
87- } else {
88- this . setState ( {
89- status : CountdownStatus . Counting ,
90- second : this . state . second - diff
91- } , this . startTimer )
92- }
86+ turnsOnTimer = ( ) => {
87+ const now = new Date ( ) ;
88+ const diff = Math . round ( ( now - this . recodTime ) / 1000 ) ;
89+ if ( this . state . second - diff <= 0 ) {
90+ this . setState ( { status : CountdownStatus . Over , second : this . props . time } ) ;
91+ } else {
92+ this . setState ( {
93+ status : CountdownStatus . Counting ,
94+ second : this . state . second - diff
95+ } , this . startTimer )
9396 }
9497 } ;
9598
9699 handlePress = ( ) => {
97- const { shouldHandleBeforeCountdown, countingTitleTemplate } = this . props ;
100+ const { shouldHandleBeforeCountdown} = this . props ;
98101 const canStartTimer = shouldHandleBeforeCountdown ( ) ;
99102 if ( ! canStartTimer ) return ;
103+
100104 this . setState ( { status : CountdownStatus . Counting } , this . startTimer ) ;
105+ this . shouldShowWarningInfo ( ) ;
106+ } ;
101107
102- const showWarn = countingTitleTemplate . indexOf ( '{time}' ) === - 1 ;
103- showWarn && console . warn ( '[rn-countdown] The countingTitleTemplate string must conform to the format that contain `{time}`!' ) ;
108+ shouldShowWarningInfo = ( ) => {
109+ const { countingTitleTemplate} = this . props ;
110+ const isCorrectFormat = countingTitleTemplate . includes ( '{time}' ) ;
111+ if ( ! isCorrectFormat ) {
112+ console . warn ( "[rn-countdown] Warning: Failed prop format: Invalid prop `countingTitleTemplate` of format without substring `{time}`." ) ;
113+ }
104114 } ;
105115
106116 startTimer = ( ) => {
0 commit comments