ant-design/components/badge/ScrollNumber.tsx

82 lines
2.2 KiB
TypeScript
Raw Normal View History

import classNames from 'classnames';
2022-06-22 14:57:09 +08:00
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import { cloneElement } from '../_util/reactNode';
import SingleNumber from './SingleNumber';
2019-08-05 18:38:10 +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;
children?: React.ReactElement<HTMLElement>;
component?: string;
2016-12-19 15:19:15 +08:00
style?: React.CSSProperties;
2018-07-23 23:16:19 +08:00
title?: string | number | null;
show: boolean;
}
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
}
const ScrollNumber: React.FC<ScrollNumberProps> = ({
prefixCls: customizePrefixCls,
count,
className,
2021-01-28 18:16:05 +08:00
motionClassName,
style,
title,
show,
component = 'sup',
children,
...restProps
}) => {
const { getPrefixCls } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('scroll-number', customizePrefixCls);
// ============================ Render ============================
const newProps = {
...restProps,
'data-show': show,
style,
2021-01-28 18:16:05 +08:00
className: classNames(prefixCls, className, motionClassName),
title: title as string,
};
// Only integer need motion
let numberNodes: React.ReactNode = count;
if (count && Number(count) % 1 === 0) {
const numberList = String(count).split('');
numberNodes = 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}
/>
));
}
// 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' }} />
if (style && style.borderColor) {
newProps.style = {
...style,
boxShadow: `0 0 0 1px ${style.borderColor} inset`,
};
}
if (children) {
2022-11-13 14:33:07 +08:00
return cloneElement(children, (oriProps) => ({
2021-01-28 18:16:05 +08:00
className: classNames(`${prefixCls}-custom-component`, oriProps?.className, motionClassName),
}));
}
2022-11-09 17:36:49 +08:00
return React.createElement(component, newProps, numberNodes);
};
export default ScrollNumber;