Skip to content

Commit dcf2796

Browse files
committed
Add 'timeFontStyle' prop.
1 parent 1c095e1 commit dcf2796

3 files changed

Lines changed: 51 additions & 17 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ titleStyle | object | Yes | none | font style of countdo
163163
countingStyle | ViewPropTypes | Yes | none | custom style when counting down
164164
countingTitleTemplate | string | Yes | {time}s后重新获取 | counting down title, must conform to the format that contain `{time}`
165165
countingTitleStyle | object | Yes | none | custom title style when counting down
166+
timeFontStyle | FontPropTypes | Yes | none | font style of time
166167
shouldStartCountdown | function | Yes | return true | before start countdown, you can use this function to handle some business logic, return true to allow countdown, otherwise return false
167168
onNetworkFailed | function | Yes | none | invoke when the network is failed, so the countdown timer will be invalid in this situation, maybe you will use it to show some message for users
168169

index.js

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default class Countdown extends PureComponent {
5555
countingStyle: ViewPropTypes.style,
5656
countingTitleTemplate: PropTypes.string,
5757
countingTitleStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
58+
timeFontStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
5859
shouldStartCountdown: PropTypes.func,
5960
onNetworkFailed: PropTypes.func,
6061
};
@@ -180,33 +181,65 @@ export default class Countdown extends PureComponent {
180181

181182
clearTimer = () => this.timer && clearInterval(this.timer);
182183

183-
render() {
184-
const { status, second } = this.state;
184+
getCountingComponent = () => {
185+
const { second } = this.state;
185186
const {
186-
title, style, overTitle, titleStyle,
187-
countingTitleTemplate, countingStyle, countingTitleStyle
187+
countingTitleTemplate,
188+
titleStyle, countingStyle, countingTitleStyle, timeFontStyle
188189
} = this.props;
189190

190-
let promptTitle = title,
191-
containerStyle = [styles.container, style],
192-
textStyle = [styles.title, titleStyle];
193-
194-
if (status === CountdownStatus.Counting) {
195-
promptTitle = countingTitleTemplate.replace('{time}', second);
196-
containerStyle.push(countingStyle);
197-
textStyle.push(countingTitleStyle);
198-
} else if (status === CountdownStatus.Over) {
199-
promptTitle = overTitle;
191+
const templateIndex = countingTitleTemplate.indexOf('{time}');
192+
const titleLength = countingTitleTemplate.length;
193+
const baseStyle = [styles.title, titleStyle, countingTitleStyle];
194+
const timeComponent = <Text style={timeFontStyle}>{second}</Text>;
195+
196+
if (countingTitleTemplate === '{time}') {
197+
return <Text style={[baseStyle, timeFontStyle]}>{second}</Text>;
198+
} else if (templateIndex === 0 && titleLength > 6) {
199+
const restText = countingTitleTemplate.split('}')[1];
200+
return (
201+
<Text style={baseStyle}>
202+
{timeComponent}
203+
{restText}
204+
</Text>
205+
)
206+
} else if (templateIndex === titleLength - 6) {
207+
const restText = countingTitleTemplate.split('{')[0];
208+
return (
209+
<Text style={baseStyle}>
210+
{restText}
211+
{timeComponent}
212+
</Text>
213+
)
200214
}
201215

216+
return (
217+
<Text style={[styles.title, titleStyle, countingTitleStyle]}>
218+
{countingTitleTemplate.split('{time}')[0]}
219+
{timeComponent}
220+
{countingTitleTemplate.split('{time}')[1]}
221+
</Text>
222+
)
223+
};
224+
225+
render() {
226+
const { status, second } = this.state;
227+
const { title, style, overTitle, titleStyle } = this.props;
228+
const isCounting = status === CountdownStatus.Counting;
229+
202230
return (
203231
<TouchableOpacity
204232
disabled={ status === CountdownStatus.Counting }
205233
activeOpacity={0.75}
206-
style={containerStyle}
234+
style={[styles.container, style]}
207235
onPress={this.handlePress}
208236
>
209-
<Text style={textStyle}>{promptTitle}</Text>
237+
{isCounting && this.getCountingComponent()}
238+
{!isCounting &&
239+
<Text style={[styles.title, titleStyle]}>
240+
{status === CountdownStatus.None ? title : overTitle}
241+
</Text>
242+
}
210243
</TouchableOpacity>
211244
);
212245
}

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.3.2",
3+
"version": "0.4.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)