2019-01-09 20:38:09 +08:00
|
|
|
import * as React from 'react';
|
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';
|
2022-10-11 22:35:50 +08:00
|
|
|
import type { countdownValueType, FormatConfig, 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;
|
|
|
|
|
2023-01-03 11:46:48 +08:00
|
|
|
export interface CountdownProps extends StatisticProps {
|
2019-01-09 20:38:09 +08:00
|
|
|
value?: countdownValueType;
|
|
|
|
format?: string;
|
2019-02-12 17:16:49 +08:00
|
|
|
onFinish?: () => void;
|
2021-05-24 16:24:00 +08:00
|
|
|
onChange?: (value?: countdownValueType) => void;
|
2019-02-12 17:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function getTime(value?: countdownValueType) {
|
2022-10-11 22:35:50 +08:00
|
|
|
return new Date(value as valueType).getTime();
|
2019-01-09 20:38:09 +08:00
|
|
|
}
|
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
const Countdown: React.FC<CountdownProps> = props => {
|
|
|
|
const { value, format = 'HH:mm:ss', onChange, onFinish } = 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
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
const countdown = React.useRef<NodeJS.Timer | 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
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
const formatter = (formatValue: countdownValueType, config: FormatConfig) =>
|
|
|
|
formatCountdown(formatValue, { ...config, format });
|
2019-01-09 20:38:09 +08:00
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
const valueRender = (node: React.ReactElement<HTMLDivElement>) =>
|
|
|
|
cloneElement(node, { title: undefined });
|
2019-01-09 20:38:09 +08:00
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
return <Statistic {...props} valueRender={valueRender} formatter={formatter} />;
|
|
|
|
};
|
2019-01-09 20:38:09 +08:00
|
|
|
|
2022-10-11 22:35:50 +08:00
|
|
|
export default React.memo(Countdown);
|