ukelli-ui
Version:
ukelli-ui 是基于 React 的 UI 库,提供简约和功能齐全的组件,可高度定制的组件接口,灵活的配置,提供给开发者另一种开发思路,也致力于尝试不同的组件使用和开发方向。
270 lines (227 loc) • 10.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _basicHelper = require("basic-helper");
var _countdownSvgBg = _interopRequireDefault(require("./countdown-svg-bg"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var timeTitleMapper = {
hour: '时',
min: '分',
sec: '秒'
};
var Countdown =
/*#__PURE__*/
function (_Component) {
_inherits(Countdown, _Component);
function Countdown(props) {
var _this;
_classCallCheck(this, Countdown);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Countdown).call(this, props));
_this.state = {
isTimerStart: false,
countdown: 0
};
return _this;
}
_createClass(Countdown, [{
key: "startCountdown",
value: function startCountdown() {
var start = this.props.start;
if (this.state.isTimerStart || start === 0) return;
this._clearTimer();
this.interval = this.startTimer();
} // getSameJumpElem() {
// const {jumpClass = ''} = this.props;
// if(!jumpClass) return;
// this.jumpElem = document.querySelector('.' + jumpClass) || null;
// }
}, {
key: "setJumpElemCount",
value: function setJumpElemCount(timeObj) {
if (!this.jumpElem) return;
this.jumpElem.innerHTML = "".concat(timeObj.hour, ":").concat(timeObj.min, ":").concat(timeObj.sec) || 0;
} // componentWillReceiveProps(nextProps) {
// if(nextProps.start !== this.props.start) {
// this.clearTimer();
// }
// }
}, {
key: "shouldComponentUpdate",
value: function shouldComponentUpdate(nextProps, nextState) {
var isReceiveNewStart = this.props.start !== nextProps.start;
var isNewCount = this.state.countdown !== nextState.countdown;
if (nextProps.start !== this.props.start) {
this.clearTimer();
}
return isNewCount || !nextState.isTimeout || !nextState.isTimerStart || isReceiveNewStart;
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
this.startCountdown();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.startCountdown();
}
}, {
key: "_clearTimer",
value: function _clearTimer() {
this.interval && clearInterval(this.interval);
this.interval = null;
}
}, {
key: "clearTimer",
value: function clearTimer() {
this._clearTimer();
this.setState({
isTimerStart: false
});
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.clearTimer();
}
}, {
key: "startTimer",
value: function startTimer() {
// if (this.state.isTimerStart) return;
var _this$props = this.props,
start = _this$props.start,
_this$props$freq = _this$props.freq,
freq = _this$props$freq === void 0 ? 10 : _this$props$freq,
countdownNotifyTimer = _this$props.countdownNotifyTimer,
onCountdownNotify = _this$props.onCountdownNotify,
onTimeout = _this$props.onTimeout;
var self = this;
var countdown = start - 1;
self.setState({
isTimerStart: true,
// isTimeout: false,
countdown: countdown
});
var oneRound = setInterval(function () {
countdown--;
self.setState({
countdown: countdown < 0 ? 0 : countdown
});
if (countdown == +countdownNotifyTimer) (0, _basicHelper.Call)(onCountdownNotify, countdown);
if (countdown === -1) {
countdown = freq - 1;
onTimeout(); // clearInterval(oneRound);
// self.setState({
// isTimerStart: false,
// isTimeout: true
// });
}
}, 1000);
return oneRound;
} // getPercentage(time) {
// const {freq} = this.props;
// let _freq = freq > 60 ? 60 : freq;
// let result = 0;
// result = time == 0 ? 0 : (_freq - time) / _freq * 100;
// return result;
// }
}, {
key: "getBgDOM",
value: function getBgDOM(timeObj, time, idx) {
var _this$props2 = this.props,
_this$props2$needBg = _this$props2.needBg,
needBg = _this$props2$needBg === void 0 ? true : _this$props2$needBg,
freq = _this$props2.freq,
firstStopColor = _this$props2.firstStopColor,
secondStopColor = _this$props2.secondStopColor;
if (!needBg) return '';
var currTime = timeObj[time];
var currCycle = freq > 60 ? 60 : freq;
var hourCycle = freq / 3600;
switch (time) {
case 'hour':
currCycle = hourCycle;
break;
case 'min':
currCycle = hourCycle > 1 ? 60 : freq / 60;
break;
}
var currPercent = +(currTime / currCycle * 100);
var percent = currPercent == 0 ? 0 : (0, _basicHelper.ToFixed)(100 - currPercent, 0); // if(time == 'sec') console.log(percent);
return _react["default"].createElement(_countdownSvgBg["default"], {
percent: percent,
text: currTime,
firstStopColor: firstStopColor,
secondStopColor: secondStopColor
});
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props3 = this.props,
_this$props3$needBg = _this$props3.needBg,
needBg = _this$props3$needBg === void 0 ? true : _this$props3$needBg,
freq = _this$props3.freq,
_this$props3$needProg = _this$props3.needProgress,
needProgress = _this$props3$needProg === void 0 ? false : _this$props3$needProg;
var countdown = this.state.countdown;
var timeObj = (0, _basicHelper.TimeFormat)(countdown);
var percent = +(countdown / freq * 100);
var progressDOM = needProgress ? _react["default"].createElement("span", {
className: "progress",
style: {
right: percent + '%'
}
}) : '';
this.setJumpElemCount(timeObj);
return _react["default"].createElement("section", {
className: "countdown"
}, Object.keys(timeObj).map(function (time, idx) {
var currTime = timeObj[time];
var countBg = _this2.getBgDOM(timeObj, time, idx);
return _react["default"].createElement("span", {
className: "item " + time,
key: time
}, _react["default"].createElement("span", {
className: "text"
}, currTime), countBg, _react["default"].createElement("span", {
className: "foot"
}, timeTitleMapper[time]));
}), progressDOM);
}
}]);
return Countdown;
}(_react.Component);
exports["default"] = Countdown;
_defineProperty(Countdown, "propTypes", {
start: _propTypes["default"].number.isRequired,
freq: _propTypes["default"].number.isRequired,
needBg: _propTypes["default"].bool,
needProgress: _propTypes["default"].bool,
jumpClass: _propTypes["default"].string,
firstStopColor: _propTypes["default"].string,
secondStopColor: _propTypes["default"].string,
countdownNotifyTimer: _propTypes["default"].any,
onCountdownNotify: _propTypes["default"].func,
onTimeout: _propTypes["default"].func.isRequired
});
_defineProperty(Countdown, "defaultProps", {
firstStopColor: '#fe0362',
secondStopColor: '#7473e3'
});