2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2023-05-06 15:49:37 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2023-07-09 16:47:31 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2021-01-29 14:27:36 +08:00
|
|
|
import SingleNumber from './SingleNumber';
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2016-10-24 16:51:53 +08:00
|
|
|
export interface ScrollNumberProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2021-01-28 18:16:05 +08:00
|
|
|
motionClassName?: string;
|
2018-07-23 23:16:19 +08:00
|
|
|
count?: string | number | null;
|
2020-11-18 18:32:00 +08:00
|
|
|
children?: React.ReactElement<HTMLElement>;
|
2023-01-20 21:06:13 +08:00
|
|
|
component?: React.ComponentType<any>;
|
2016-12-19 15:19:15 +08:00
|
|
|
style?: React.CSSProperties;
|
2018-07-23 23:16:19 +08:00
|
|
|
title?: string | number | null;
|
2020-11-18 18:32:00 +08:00
|
|
|
show: boolean;
|
2016-10-24 16:51:53 +08:00
|
|
|
}
|
|
|
|
|
2017-11-22 11:25:05 +08:00
|
|
|
export interface ScrollNumberState {
|
|
|
|
animateStarted?: boolean;
|
2018-07-23 23:16:19 +08:00
|
|
|
count?: string | number | null;
|
2017-11-22 11:25:05 +08:00
|
|
|
}
|
|
|
|
|
2023-07-09 16:47:31 +08:00
|
|
|
const ScrollNumber = React.forwardRef<HTMLElement, ScrollNumberProps>((props, ref) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
count,
|
|
|
|
className,
|
|
|
|
motionClassName,
|
|
|
|
style,
|
|
|
|
title,
|
|
|
|
show,
|
|
|
|
component: Component = 'sup',
|
|
|
|
children,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('scroll-number', customizePrefixCls);
|
2020-04-25 20:53:21 +08:00
|
|
|
|
2023-07-09 16:47:31 +08:00
|
|
|
// ============================ Render ============================
|
|
|
|
const newProps = {
|
|
|
|
...restProps,
|
|
|
|
'data-show': show,
|
|
|
|
style,
|
|
|
|
className: classNames(prefixCls, className, motionClassName),
|
|
|
|
title: title as string,
|
|
|
|
};
|
2020-11-18 18:32:00 +08:00
|
|
|
|
2023-07-09 16:47:31 +08:00
|
|
|
// Only integer need motion
|
|
|
|
let numberNodes: React.ReactNode = count;
|
|
|
|
if (count && Number(count) % 1 === 0) {
|
|
|
|
const numberList = String(count).split('');
|
2021-01-29 14:27:36 +08:00
|
|
|
|
2023-08-14 19:14:46 +08:00
|
|
|
numberNodes = (
|
|
|
|
<bdi>
|
|
|
|
{numberList.map((num, i) => (
|
|
|
|
<SingleNumber
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
count={Number(count)}
|
|
|
|
value={num}
|
|
|
|
// eslint-disable-next-line react/no-array-index-key
|
|
|
|
key={numberList.length - i}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</bdi>
|
|
|
|
);
|
2023-07-09 16:47:31 +08:00
|
|
|
}
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2023-07-09 16:47:31 +08:00
|
|
|
// allow specify the border
|
|
|
|
// mock border-color by box-shadow for compatible with old usage:
|
|
|
|
// <Badge count={4} style={{ backgroundColor: '#fff', color: '#999', borderColor: '#d9d9d9' }} />
|
2024-06-22 21:59:12 +08:00
|
|
|
if (style?.borderColor) {
|
2023-07-09 16:47:31 +08:00
|
|
|
newProps.style = {
|
|
|
|
...style,
|
|
|
|
boxShadow: `0 0 0 1px ${style.borderColor} inset`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (children) {
|
|
|
|
return cloneElement(children, (oriProps) => ({
|
|
|
|
className: classNames(`${prefixCls}-custom-component`, oriProps?.className, motionClassName),
|
|
|
|
}));
|
|
|
|
}
|
2023-01-20 21:06:13 +08:00
|
|
|
|
2023-07-09 16:47:31 +08:00
|
|
|
return (
|
|
|
|
<Component {...newProps} ref={ref}>
|
|
|
|
{numberNodes}
|
|
|
|
</Component>
|
|
|
|
);
|
|
|
|
});
|
2019-03-07 16:03:02 +08:00
|
|
|
|
|
|
|
export default ScrollNumber;
|