@txdfe/at
Version:
一个设计体系组件库
209 lines (202 loc) • 9.8 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Animate from '../animate';
import { support, dom } from '../util';
/**
* badge sup component
*/
// util::getDigitArray
var getDigitArray = function getDigitArray(num) {
return num.toString().split('').reverse().map(function (i) {
return parseInt(i, 10);
});
};
var Sup = /*#__PURE__*/function (_Component) {
function Sup(props) {
var _this;
_classCallCheck(this, Sup);
_this = _callSuper(this, Sup, [props]);
// 记录最后一次显示的数字
_this.lastCount = 0;
return _this;
}
_inherits(Sup, _Component);
return _createClass(Sup, [{
key: "componentDidMount",
value: function componentDidMount() {
this.computeStyle(true);
}
}, {
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
if ('count' in nextProps) {
if (nextProps.count !== this.props.count) {
this.lastCount = this.props.count;
}
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this2 = this;
if (prevProps.count !== this.props.count) {
this.computeStyle(false);
// NOTE why called `computeStyle` again after 300ms ?
setTimeout(function () {
_this2.computeStyle(true, true);
}, 300);
}
}
}, {
key: "computeStyle",
value: function computeStyle(removeTransition, revert) {
var _this3 = this;
var _this$props = this.props,
prefix = _this$props.prefix,
count = _this$props.count,
overflowCount = _this$props.overflowCount;
if (count < 0) {
return;
}
var supNode = this.refs.sup;
if (supNode && dom.hasClass(supNode, "".concat(prefix, "badge-count"))) {
var scrollNums = supNode.querySelectorAll(".".concat(prefix, "badge-scroll-number-only"));
if (scrollNums.length) {
var _window$getComputedSt = window.getComputedStyle(supNode),
height = _window$getComputedSt.height;
scrollNums = [].slice.call(scrollNums, 0).reverse();
getDigitArray(count).forEach(function (digit, i) {
var position = _this3.getPositionByDigit(digit, i, revert);
// transform用百分比,防止缩放时的取整
// 不支持的保留精度,防止出现小数情况
var transformTo = -position * parseFloat(height);
removeTransition = removeTransition || typeof getDigitArray(_this3.lastCount)[i] === 'undefined' || _this3.lastCount > overflowCount || _this3.lastCount <= 0;
var scrollStyle = support.animation ? {
transition: _this3.props.noTransition || removeTransition ? 'none' : 'transform .3s cubic-bezier(.645, .045, .355, 1), -webkit-transform .3s cubic-bezier(.645, .045, .355, 1)',
WebkitTransform: "translateY(".concat(-position * 100, "%)"),
transform: "translateY(".concat(-position * 100, "%)"),
height: height,
lineHeight: height
} : {
top: "".concat(transformTo, "px"),
height: height,
lineHeight: height
};
Object.keys(scrollStyle).forEach(function (key) {
scrollNums[i].style[key] = scrollStyle[key];
});
});
}
}
}
}, {
key: "getPositionByDigit",
value: function getPositionByDigit(digit, i, revert) {
if (revert) {
return 10 + digit;
}
var lastDigit = getDigitArray(this.lastCount)[i] || 0;
if (this.props.count > this.lastCount) {
return (digit >= lastDigit ? 10 : 20) + digit;
}
if (digit <= lastDigit) {
return 10 + digit;
}
return digit;
}
}, {
key: "render",
value: function render() {
var _this$props2 = this.props,
prefix = _this$props2.prefix,
count = _this$props2.count,
showZero = _this$props2.showZero,
overflowCount = _this$props2.overflowCount,
dot = _this$props2.dot,
noTransition = _this$props2.noTransition,
style = _this$props2.style,
content = _this$props2.content;
var supClasses = classNames("".concat(prefix, "badge-scroll-number"), _defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "badge-count"), !!count || count === 0 && showZero), "".concat(prefix, "badge-dot"), dot), "".concat(prefix, "badge-custom"), !!content));
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;
}
// const animation = {
// appear: 'zoomIn',
// enter: 'zoomIn',
// leave: 'zoomOut',
// };
// const wrapper = support.animation ? <Animate animation={animation} /> : <span />;
var wrapper = /*#__PURE__*/React.createElement("span", null);
var element = show ? /*#__PURE__*/React.createElement("sup", {
ref: "sup",
className: supClasses,
style: style
}, children) : null;
return /*#__PURE__*/React.cloneElement(wrapper, {}, element);
}
}], [{
key: "renderDigit",
value:
// 单排可滚动的数字列表
function renderDigit(prefix, digit, key) {
var children = [];
for (var i = 0; i < 30; i++) {
children.push(/*#__PURE__*/React.createElement("span", {
key: i
}, i % 10));
}
return /*#__PURE__*/React.createElement("span", {
className: "".concat(prefix, "badge-scroll-number-only"),
key: key
}, children);
}
// 可滚动数字组
}, {
key: "renderNumber",
value: function renderNumber(prefix, count) {
return getDigitArray(count).map(function (digit, i) {
return Sup.renderDigit(prefix, digit, i);
}).reverse();
}
}]);
}(Component);
_defineProperty(Sup, "propTypes", {
prefix: PropTypes.string,
count: PropTypes.number,
showZero: PropTypes.bool,
overflowCount: PropTypes.number,
content: PropTypes.node,
dot: PropTypes.bool,
noTransition: PropTypes.bool,
// 是否显示滚动动画,tabs 中的 badge 默认不显示
style: PropTypes.object
});
_defineProperty(Sup, "defaultProps", {
prefix: 'next-',
count: 0,
showZero: false,
overflowCount: 99,
dot: false,
noTransition: false
});
export { Sup as default };