index.js 2.52 KB
Newer Older
afc163's avatar
afc163 committed
1 2 3 4 5
import React, { Component } from 'react';

function fixedZero(val) {
  return val * 1 < 10 ? `0${val}` : val;
}
jim's avatar
jim committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
const initTime = props => {
  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 {
    lastTime: lastTime < 0 ? 0 : lastTime,
  };
};
afc163's avatar
afc163 committed
24 25

class CountDown extends Component {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
26

afc163's avatar
afc163 committed
27 28
  constructor(props) {
    super(props);
jim's avatar
jim committed
29
    const { lastTime } = initTime(props);
afc163's avatar
afc163 committed
30 31 32 33 34
    this.state = {
      lastTime,
    };
  }

35 36 37 38 39 40 41 42 43 44
  static getDerivedStateFromProps(nextProps, preState) {
    const { lastTime } = initTime(nextProps);
    if (preState.lastTime !== lastTime) {
      return {
        lastTime,
      };
    }
    return null;
  }

afc163's avatar
afc163 committed
45 46 47 48
  componentDidMount() {
    this.tick();
  }

jim's avatar
jim committed
49
  componentDidUpdate(prevProps) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
50 51
    const { target } = this.props;
    if (target !== prevProps.target) {
52
      clearTimeout(this.timer);
jim's avatar
jim committed
53
      this.tick();
afc163's avatar
afc163 committed
54 55 56 57 58 59 60
    }
  }

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

61 62 63
  timer = 0;

  interval = 1000;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
64

afc163's avatar
afc163 committed
65 66 67
  // defaultFormat = time => (
  //  <span>{moment(time).format('hh:mm:ss')}</span>
  // );
jim's avatar
jim committed
68
  defaultFormat = time => {
afc163's avatar
afc163 committed
69 70 71
    const hours = 60 * 60 * 1000;
    const minutes = 60 * 1000;

72
    const h = Math.floor(time / hours);
jim's avatar
jim committed
73 74
    const m = Math.floor((time - h * hours) / minutes);
    const s = Math.floor((time - h * hours - m * minutes) / 1000);
afc163's avatar
afc163 committed
75
    return (
jim's avatar
jim committed
76
      <span>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
77 78 79 80 81
        {fixedZero(h)}
        :
        {fixedZero(m)}
        :
        {fixedZero(s)}
jim's avatar
jim committed
82
      </span>
afc163's avatar
afc163 committed
83
    );
jim's avatar
jim committed
84
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
85

afc163's avatar
afc163 committed
86 87 88 89 90 91 92
  tick = () => {
    const { onEnd } = this.props;
    let { lastTime } = this.state;

    this.timer = setTimeout(() => {
      if (lastTime < this.interval) {
        clearTimeout(this.timer);
jim's avatar
jim committed
93 94 95 96 97 98 99 100
        this.setState(
          {
            lastTime: 0,
          },
          () => {
            if (onEnd) {
              onEnd();
            }
101
          }
jim's avatar
jim committed
102
        );
afc163's avatar
afc163 committed
103 104
      } else {
        lastTime -= this.interval;
jim's avatar
jim committed
105 106 107 108 109 110 111 112
        this.setState(
          {
            lastTime,
          },
          () => {
            this.tick();
          }
        );
afc163's avatar
afc163 committed
113 114
      }
    }, this.interval);
jim's avatar
jim committed
115
  };
afc163's avatar
afc163 committed
116 117

  render() {
jim's avatar
jim committed
118
    const { format = this.defaultFormat, onEnd, ...rest } = this.props;
afc163's avatar
afc163 committed
119 120 121
    const { lastTime } = this.state;
    const result = format(lastTime);

jim's avatar
jim committed
122
    return <span {...rest}>{result}</span>;
afc163's avatar
afc163 committed
123 124 125 126
  }
}

export default CountDown;