2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2024-04-01 15:49:45 +08:00
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
import useForceUpdate from '../_util/hooks/useForceUpdate';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { StatisticProps } from './Statistic';
|
|
|
|
import Statistic from './Statistic';
|
2024-04-01 15:49:45 +08:00
|
|
|
import type { valueType } from './utils';
|
2022-05-07 14:31:54 +08:00
|
|
|
import { formatCountdown } from './utils';
|
2019-01-09 20:38:09 +08:00
|
|
|
|
|
|
|
const REFRESH_INTERVAL = 1000 / 30;
|
|
|
|
|
2022-12-01 14:33:51 +08:00
|
|
|
export interface CountdownProps extends StatisticProps {
|
2019-01-09 20:38:09 +08:00
|
|
|
format?: string;
|
2019-02-12 17:16:49 +08:00
|
|
|
onFinish?: () => void;
|
2023-01-05 14:07:33 +08:00
|
|
|
onChange?: (value?: valueType) => void;
|
2019-02-12 17:16:49 +08:00
|
|
|
}
|
|
|
|
|
2023-01-05 14:07:33 +08:00
|
|
|
function getTime(value?: valueType) {
|
2022-10-11 22:35:50 +08:00
|
|
|
return new Date(value as valueType).getTime();
|
2019-01-09 20:38:09 +08:00
|
|
|
}
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const Countdown: React.FC<CountdownProps> = (props) => {
|
2024-01-26 15:27:02 +08:00
|
|
|
const { value, format = 'HH:mm:ss', onChange, onFinish, ...rest } = props;
|
2019-01-09 20:38:09 +08:00
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
const forceUpdate = useForceUpdate();
|
2019-01-09 20:38:09 +08:00
|
|
|
|
2023-08-14 13:32:57 +08:00
|
|
|
const countdown = React.useRef<ReturnType<typeof setTimeout> | null>(null);
|
2019-01-09 20:38:09 +08:00
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
const stopTimer = () => {
|
|
|
|
onFinish?.();
|
|
|
|
if (countdown.current) {
|
|
|
|
clearInterval(countdown.current);
|
|
|
|
countdown.current = null;
|
2019-01-09 20:38:09 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
const syncTimer = () => {
|
2021-05-24 16:24:00 +08:00
|
|
|
const timestamp = getTime(value);
|
2022-10-11 22:35:50 +08:00
|
|
|
if (timestamp >= Date.now()) {
|
|
|
|
countdown.current = setInterval(() => {
|
|
|
|
forceUpdate();
|
|
|
|
onChange?.(timestamp - Date.now());
|
|
|
|
if (timestamp < Date.now()) {
|
|
|
|
stopTimer();
|
|
|
|
}
|
|
|
|
}, REFRESH_INTERVAL);
|
|
|
|
}
|
2019-01-09 20:38:09 +08:00
|
|
|
};
|
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
syncTimer();
|
|
|
|
return () => {
|
|
|
|
if (countdown.current) {
|
|
|
|
clearInterval(countdown.current);
|
|
|
|
countdown.current = null;
|
2019-02-12 17:16:49 +08:00
|
|
|
}
|
2022-10-11 22:35:50 +08:00
|
|
|
};
|
|
|
|
}, [value]);
|
2019-01-09 20:38:09 +08:00
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
const formatter: StatisticProps['formatter'] = (formatValue, config) =>
|
2022-10-11 22:35:50 +08:00
|
|
|
formatCountdown(formatValue, { ...config, format });
|
2019-01-09 20:38:09 +08:00
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
const valueRender: StatisticProps['valueRender'] = (node) =>
|
2022-10-11 22:35:50 +08:00
|
|
|
cloneElement(node, { title: undefined });
|
2019-01-09 20:38:09 +08:00
|
|
|
|
2024-01-26 15:27:02 +08:00
|
|
|
return <Statistic {...rest} value={value} valueRender={valueRender} formatter={formatter} />;
|
2022-10-11 22:35:50 +08:00
|
|
|
};
|
2019-01-09 20:38:09 +08:00
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
export default React.memo(Countdown);
|