UNPKG

ukelli-ui

Version:

ukelli-ui 是基于 React 的 UI 库,提供简约和功能齐全的组件,可高度定制的组件接口,灵活的配置,提供给开发者另一种开发思路,也致力于尝试不同的组件使用和开发方向。

407 lines (333 loc) 15.7 kB
"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 _reactTransitionGroup = require("react-transition-group"); var _grid = require("../../core/grid"); 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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } 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 _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } 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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } 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 AnimatedCard = function AnimatedCard(_ref) { var position = _ref.position, animation = _ref.animation, digit = _ref.digit, style = _ref.style; return _react["default"].createElement("div", { className: "flip-card ".concat(position, " ").concat(animation), style: style }, _react["default"].createElement("span", { className: "count" }, digit)); }; var StaticCard = function StaticCard(_ref2) { var position = _ref2.position, digit = _ref2.digit, style = _ref2.style; return _react["default"].createElement("div", { className: position, style: style }, _react["default"].createElement("span", { className: "count" }, digit)); }; var FlipUnitContainer = /*#__PURE__*/ function (_React$Component) { _inherits(FlipUnitContainer, _React$Component); function FlipUnitContainer() { var _getPrototypeOf2; var _this; _classCallCheck(this, FlipUnitContainer); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(FlipUnitContainer)).call.apply(_getPrototypeOf2, [this].concat(args))); _defineProperty(_assertThisInitialized(_this), "preDigit", 0); _defineProperty(_assertThisInitialized(_this), "shuffle", true); _defineProperty(_assertThisInitialized(_this), "state", { preDigit: 0, shuffle: true }); return _this; } _createClass(FlipUnitContainer, [{ key: "shouldComponentUpdate", value: function shouldComponentUpdate(nextProps) { if (this.props.digit !== nextProps.digit) { this.preDigit = this.props.digit; this.shuffle = !this.shuffle; } return true; } // componentDidUpdate(prevProps) { // if(this.props.digit !== prevProps.digit) { // this.setState(({ preDigit, shuffle }) => { // return { // preDigit: prevProps.digit, // shuffle: !shuffle // }; // }); // } // } }, { key: "render", value: function render() { var _this$props = this.props, digit = _this$props.digit, unit = _this$props.unit, _this$props$width = _this$props.width, width = _this$props$width === void 0 ? 140 : _this$props$width, _this$props$height = _this$props.height, height = _this$props$height === void 0 ? 120 : _this$props$height, flipItemStyle = _this$props.flipItemStyle; // const { shuffle, preDigit } = this.state; var shuffle = this.shuffle, preDigit = this.preDigit; var previousDigit = +preDigit || 0; var currentDigit = +digit; // let previousDigit = +FlipDigitCache[unit] || 0; if (unit !== 'hour') { previousDigit = previousDigit === -1 ? 59 : previousDigit; previousDigit = previousDigit === 60 ? '00' : previousDigit; } else { previousDigit = previousDigit === -1 ? 23 : previousDigit; } if (currentDigit !== '00' && currentDigit < 10) { currentDigit = "0".concat(currentDigit); } if (previousDigit !== '00' && previousDigit < 10) { previousDigit = "0".concat(previousDigit); } var digit1 = shuffle ? previousDigit : currentDigit; var digit2 = !shuffle ? previousDigit : currentDigit; var animation1 = shuffle ? 'fold' : 'unfold'; var animation2 = !shuffle ? 'fold' : 'unfold'; var scaleStyle = { height: height, width: width, fontSize: height / 1.3 }; var styleForAnimate = _objectSpread({}, flipItemStyle, { height: height / 2, width: width }); return _react["default"].createElement("div", { className: "flip-unit-container", style: scaleStyle }, _react["default"].createElement(StaticCard, { position: "before", style: flipItemStyle, digit: currentDigit }), _react["default"].createElement(StaticCard, { position: "now", style: flipItemStyle, digit: previousDigit }), _react["default"].createElement(AnimatedCard, { style: styleForAnimate, position: "first", digit: digit1, animation: animation1 }), _react["default"].createElement(AnimatedCard, { style: styleForAnimate, position: "second", digit: digit2, animation: animation2 })); } }]); return FlipUnitContainer; }(_react["default"].Component); _defineProperty(FlipUnitContainer, "propTypes", { digit: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string]), width: _propTypes["default"].number, height: _propTypes["default"].number, flipItemStyle: _propTypes["default"].shape({}), unit: _propTypes["default"].oneOf(['hour', 'min', 'sec']) }); _defineProperty(FlipUnitContainer, "defaultProps", { width: 140, height: 120 }); var Countdown = /*#__PURE__*/ function (_Component) { _inherits(Countdown, _Component); // isTimerStart = false; function Countdown(props) { var _this2; _classCallCheck(this, Countdown); _this2 = _possibleConstructorReturn(this, _getPrototypeOf(Countdown).call(this, props)); _this2.state = { isTimerStart: false, countdown: 0 }; return _this2; } _createClass(Countdown, [{ key: "shouldComponentUpdate", value: function shouldComponentUpdate(nextProps, nextState) { var isReceiveNewProps = this.props !== nextProps; var isNewCount = this.state.countdown !== nextState.countdown; var isChangeTimerStatus = this.state.isTimerStart !== nextState.isTimerStart; // if(isReceiveNewProps) { // this.clearTimer(); // } return isNewCount || isReceiveNewProps || isChangeTimerStatus; } }, { key: "componentDidMount", value: function componentDidMount() { this.startCountdown(); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState) { if (this.props.start != prevProps.start || !this.state.isTimerStart) { this.clearTimer(); this.startCountdown(); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.clearTimer(); } }, { key: "startCountdown", value: function startCountdown() { var start = this.props.start; if (this.state.isTimerStart || start === 0) return; this.clearTimer(); this.startTimer(); } }, { key: "setJumpElemCount", value: function setJumpElemCount(timeObj) { if (!this.jumpElem) return; this.jumpElem.innerHTML = "".concat(timeObj.hour, ":").concat(timeObj.min, ":").concat(timeObj.sec) || 0; } }, { key: "clearTimer", value: function clearTimer() { // this.isTimerStart = false; this.interval && clearInterval(this.interval); this.interval = null; this.setState({ isTimerStart: false }); } }, { key: "startTimer", value: function startTimer() { var _this3 = this; if (this.state.isTimerStart) return; var _this$props2 = this.props, start = _this$props2.start, countdownNotifyTimer = _this$props2.countdownNotifyTimer, onCountdownNotify = _this$props2.onCountdownNotify, onTimeout = _this$props2.onTimeout; var countdown = start - 1; // this.isTimerStart = true; this.setState({ isTimerStart: true, countdown: countdown }); this.interval = setInterval(function () { countdown--; _this3.setState({ countdown: countdown < 0 ? 0 : countdown }); if (countdown == +countdownNotifyTimer) (0, _basicHelper.Call)(onCountdownNotify, countdown); if (countdown === -1) { onTimeout(); _this3.clearTimer(); // clearInterval(oneRound); // this.setState({ // isTimerStart: false, // isTimeout: true // }); } }, 1000); } }, { key: "render", value: function render() { var _this$props3 = this.props, width = _this$props3.width, height = _this$props3.height, countdownNotifyTimer = _this$props3.countdownNotifyTimer, onCountdownNotify = _this$props3.onCountdownNotify, onTimeout = _this$props3.onTimeout, flipItemStyle = _this$props3.flipItemStyle, className = _this$props3.className, other = _objectWithoutProperties(_this$props3, ["width", "height", "countdownNotifyTimer", "onCountdownNotify", "onTimeout", "flipItemStyle", "className"]); var countdown = this.state.countdown; var timeObj = (0, _basicHelper.TimeFormat)(countdown); this.setJumpElemCount(timeObj); // const hasCountdown = countdown != -1; var container = _react["default"].createElement("section", { className: "flip-clock " + (className ? className : '') }, _react["default"].createElement(_grid.Grid, { container: true }, Object.keys(timeObj).map(function (unit, idx) { var currTime = timeObj[unit]; return _react["default"].createElement(_grid.Grid, _extends({ key: unit }, other), _react["default"].createElement(FlipUnitContainer, { unit: unit, flipItemStyle: flipItemStyle, width: width, height: height, digit: currTime })); }))); return container; // return ( // <TransitionGroup component={null}> // <CSSTransition // key={hasCountdown ? 'has' : 'none'} // classNames="fade" // timeout={200}> // {container} // </CSSTransition> // </TransitionGroup> // ); } }]); return Countdown; }(_react.Component); exports["default"] = Countdown; _defineProperty(Countdown, "propTypes", { /** 倒计时开始时间 */ start: _propTypes["default"].number.isRequired, /** 每一个 flip 的宽度 */ width: _propTypes["default"].number, /** 每一个 flip 的高度 */ height: _propTypes["default"].number, /** 给每一个 flip 的 style */ flipItemStyle: _propTypes["default"].shape({}), /** 需要广播的时间节点 */ countdownNotifyTimer: _propTypes["default"].number, /** 广播的时间节点触发的回调 */ onCountdownNotify: _propTypes["default"].func, /** 时间到 0 的时候触发的回调 */ onTimeout: _propTypes["default"].func.isRequired }); _defineProperty(Countdown, "defaultProps", { width: 140, height: 120 });