ant-design/components/badge/ScrollNumber.tsx

185 lines
5.0 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider';
import { cloneElement } from '../_util/reactNode';
2015-11-23 22:58:11 +08:00
2018-07-23 23:16:19 +08:00
function getNumberArray(num: string | number | undefined | null) {
2018-12-07 20:02:01 +08:00
return num
? num
.toString()
.split('')
.reverse()
.map(i => {
const current = Number(i);
return isNaN(current) ? i : current;
})
2018-12-07 20:02:01 +08:00
: [];
2015-11-23 22:58:11 +08:00
}
function renderNumberList(position: number, className: string) {
2019-08-05 18:38:10 +08:00
const childrenToReturn: React.ReactElement<any>[] = [];
for (let i = 0; i < 30; i++) {
childrenToReturn.push(
<p
key={i.toString()}
className={classNames(className, {
current: position === i,
})}
>
2019-08-05 18:38:10 +08:00
{i % 10}
</p>,
);
}
return childrenToReturn;
}
export interface ScrollNumberProps {
prefixCls?: string;
className?: string;
2018-07-23 23:16:19 +08:00
count?: string | number | null;
children?: React.ReactElement<HTMLElement>;
component?: string;
onAnimated?: Function;
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: customizeCount,
className,
style,
title,
show,
component = 'sup',
children,
onAnimated = () => {},
...restProps
}) => {
const [animateStarted, setAnimateStarted] = React.useState(true);
const [count, setCount] = React.useState(customizeCount);
const [prevCount, setPrevCount] = React.useState(customizeCount);
const [lastCount, setLastCount] = React.useState(customizeCount);
const { getPrefixCls } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('scroll-number', customizePrefixCls);
if (prevCount !== customizeCount) {
setAnimateStarted(true);
setPrevCount(customizeCount);
2015-11-23 22:58:11 +08:00
}
React.useEffect(() => {
setLastCount(count);
let timeout: number;
2019-08-05 18:38:10 +08:00
if (animateStarted) {
// Let browser has time to reset the scroller before actually
// performing the transition.
timeout = setTimeout(() => {
setAnimateStarted(false);
setCount(customizeCount);
onAnimated();
});
2019-08-05 18:38:10 +08:00
}
return () => {
if (timeout) {
clearTimeout(timeout);
}
};
}, [animateStarted, customizeCount, onAnimated]);
// =========================== Function ===========================
const getPositionByNum = (num: number, i: number) => {
const currentCount = Math.abs(Number(count));
const lstCount = Math.abs(Number(lastCount));
const currentDigit = Math.abs(getNumberArray(count)[i] as number);
const lastDigit = Math.abs(getNumberArray(lstCount)[i] as number);
if (animateStarted) {
2015-11-23 22:58:11 +08:00
return 10 + num;
}
2015-11-23 22:58:11 +08:00
// 同方向则在同一侧切换数字
if (currentCount > lstCount) {
2015-11-23 22:58:11 +08:00
if (currentDigit >= lastDigit) {
return 10 + num;
}
return 20 + num;
}
if (currentDigit <= lastDigit) {
return 10 + num;
2015-11-23 22:58:11 +08:00
}
return num;
2019-06-19 19:09:08 +08:00
};
// ============================ Render ============================
const newProps = {
...restProps,
'data-show': show,
style,
className: classNames(prefixCls, className),
title: title as string,
};
const renderCurrentNumber = (num: number | string, i: number) => {
if (typeof num === 'number') {
const position = getPositionByNum(num, i);
const removeTransition = animateStarted || getNumberArray(lastCount)[i] === undefined;
2019-08-15 09:47:04 +08:00
return React.createElement(
'span',
{
className: `${prefixCls}-only`,
style: {
transition: removeTransition ? 'none' : undefined,
msTransform: `translateY(${-position * 100}%)`,
WebkitTransform: `translateY(${-position * 100}%)`,
transform: `translateY(${-position * 100}%)`,
},
key: i,
2018-12-07 20:02:01 +08:00
},
renderNumberList(position, `${prefixCls}-only-unit`),
);
}
return (
<span key="symbol" className={`${prefixCls}-symbol`}>
{num}
</span>
2018-12-07 20:02:01 +08:00
);
};
2015-11-23 22:58:11 +08:00
const numberNodeRef = React.useRef<React.ReactNode>(null);
if (show) {
numberNodeRef.current =
count && Number(count) % 1 === 0
? getNumberArray(count)
.map((num, i) => renderCurrentNumber(num, i))
.reverse()
: count;
}
// 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) {
return cloneElement(children, oriProps => ({
className: classNames(`${prefixCls}-custom-component`, oriProps?.className),
}));
}
return React.createElement(component as any, newProps, numberNodeRef.current);
};
export default ScrollNumber;