index.js 2.48 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 {
26 27 28
  timer = 0;

  interval = 1000;
陈帅's avatar
陈帅 committed
29

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

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

afc163's avatar
afc163 committed
48 49 50 51
  componentDidMount() {
    this.tick();
  }

jim's avatar
jim committed
52
  componentDidUpdate(prevProps) {
陈帅's avatar
陈帅 committed
53 54
    const { target } = this.props;
    if (target !== prevProps.target) {
55
      clearTimeout(this.timer);
jim's avatar
jim committed
56
      this.tick();
afc163's avatar
afc163 committed
57 58 59 60 61 62 63 64 65 66
    }
  }

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

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

71
    const h = Math.floor(time / hours);
jim's avatar
jim committed
72 73
    const m = Math.floor((time - h * hours) / minutes);
    const s = Math.floor((time - h * hours - m * minutes) / 1000);
afc163's avatar
afc163 committed
74
    return (
jim's avatar
jim committed
75
      <span>
76
        {fixedZero(h)}:{fixedZero(m)}:{fixedZero(s)}
jim's avatar
jim committed
77
      </span>
afc163's avatar
afc163 committed
78
    );
jim's avatar
jim committed
79
  };
陈帅's avatar
陈帅 committed
80

afc163's avatar
afc163 committed
81 82 83 84 85 86 87
  tick = () => {
    const { onEnd } = this.props;
    let { lastTime } = this.state;

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

  render() {
jim's avatar
jim committed
113
    const { format = this.defaultFormat, onEnd, ...rest } = this.props;
afc163's avatar
afc163 committed
114 115 116
    const { lastTime } = this.state;
    const result = format(lastTime);

jim's avatar
jim committed
117
    return <span {...rest}>{result}</span>;
afc163's avatar
afc163 committed
118 119 120 121
  }
}

export default CountDown;