index.js 2.43 KB
Newer Older
afc163's avatar
afc163 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import React, { Component } from 'react';

function fixedZero(val) {
  return val * 1 < 10 ? `0${val}` : val;
}

class CountDown extends Component {
  constructor(props) {
    super(props);

    const { lastTime } = this.initTime(props);

    this.state = {
      lastTime,
    };
  }

  componentDidMount() {
    this.tick();
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.target !== nextProps.target) {
24
      clearTimeout(this.timer);
afc163's avatar
afc163 committed
25
      const { lastTime } = this.initTime(nextProps);
jim's avatar
jim committed
26 27 28 29 30 31 32 33
      this.setState(
        {
          lastTime,
        },
        () => {
          this.tick();
        }
      );
afc163's avatar
afc163 committed
34 35 36 37 38 39 40 41 42
    }
  }

  componentWillUnmount() {
    clearTimeout(this.timer);
  }

  timer = 0;
  interval = 1000;
jim's avatar
jim committed
43
  initTime = props => {
afc163's avatar
afc163 committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57
    let lastTime = 0;
    let targetTime = 0;
    try {
      if (Object.prototype.toString.call(props.target) === '[object Date]') {
        targetTime = props.target.getTime();
      } else {
        targetTime = new Date(props.target).getTime();
      }
    } catch (e) {
      throw new Error('invalid target prop', e);
    }

    lastTime = targetTime - new Date().getTime();
    return {
58
      lastTime: lastTime < 0 ? 0 : lastTime,
afc163's avatar
afc163 committed
59
    };
jim's avatar
jim committed
60
  };
afc163's avatar
afc163 committed
61 62 63
  // defaultFormat = time => (
  //  <span>{moment(time).format('hh:mm:ss')}</span>
  // );
jim's avatar
jim committed
64
  defaultFormat = time => {
afc163's avatar
afc163 committed
65 66 67
    const hours = 60 * 60 * 1000;
    const minutes = 60 * 1000;

68
    const h = Math.floor(time / hours);
jim's avatar
jim committed
69 70
    const m = Math.floor((time - h * hours) / minutes);
    const s = Math.floor((time - h * hours - m * minutes) / 1000);
afc163's avatar
afc163 committed
71
    return (
jim's avatar
jim committed
72 73 74
      <span>
        {fixedZero(h)}:{fixedZero(m)}:{fixedZero(s)}
      </span>
afc163's avatar
afc163 committed
75
    );
jim's avatar
jim committed
76
  };
afc163's avatar
afc163 committed
77 78 79 80 81 82 83
  tick = () => {
    const { onEnd } = this.props;
    let { lastTime } = this.state;

    this.timer = setTimeout(() => {
      if (lastTime < this.interval) {
        clearTimeout(this.timer);
jim's avatar
jim committed
84 85 86 87 88 89 90 91
        this.setState(
          {
            lastTime: 0,
          },
          () => {
            if (onEnd) {
              onEnd();
            }
92
          }
jim's avatar
jim committed
93
        );
afc163's avatar
afc163 committed
94 95
      } else {
        lastTime -= this.interval;
jim's avatar
jim committed
96 97 98 99 100 101 102 103
        this.setState(
          {
            lastTime,
          },
          () => {
            this.tick();
          }
        );
afc163's avatar
afc163 committed
104 105
      }
    }, this.interval);
jim's avatar
jim committed
106
  };
afc163's avatar
afc163 committed
107 108

  render() {
jim's avatar
jim committed
109
    const { format = this.defaultFormat, onEnd, ...rest } = this.props;
afc163's avatar
afc163 committed
110 111 112
    const { lastTime } = this.state;
    const result = format(lastTime);

jim's avatar
jim committed
113
    return <span {...rest}>{result}</span>;
afc163's avatar
afc163 committed
114 115 116 117
  }
}

export default CountDown;