@orca-fe/pocket
Version:
UI components by orca-team
141 lines (140 loc) • 9.76 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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _countup = require("countup.js");
var _tools = require("@orca-fe/tools");
var _ahooks = require("ahooks");
var _Flop = _interopRequireDefault(require("./Flop.style"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["value", "duration", "separator", "numStyle", "decimals", "className", "prefix", "suffix", "suffixProps", "convertUnit", "decimalsMaxLength", "start"],
_excluded2 = ["rules"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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 decimalLength(num) {
var max = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
var s = String(num);
var dot = s.indexOf('.');
if (dot < 0) {
return 0;
}
return s.length - 1 - dot;
}
var defaultConvertUnitRules = [{
divisor: 10000,
unit: '万'
}, {
divisor: 10000,
unit: '亿'
}, {
divisor: 10000,
unit: '万亿'
}];
var Flop = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
var _props$value = props.value,
value = _props$value === void 0 ? 0 : _props$value,
_props$duration = props.duration,
duration = _props$duration === void 0 ? 2 : _props$duration,
_props$separator = props.separator,
separator = _props$separator === void 0 ? ',' : _props$separator,
_props$numStyle = props.numStyle,
numStyle = _props$numStyle === void 0 ? {} : _props$numStyle,
decimals = props.decimals,
_props$className = props.className,
className = _props$className === void 0 ? '' : _props$className,
prefix = props.prefix,
suffix = props.suffix,
suffixProps = props.suffixProps,
convertUnit = props.convertUnit,
_props$decimalsMaxLen = props.decimalsMaxLength,
decimalsMaxLength = _props$decimalsMaxLen === void 0 ? 4 : _props$decimalsMaxLen,
_props$start = props.start,
start = _props$start === void 0 ? 0 : _props$start,
otherProps = _objectWithoutProperties(props, _excluded);
var styles = (0, _Flop.default)();
var _value = Number.isNaN(Number(value)) ? 0 : Number(value);
var convertUnitFn = (0, _react.useMemo)(function () {
var _ref = convertUnit || {},
_ref$rules = _ref.rules,
rules = _ref$rules === void 0 ? defaultConvertUnitRules : _ref$rules,
otherConfig = _objectWithoutProperties(_ref, _excluded2);
return (0, _tools.createCovertUnitFn)(rules, otherConfig);
}, [convertUnit]);
var valueUnit = (0, _react.useMemo)(function () {
if (convertUnit === false) {
return (0, _tools.createUnitValue)(_value.toFixed(decimalsMaxLength), '');
}
return convertUnitFn(_value.toFixed(decimalsMaxLength));
}, [_value, convertUnit, convertUnitFn]);
var countupRef = (0, _react.useRef)(null);
var _useState = (0, _react.useState)({
countup: undefined
}),
_useState2 = _slicedToArray(_useState, 1),
_this = _useState2[0];
(0, _react.useEffect)(function () {
var countUpDom = countupRef.current;
if (countUpDom) {
if (!_this.countup) {
_this.countup = new _countup.CountUp(countUpDom, valueUnit.value, {
startVal: start,
duration: duration,
separator: separator,
decimalPlaces: decimals !== null && decimals !== void 0 ? decimals : decimalLength(valueUnit.value)
});
_this.countup.start();
}
}
}, []);
(0, _ahooks.useUpdateEffect)(function () {
var _this$countup;
if ((_this$countup = _this.countup) !== null && _this$countup !== void 0 && _this$countup.options) {
_this.countup.options.duration = duration;
_this.countup.reset();
}
}, [duration]);
(0, _ahooks.useUpdateEffect)(function () {
var _this$countup2;
if ((_this$countup2 = _this.countup) !== null && _this$countup2 !== void 0 && _this$countup2.options) {
_this.countup.options.separator = separator;
}
}, [separator]);
(0, _ahooks.useUpdateEffect)(function () {
if (_this.countup) {
if (_this.countup.options) {
_this.countup.options.decimalPlaces = decimals !== null && decimals !== void 0 ? decimals : decimalLength(valueUnit.value);
}
_this.countup.update(valueUnit.value);
}
}, [valueUnit]);
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", _objectSpread(_objectSpread({
ref: ref,
className: "".concat(styles.root, " ").concat(className)
}, otherProps), {}, {
children: [prefix, /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
ref: countupRef,
className: styles.countup,
style: numStyle
}), typeof suffix === 'function' ? suffix(valueUnit.unit) : /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", _objectSpread(_objectSpread({}, suffixProps), {}, {
children: [valueUnit.unit, suffix]
}))]
}));
});
var _default = exports.default = Flop;