Skip to content

Commit 82fa87d

Browse files
authored
improve timer component (#945)
1 parent 0d75237 commit 82fa87d

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

packages/core/src/components/Timer.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ import React, {
55
useImperativeHandle,
66
forwardRef,
77
} from "react";
8-
import { Text, StyleSheet, TextStyle, StyleProp } from "react-native";
9-
10-
interface TimerProps {
8+
import {
9+
Text,
10+
StyleSheet,
11+
TextStyle,
12+
StyleProp,
13+
TextProps,
14+
} from "react-native";
15+
16+
interface TimerProps extends TextProps {
1117
style?: StyleProp<TextStyle>;
1218
initialTime?: number;
1319
updateInterval?: number;
@@ -35,10 +41,11 @@ const Timer = forwardRef<TimerHandle, TimerProps>(
3541
onTimerEnd,
3642
countDirection = "up",
3743
timerEndTime,
44+
...rest
3845
},
3946
ref
4047
) => {
41-
const defaultInitialTime = countDirection === "up" ? 0 : 100000;
48+
const defaultInitialTime = 0;
4249
const [time, setTime] = useState(initialTime ?? defaultInitialTime);
4350
const timerRef = useRef<NodeJS.Timeout | null>(null);
4451

@@ -64,7 +71,10 @@ const Timer = forwardRef<TimerHandle, TimerProps>(
6471
// Count down
6572
if (newTime <= 0 && countDirection === "down") {
6673
clearTimer();
67-
onTimerEnd?.();
74+
// Delay the onTimerEnd callback to ensure it triggers after the final time update
75+
setTimeout(() => {
76+
onTimerEnd?.();
77+
}, updateInterval);
6878
return 0;
6979
}
7080
// Count up
@@ -74,7 +84,10 @@ const Timer = forwardRef<TimerHandle, TimerProps>(
7484
newTime >= timerEndTime
7585
) {
7686
clearTimer();
77-
onTimerEnd?.();
87+
// Delay the onTimerEnd callback to ensure it triggers after the final time update
88+
setTimeout(() => {
89+
onTimerEnd?.();
90+
}, updateInterval);
7891
return timerEndTime;
7992
}
8093

@@ -123,15 +136,17 @@ const Timer = forwardRef<TimerHandle, TimerProps>(
123136
};
124137

125138
return (
126-
<Text style={[styles.defaultTimerStyle, style]}>{formatTime(time)}</Text>
139+
<Text {...rest} style={[styles.defaultTimerStyle, style]}>
140+
{formatTime(time)}
141+
</Text>
127142
);
128143
}
129144
);
130145

131146
const styles = StyleSheet.create({
132147
defaultTimerStyle: {
133148
fontSize: 24,
134-
textAlign: "center",
149+
textAlign: "left",
135150
},
136151
});
137152

0 commit comments

Comments
 (0)