-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (158 loc) · 4.05 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import React from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
} from 'react-native';
import _ from 'lodash';
import {sprintf} from 'sprintf-js';
const DEFAULT_BG_COLOR = '#FAB913';
const DEFAULT_TIME_TXT_COLOR = '#000';
const DEFAULT_DIGIT_TXT_COLOR = '#000';
const DEFAULT_TIME_TO_SHOW = ['D', 'H', 'M', 'S'];
class CountDown extends React.Component {
static propTypes = {
digitBgColor: PropTypes.string,
digitTxtColor: PropTypes.string,
timeTxtColor: PropTypes.string,
timeToShow: PropTypes.array,
size: PropTypes.number,
until: PropTypes.number,
onFinish: PropTypes.func,
onPress: PropTypes.func,
};
state = {
until: this.props.until,
};
componentDidMount() {
if (this.props.onFinish) {
this.onFinish = _.once(this.props.onFinish);
}
this.timer = setInterval(this.updateTimer, 1000);
}
componentWillUnmount() {
clearInterval(this.timer);
}
getTimeLeft = () => {
const {until} = this.state;
return {
seconds: until % 60,
minutes: parseInt(until / 60, 10) % 60,
hours: parseInt(until / (60 * 60), 10) % 24,
days: parseInt(until / (60 * 60 * 24), 10),
};
};
updateTimer = () => {
const {until} = this.state;
if (until <= 1) {
clearInterval(this.timer);
if (this.onFinish) {
this.onFinish();
}
}
this.setState({until: until - 1});
};
renderDigit = (d) => {
const {digitBgColor, digitTxtColor, size} = this.props;
return (
<View style={[
styles.digitCont,
{backgroundColor: digitBgColor},
{width: size * 2.3, height: size * 2.6},
]}>
<Text style={[
styles.digitTxt,
{fontSize: size},
{color: digitTxtColor}
]}>
{d}
</Text>
</View>
);
};
renderDoubleDigits = (label, digits) => {
const {timeTxtColor, size} = this.props;
return (
<View key={label} style={styles.doubleDigitCont}>
<View style={styles.timeInnerCont}>
{this.renderDigit(digits)}
<Text style={[
styles.timeTxt,
{fontSize: size / 1.2},
{color: timeTxtColor},
]}>
{label}
</Text>
</View>
</View>
);
};
renderCountDown = () => {
const {timeToShow} = this.props;
const {until} = this.state;
const {days, hours, minutes, seconds} = this.getTimeLeft();
const newTime = sprintf('%02d:%02d:%02d:%02d', days, hours, minutes, seconds).split(':');
const Component = this.props.onPress ? TouchableOpacity : View;
return (
<Component
style={styles.timeCont}
onPress={this.props.onPress}
>
{_.includes(timeToShow, 'D') ? this.renderDoubleDigits('d', newTime[0]) : null}
{_.includes(timeToShow, 'H') ? this.renderDoubleDigits('h', newTime[1]) : null}
{_.includes(timeToShow, 'M') ? this.renderDoubleDigits('m', newTime[2]) : null}
{_.includes(timeToShow, 'S') ? this.renderDoubleDigits('s', newTime[3]) : null}
</Component>
);
};
render() {
return (
<View style={this.props.style}>
{this.renderCountDown()}
</View>
);
}
}
CountDown.defaultProps = {
digitBgColor: DEFAULT_BG_COLOR,
digitTxtColor: DEFAULT_DIGIT_TXT_COLOR,
timeTxtColor: DEFAULT_TIME_TXT_COLOR,
timeToShow: DEFAULT_TIME_TO_SHOW,
until: 0,
size: 15,
};
const styles = StyleSheet.create({
timeCont: {
flexDirection: 'row',
justifyContent: 'center',
},
timeTxt: {
color: 'white',
marginBottom: 2,
marginTop: 5,
marginLeft: -8,
backgroundColor: 'transparent',
},
timeInnerCont: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
digitCont: {
borderRadius: 5,
marginHorizontal: 2,
alignItems: 'center',
justifyContent: 'center',
},
doubleDigitCont: {
justifyContent: 'center',
alignItems: 'center',
},
digitTxt: {
color: 'white',
fontWeight: 'bold',
},
});
module.exports = CountDown;