UNPKG

@alifd/next

Version:

A configurable component library for web built on React.

178 lines (177 loc) 7.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var react_1 = tslib_1.__importStar(require("react")); var prop_types_1 = tslib_1.__importDefault(require("prop-types")); var react_lifecycles_compat_1 = require("react-lifecycles-compat"); var classnames_1 = tslib_1.__importDefault(require("classnames")); var animate_1 = tslib_1.__importDefault(require("../animate")); var util_1 = require("../util"); /** * badge sup component */ // util::getDigitArray var getDigitArray = function (num) { return num .toString() .split('') .reverse() .map(function (i) { return parseInt(i, 10); }); }; var Sup = /** @class */ (function (_super) { tslib_1.__extends(Sup, _super); function Sup(props) { var _this = _super.call(this, props) || this; _this.saveRef = function (ref) { _this.supEl = ref; }; // render 时,上一次的渲染数字 和 当前渲染的数字 _this.state = { lastCount: 0, currentCount: props.count, }; return _this; } // 单排可滚动的数字列表 Sup.renderDigit = function (prefix, digit, key) { var children = []; for (var i = 0; i < 30; i++) { children.push(react_1.default.createElement("span", { key: i }, i % 10)); } return (react_1.default.createElement("span", { className: "".concat(prefix, "badge-scroll-number-only"), key: key }, children)); }; // 可滚动数字组 Sup.renderNumber = function (prefix, count) { return getDigitArray(count) .map(function (digit, i) { return Sup.renderDigit(prefix, digit, i); }) .reverse(); }; Sup.getDerivedStateFromProps = function (nextProps, prevState) { if ('count' in nextProps) { return { lastCount: prevState.currentCount, currentCount: nextProps.count, }; } return null; }; Sup.prototype.componentDidMount = function () { this.computeStyle(true); }; Sup.prototype.componentDidUpdate = function (prevProps) { var _this = this; if (prevProps.count !== this.props.count) { this.computeStyle(false); // NOTE why called `computeStyle` again after 300ms ? setTimeout(function () { _this.computeStyle(true, true); }, 300); } }; Sup.prototype.computeStyle = function (removeTransition, revert) { var _this = this; var _a = this.props, prefix = _a.prefix, count = _a.count, overflowCount = _a.overflowCount; var lastCount = this.state.lastCount; if (lastCount === undefined || count === undefined || overflowCount === undefined || Number(count) < 0) { return; } var supNode = this.supEl; if (supNode && util_1.dom.hasClass(supNode, "".concat(prefix, "badge-count"))) { var scrollNums_1 = supNode.querySelectorAll(".".concat(prefix, "badge-scroll-number-only")); if (scrollNums_1.length) { var height_1 = window.getComputedStyle(supNode).height; scrollNums_1 = [].slice.call(scrollNums_1, 0).reverse(); getDigitArray(count).forEach(function (digit, i) { var position = _this.getPositionByDigit(digit, i, revert); var transformTo = -position * parseFloat(height_1); removeTransition = removeTransition || typeof getDigitArray(lastCount)[i] === 'undefined' || lastCount > overflowCount || Number(lastCount) <= 0; var scrollStyle = util_1.support.animation ? { transition: removeTransition ? 'none' : 'transform .3s cubic-bezier(.645, .045, .355, 1), -webkit-transform .3s cubic-bezier(.645, .045, .355, 1)', WebkitTransform: "translateY(".concat(transformTo, "px)"), transform: "translateY(".concat(transformTo, "px)"), height: height_1, lineHeight: height_1, } : { top: "".concat(transformTo, "px"), height: height_1, lineHeight: height_1, }; Object.keys(scrollStyle).forEach(function (key) { scrollNums_1[i].style.setProperty(key, scrollStyle[key]); }); }); } } }; Sup.prototype.getPositionByDigit = function (digit, i, revert) { var lastCount = this.state.lastCount; if (revert) { return 10 + digit; } var lastDigit = getDigitArray(lastCount)[i] || 0; if (this.props.count > lastCount) { return (digit >= lastDigit ? 10 : 20) + digit; } if (digit <= lastDigit) { return 10 + digit; } return digit; }; Sup.prototype.render = function () { var _a; var _b = this.props, prefix = _b.prefix, count = _b.count, showZero = _b.showZero, _c = _b.overflowCount, overflowCount = _c === void 0 ? 99 : _c, dot = _b.dot, style = _b.style, content = _b.content; var supClasses = (0, classnames_1.default)("".concat(prefix, "badge-scroll-number"), (_a = {}, _a["".concat(prefix, "badge-count")] = !!count || (count === 0 && showZero), _a["".concat(prefix, "badge-dot")] = dot, _a["".concat(prefix, "badge-custom")] = !!content, _a)); var children = null; var show = dot || count > 0 || (count === 0 && showZero) || content; if (count > 0 || (count === 0 && showZero)) { var realCount = overflowCount > 0 && count > overflowCount ? "".concat(overflowCount, "+") : count; children = isNaN(realCount) ? realCount : Sup.renderNumber(prefix, count); } else if (content) { children = content; } var animation = { appear: 'zoomIn', enter: 'zoomIn', leave: 'zoomOut', }; var wrapper = util_1.support.animation ? react_1.default.createElement(animate_1.default, { animation: animation }) : react_1.default.createElement("span", null); var element = show ? (react_1.default.createElement("sup", { ref: this.saveRef, className: supClasses, style: style }, children)) : null; return react_1.default.cloneElement(wrapper, {}, element); }; Sup.propTypes = { prefix: prop_types_1.default.string, count: prop_types_1.default.number, showZero: prop_types_1.default.bool, overflowCount: prop_types_1.default.number, content: prop_types_1.default.node, dot: prop_types_1.default.bool, style: prop_types_1.default.object, }; Sup.defaultProps = { prefix: 'next-', count: 0, showZero: false, overflowCount: 99, dot: false, }; return Sup; }(react_1.Component)); exports.default = (0, react_lifecycles_compat_1.polyfill)(Sup);