Skip to content

Commit c30252b

Browse files
committed
Update to 0.1.0
1 parent c641f11 commit c30252b

3 files changed

Lines changed: 43 additions & 24 deletions

File tree

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ import CountdownView from 'rn-countdown';
3636

3737
export default class RNCountdownDemo extends Component {
3838

39+
state = {
40+
hasText: false
41+
};
3942
phoneNumber = '';
4043

4144
shouldHandleBeforeCountdown = () => {
@@ -49,7 +52,15 @@ export default class RNCountdownDemo extends Component {
4952
this.countdown && this.countdown.stopCountdown();
5053
};
5154

55+
handleChangeText = text => {
56+
this.phoneNumber = text;
57+
this.setState({hasText: !!this.phoneNumber})
58+
};
59+
5260
render() {
61+
const style = this.state.hasText ? {backgroundColor: 'rgb(59, 197, 81)', borderWidth: 0} : {};
62+
const title = this.state.hasText ? {color: '#fff'} : {};
63+
5364
return (
5465
<View style={styles.container}>
5566
<View style={styles.phoneCell}>
@@ -59,16 +70,16 @@ export default class RNCountdownDemo extends Component {
5970
style={styles.input}
6071
placeholder="请输入手机号码"
6172
underlineColorAndroid="transparent"
62-
onChangeText={text => this.phoneNumber = text}
73+
onChangeText={this.handleChangeText}
6374
/>
6475
</View>
6576
<CountdownView
6677
ref={r => this.countdown = r}
6778
time={10}
6879
title="发送验证码"
6980
overTitle="重新发送"
70-
style={styles.countdown}
71-
titleStyle={styles.countdownTitle}
81+
style={[styles.countdown, style]}
82+
titleStyle={[styles.countdownTitle, title]}
7283
countingTitleTemplate="发送中({time})"
7384
countingStyle={styles.countingdown}
7485
countingTitleStyle={styles.countingTitle}
@@ -111,15 +122,13 @@ const styles = StyleSheet.create({
111122
fontSize: 14
112123
},
113124
countdown: {
114-
backgroundColor: 'rgb(59, 197, 81)',
115125
borderRadius: 15,
116-
borderWidth: 0
117126
},
118127
countingdown: {
119128
backgroundColor: 'transparent',
120129
borderWidth: StyleSheet.hairlineWidth
121130
},
122-
countdownTitle: {color: '#fff'},
131+
countdownTitle: {color: '#ccc'},
123132
countingTitle: {color: '#ccc'}
124133
});
125134
```

index.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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';
1010
import {
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
* */
2424
const 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 = () => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rn-countdown",
3-
"version": "0.0.5",
3+
"version": "0.1.0",
44
"description": "A smart countdown component for react-native apps. You may use it to handle different status when request a verification code.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)