ScrollNumber.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import classNames from 'classnames';
  12. import { cloneElement } from '../_util/reactNode';
  13. import { ConfigContext } from '../config-provider';
  14. import SingleNumber from './SingleNumber';
  15. const ScrollNumber = /*#__PURE__*/React.forwardRef((props, ref) => {
  16. const {
  17. prefixCls: customizePrefixCls,
  18. count,
  19. className,
  20. motionClassName,
  21. style,
  22. title,
  23. show,
  24. component: Component = 'sup',
  25. children
  26. } = props,
  27. restProps = __rest(props, ["prefixCls", "count", "className", "motionClassName", "style", "title", "show", "component", "children"]);
  28. const {
  29. getPrefixCls
  30. } = React.useContext(ConfigContext);
  31. const prefixCls = getPrefixCls('scroll-number', customizePrefixCls);
  32. // ============================ Render ============================
  33. const newProps = Object.assign(Object.assign({}, restProps), {
  34. 'data-show': show,
  35. style,
  36. className: classNames(prefixCls, className, motionClassName),
  37. title: title
  38. });
  39. // Only integer need motion
  40. let numberNodes = count;
  41. if (count && Number(count) % 1 === 0) {
  42. const numberList = String(count).split('');
  43. numberNodes = /*#__PURE__*/React.createElement("bdi", null, numberList.map((num, i) => (/*#__PURE__*/React.createElement(SingleNumber, {
  44. prefixCls: prefixCls,
  45. count: Number(count),
  46. value: num,
  47. // eslint-disable-next-line react/no-array-index-key
  48. key: numberList.length - i
  49. }))));
  50. }
  51. // allow specify the border
  52. // mock border-color by box-shadow for compatible with old usage:
  53. // <Badge count={4} style={{ backgroundColor: '#fff', color: '#999', borderColor: '#d9d9d9' }} />
  54. if (style === null || style === void 0 ? void 0 : style.borderColor) {
  55. newProps.style = Object.assign(Object.assign({}, style), {
  56. boxShadow: `0 0 0 1px ${style.borderColor} inset`
  57. });
  58. }
  59. if (children) {
  60. return cloneElement(children, oriProps => ({
  61. className: classNames(`${prefixCls}-custom-component`, oriProps === null || oriProps === void 0 ? void 0 : oriProps.className, motionClassName)
  62. }));
  63. }
  64. return /*#__PURE__*/React.createElement(Component, Object.assign({}, newProps, {
  65. ref: ref
  66. }), numberNodes);
  67. });
  68. export default ScrollNumber;