forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountdown.jsx
97 lines (83 loc) · 2.04 KB
/
Countdown.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import * as moment from 'moment';
import interopDefault from '../_util/interopDefault';
import { cloneElement } from '../_util/vnode';
import { initDefaultProps } from '../_util/props-util';
import Statistic, { StatisticProps } from './Statistic';
import { formatCountdown } from './utils';
const REFRESH_INTERVAL = 1000 / 30;
function getTime(value) {
return interopDefault(moment)(value).valueOf();
}
export default {
name: 'AStatisticCountdown',
props: initDefaultProps(StatisticProps, {
format: 'HH:mm:ss',
}),
created() {
this.countdownId = undefined;
},
mounted() {
this.syncTimer();
},
updated() {
this.syncTimer();
},
beforeDestroy() {
this.stopTimer();
},
methods: {
syncTimer() {
const { value } = this.$props;
const timestamp = getTime(value);
if (timestamp >= Date.now()) {
this.startTimer();
} else {
this.stopTimer();
}
},
startTimer() {
if (this.countdownId) return;
this.countdownId = window.setInterval(() => {
this.$refs.statistic.$forceUpdate();
this.syncTimer();
}, REFRESH_INTERVAL);
},
stopTimer() {
const { value } = this.$props;
if (this.countdownId) {
clearInterval(this.countdownId);
this.countdownId = undefined;
const timestamp = getTime(value);
if (timestamp < Date.now()) {
this.$emit('finish');
}
}
},
formatCountdown({ value, config }) {
const { format } = this.$props;
return formatCountdown(value, { ...config, format });
},
// Countdown do not need display the timestamp
valueRenderHtml: node =>
cloneElement(node, {
props: {
title: undefined,
},
}),
},
render() {
return (
<Statistic
ref="statistic"
{...{
props: {
...this.$props,
valueRender: this.valueRenderHtml,
formatter: this.formatCountdown,
},
on: this.$listeners,
}}
/>
);
},
};