|
| 1 | +import React, { |
| 2 | + useState, |
| 3 | + useEffect, |
| 4 | + useRef, |
| 5 | + useImperativeHandle, |
| 6 | + forwardRef, |
| 7 | +} from "react"; |
| 8 | +import { Text, StyleSheet, TextStyle, StyleProp } from "react-native"; |
| 9 | + |
| 10 | +interface TimerProps { |
| 11 | + style?: StyleProp<TextStyle>; |
| 12 | + initialTime?: number; |
| 13 | + updateInterval?: number; |
| 14 | + format?: "ss" | "mm:ss" | "hh:mm:ss" | "ss:ms" | "mm:ss:ms" | "hh:mm:ss:ms"; |
| 15 | + onTimerChange?: (time: number) => void; |
| 16 | + onTimerEnd?: () => void; |
| 17 | + countDirection?: "up" | "down"; |
| 18 | + timerEndTime?: number; |
| 19 | +} |
| 20 | + |
| 21 | +export interface TimerHandle { |
| 22 | + start: () => void; |
| 23 | + stop: () => void; |
| 24 | + reset: (newTime?: number) => void; |
| 25 | +} |
| 26 | + |
| 27 | +const Timer = forwardRef<TimerHandle, TimerProps>( |
| 28 | + ( |
| 29 | + { |
| 30 | + style, |
| 31 | + initialTime, |
| 32 | + updateInterval = 1000, |
| 33 | + format = "mm:ss", |
| 34 | + onTimerChange, |
| 35 | + onTimerEnd, |
| 36 | + countDirection = "up", |
| 37 | + timerEndTime, |
| 38 | + }, |
| 39 | + ref |
| 40 | + ) => { |
| 41 | + const defaultInitialTime = countDirection === "up" ? 0 : 100000; |
| 42 | + const [time, setTime] = useState(initialTime ?? defaultInitialTime); |
| 43 | + const timerRef = useRef<NodeJS.Timeout | null>(null); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + onTimerChange?.(time); |
| 47 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 48 | + }, [time]); |
| 49 | + |
| 50 | + useImperativeHandle(ref, () => ({ |
| 51 | + start: startTimer, |
| 52 | + stop: stopTimer, |
| 53 | + reset: resetTimer, |
| 54 | + })); |
| 55 | + |
| 56 | + const startTimer = () => { |
| 57 | + if (timerRef.current) return; |
| 58 | + timerRef.current = setInterval(() => { |
| 59 | + setTime((prevTime) => { |
| 60 | + const newTime = |
| 61 | + countDirection === "up" |
| 62 | + ? prevTime + updateInterval |
| 63 | + : prevTime - updateInterval; |
| 64 | + // Count down |
| 65 | + if (newTime <= 0 && countDirection === "down") { |
| 66 | + clearTimer(); |
| 67 | + onTimerEnd?.(); |
| 68 | + return 0; |
| 69 | + } |
| 70 | + // Count up |
| 71 | + if ( |
| 72 | + countDirection === "up" && |
| 73 | + timerEndTime !== undefined && |
| 74 | + newTime >= timerEndTime |
| 75 | + ) { |
| 76 | + clearTimer(); |
| 77 | + onTimerEnd?.(); |
| 78 | + return timerEndTime; |
| 79 | + } |
| 80 | + |
| 81 | + return newTime; |
| 82 | + }); |
| 83 | + }, updateInterval); |
| 84 | + }; |
| 85 | + |
| 86 | + const stopTimer = () => clearTimer(); |
| 87 | + |
| 88 | + const resetTimer = ( |
| 89 | + newTime: number = initialTime ?? defaultInitialTime |
| 90 | + ) => { |
| 91 | + clearTimer(); |
| 92 | + setTime(newTime); |
| 93 | + }; |
| 94 | + |
| 95 | + const clearTimer = () => { |
| 96 | + if (timerRef.current) { |
| 97 | + clearInterval(timerRef.current); |
| 98 | + timerRef.current = null; |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + useEffect(() => { |
| 103 | + return () => clearTimer(); |
| 104 | + }, []); |
| 105 | + |
| 106 | + const formatTime = (milliseconds: number): string => { |
| 107 | + const totalSeconds = Math.floor(milliseconds / 1000); |
| 108 | + const hours = Math.floor(totalSeconds / 3600); |
| 109 | + const minutes = Math.floor((totalSeconds - hours * 3600) / 60); |
| 110 | + const seconds = totalSeconds - hours * 3600 - minutes * 60; |
| 111 | + const ms = milliseconds % 1000; |
| 112 | + |
| 113 | + const formattedHours = String(hours).padStart(2, "0"); |
| 114 | + const formattedMinutes = String(minutes).padStart(2, "0"); |
| 115 | + const formattedSeconds = String(seconds).padStart(2, "0"); |
| 116 | + const formattedMs = String(ms).padStart(3, "0"); |
| 117 | + |
| 118 | + return format |
| 119 | + .replace("hh", formattedHours) |
| 120 | + .replace("mm", formattedMinutes) |
| 121 | + .replace("ss", formattedSeconds) |
| 122 | + .replace("ms", formattedMs); |
| 123 | + }; |
| 124 | + |
| 125 | + return ( |
| 126 | + <Text style={[styles.defaultTimerStyle, style]}>{formatTime(time)}</Text> |
| 127 | + ); |
| 128 | + } |
| 129 | +); |
| 130 | + |
| 131 | +const styles = StyleSheet.create({ |
| 132 | + defaultTimerStyle: { |
| 133 | + fontSize: 24, |
| 134 | + textAlign: "center", |
| 135 | + }, |
| 136 | +}); |
| 137 | + |
| 138 | +export default Timer; |
0 commit comments