@jdcfe/yep-react
Version:
一套移动端的React组件库
158 lines (140 loc) • 5.57 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import * as React from 'react';
import classNames from 'classnames';
import noop from '../_utils/noop';
import TimerService from './TimerService';
var addZero = function addZero(n) {
if (n < 10) {
return "0".concat(n);
}
return "".concat(n);
};
var getFormateTime = function getFormateTime(time) {
var dayStr = "".concat(Math.floor(time / 60 / 60 / 24));
var hourStr = addZero(Math.floor(time / 60 / 60 % 24));
var minuteStr = addZero(Math.floor(time / 60 % 60));
var secondStr = addZero(Math.floor(time % 60));
var dayNum = Math.floor(time / 60 / 60 / 24);
var hourNum = Math.floor(time / 60 / 60 % 24);
var minuteNum = Math.floor(time / 60 % 60);
var secondNum = Math.floor(time % 60);
return {
dayStr: dayStr,
hourStr: hourStr,
minuteStr: minuteStr,
secondStr: secondStr,
dayNum: dayNum,
hourNum: hourNum,
minuteNum: minuteNum,
secondNum: secondNum
};
};
var CountDown = /*#__PURE__*/function (_React$PureComponent) {
_inherits(CountDown, _React$PureComponent);
var _super = _createSuper(CountDown);
function CountDown(props) {
var _this;
_classCallCheck(this, CountDown);
_this = _super.call(this, props);
_this.state = {
seconds: Math.round(props.leftTime)
};
_this.countDown = _this.countDown.bind(_assertThisInitialized(_this));
_this.renderText = _this.renderText.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(CountDown, [{
key: "componentDidMount",
value: function componentDidMount() {
TimerService.register(this.countDown);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
TimerService.unregister(this.countDown);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (this.props.leftTime !== prevProps.leftTime) {
//清掉定时器,再重新注册定时器,才能保证1s后减1,否则是按之前的定时器去减
//暂时不按这种方式,有性能
//TimerService.unregister(this.countDown);
//TimerService.register(this.countDown);
this.setState({
seconds: Math.round(this.props.leftTime)
});
}
if (this.props.leftTime >= 0 && this.state.seconds <= 0) {
TimerService.register(this.countDown);
}
}
}, {
key: "countDown",
value: function countDown() {
var _this$props = this.props,
onEnd = _this$props.onEnd,
onChange = _this$props.onChange;
var seconds = this.state.seconds - 1;
this.setState({
seconds: seconds
});
onChange && onChange(seconds);
if (seconds <= 0) {
TimerService.unregister(this.countDown);
onEnd && onEnd();
}
}
}, {
key: "renderText",
value: function renderText(date) {
var _this$props2 = this.props,
children = _this$props2.children,
prefixCls = _this$props2.prefixCls;
if (children) {
return children(date);
}
var hourStr = date.hourStr,
minuteStr = date.minuteStr,
secondStr = date.secondStr;
return /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-default")
}, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-default-box")
}, hourStr), ":", /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-default-box")
}, minuteStr), ":", /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-default-box")
}, secondStr));
}
}, {
key: "render",
value: function render() {
var _this$props3 = this.props,
prefixCls = _this$props3.prefixCls,
overText = _this$props3.overText,
className = _this$props3.className;
var cls = classNames(prefixCls, className);
var restTime = this.state.seconds;
var isOver = restTime <= 0;
var date = getFormateTime(restTime);
return /*#__PURE__*/React.createElement("div", {
className: cls
}, isOver && overText ? overText : this.renderText(date));
}
}]);
return CountDown;
}(React.PureComponent);
export { CountDown as default };
CountDown.defaultProps = {
prefixCls: 'Yep-count-down',
style: {},
onEnd: noop
};