forked from buhe/react-native-countdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (88 loc) · 2.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Created by guguyanhua on 12/11/15.
*/
import React, {
Image,
Text,
View,
StyleSheet,
TextInput,
TouchableHighlight,
TouchableWithoutFeedback
} from 'react-native';
var TimerMixin = require('react-timer-mixin');
var CountDown = React.createClass({
mixins: [TimerMixin],
getInitialState: function () {
return {
time: this.props.time ? this.props.time : 60,
disabled: true
};
},
componentDidMount(){
this._countdown();
},
render(){
var style = [styles.text];
var component;
if (this.state.disabled) {
style.push({color: 'gray'});
style.push(this.props.disabledTextStyle);
component =
<View
style={[styles.wrapper,this.props.buttonStyle]}
>
<TouchableWithoutFeedback
>
<Text style={[style]}>{this.props.text}({this.state.time})</Text>
</TouchableWithoutFeedback>
</View>
} else {
component =
<TouchableHighlight
style={[styles.wrapper,this.props.buttonStyle]}
onPress={this._onPress.bind(this)}
>
<Text style={[style,this.props.textStyle]}>{this.props.text}({this.state.time})</Text>
</TouchableHighlight>
}
return (
component
)
},
_onPress(){
if (this.state.disabled) {
//nothing
} else {
this.setState({disabled: true});
this._countdown();
if(this.props.onPress){
this.props.onPress();
}
}
},
_countdown(){
var timer = function () {
var time = this.state.time - 1;
this.setState({time: time});
if (time > 0) {
this.setTimeout(timer, 1000);
} else {
this.setState({disabled: false});
this.setState({time: this.props.time ? this.props.time : 60});
}
};
this.setTimeout(timer.bind(this), 1000);
}
});
var styles = StyleSheet.create({
text: {
color: 'black'
},
wrapper: {
padding: 10,
marginRight:10,
backgroundColor: '#e5e5e5',
}
});
module.exports = CountDown;