UNPKG

@orca-fe/pocket

Version:

UI components by orca-team

131 lines 8.12 kB
var _excluded = ["value", "duration", "separator", "numStyle", "decimals", "className", "prefix", "suffix", "suffixProps", "convertUnit", "decimalsMaxLength", "start"], _excluded2 = ["rules"]; 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; } import React, { useEffect, useMemo, useRef, useState } from 'react'; import { CountUp } from 'countup.js'; import { createCovertUnitFn, createUnitValue } from '@orca-fe/tools'; import { useUpdateEffect } from 'ahooks'; import useStyles from "./Flop.style"; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; function decimalLength(num, max = 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.forwardRef((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 = useStyles(); var _value = Number.isNaN(Number(value)) ? 0 : Number(value); var convertUnitFn = useMemo(() => { var _ref = convertUnit || {}, _ref$rules = _ref.rules, rules = _ref$rules === void 0 ? defaultConvertUnitRules : _ref$rules, otherConfig = _objectWithoutProperties(_ref, _excluded2); return createCovertUnitFn(rules, otherConfig); }, [convertUnit]); var valueUnit = useMemo(() => { if (convertUnit === false) { return createUnitValue(_value.toFixed(decimalsMaxLength), ''); } return convertUnitFn(_value.toFixed(decimalsMaxLength)); }, [_value, convertUnit, convertUnitFn]); var countupRef = useRef(null); var _useState = useState({ countup: undefined }), _useState2 = _slicedToArray(_useState, 1), _this = _useState2[0]; useEffect(() => { var countUpDom = countupRef.current; if (countUpDom) { if (!_this.countup) { _this.countup = new CountUp(countUpDom, valueUnit.value, { startVal: start, duration, separator, decimalPlaces: decimals !== null && decimals !== void 0 ? decimals : decimalLength(valueUnit.value) }); _this.countup.start(); } } }, []); useUpdateEffect(() => { 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]); useUpdateEffect(() => { var _this$countup2; if ((_this$countup2 = _this.countup) !== null && _this$countup2 !== void 0 && _this$countup2.options) { _this.countup.options.separator = separator; } }, [separator]); useUpdateEffect(() => { 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__*/_jsxs("span", _objectSpread(_objectSpread({ ref: ref, className: `${styles.root} ${className}` }, otherProps), {}, { children: [prefix, /*#__PURE__*/_jsx("span", { ref: countupRef, className: styles.countup, style: numStyle }), typeof suffix === 'function' ? suffix(valueUnit.unit) : /*#__PURE__*/_jsxs("span", _objectSpread(_objectSpread({}, suffixProps), {}, { children: [valueUnit.unit, suffix] }))] })); }); export default Flop;