2019-08-15 09:28:15 +08:00
|
|
|
import * as React from 'react';
|
2016-09-10 17:17:55 +08:00
|
|
|
import omit from 'omit.js';
|
2017-07-03 16:57:11 +08:00
|
|
|
import classNames from 'classnames';
|
2019-08-05 18:38:10 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
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()
|
2019-04-02 19:01:12 +08:00
|
|
|
.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
|
|
|
}
|
|
|
|
|
2020-02-05 13:52:26 +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(
|
2020-02-05 13:52:26 +08:00
|
|
|
<p
|
|
|
|
key={i.toString()}
|
|
|
|
className={classNames(className, {
|
|
|
|
current: position === i,
|
|
|
|
})}
|
|
|
|
>
|
2019-08-05 18:38:10 +08:00
|
|
|
{i % 10}
|
|
|
|
</p>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return childrenToReturn;
|
|
|
|
}
|
|
|
|
|
2016-10-24 16:51:53 +08:00
|
|
|
export interface ScrollNumberProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2018-07-23 23:16:19 +08:00
|
|
|
count?: string | number | null;
|
2019-06-16 20:51:47 +08:00
|
|
|
displayComponent?: React.ReactElement<HTMLElement>;
|
2016-10-24 16:51:53 +08:00
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const ScrollNumber: React.FC<ScrollNumberProps> = props => {
|
|
|
|
const [animateStarted, setAnimateStarted] = React.useState(true);
|
|
|
|
const [count, setCount] = React.useState(props.count);
|
|
|
|
const [prevCount, setPrevCount] = React.useState(props.count);
|
|
|
|
const [lastCount, setLastCount] = React.useState(props.count);
|
|
|
|
|
|
|
|
if (prevCount !== props.count) {
|
|
|
|
setAnimateStarted(true);
|
|
|
|
setPrevCount(props.count);
|
2015-11-23 22:58:11 +08:00
|
|
|
}
|
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
setLastCount(count);
|
|
|
|
let timeout: number;
|
2019-08-05 18:38:10 +08:00
|
|
|
if (animateStarted) {
|
2020-03-05 13:29:53 +08:00
|
|
|
// Let browser has time to reset the scroller before actually
|
|
|
|
// performing the transition.
|
2020-04-25 20:53:21 +08:00
|
|
|
timeout = setTimeout(() => {
|
|
|
|
setAnimateStarted(false);
|
|
|
|
setCount(props.count);
|
|
|
|
if (props.onAnimated) {
|
|
|
|
props.onAnimated();
|
|
|
|
}
|
2020-03-05 13:29:53 +08:00
|
|
|
});
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
2020-04-25 20:53:21 +08:00
|
|
|
return () => {
|
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [animateStarted, count, props.count, props.onAnimated]);
|
2020-03-05 13:29:53 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const getPositionByNum = (num: number, i: number) => {
|
2019-04-02 19:01:12 +08:00
|
|
|
const currentCount = Math.abs(Number(count));
|
2020-04-25 20:53:21 +08:00
|
|
|
const lstCount = Math.abs(Number(lastCount));
|
|
|
|
const currentDigit = Math.abs(getNumberArray(count)[i] as number);
|
|
|
|
const lastDigit = Math.abs(getNumberArray(lstCount)[i] as number);
|
2019-04-02 19:01:12 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
if (animateStarted) {
|
2015-11-23 22:58:11 +08:00
|
|
|
return 10 + num;
|
|
|
|
}
|
2019-04-02 19:01:12 +08:00
|
|
|
|
2015-11-23 22:58:11 +08:00
|
|
|
// 同方向则在同一侧切换数字
|
2020-04-25 20:53:21 +08:00
|
|
|
if (currentCount > lstCount) {
|
2015-11-23 22:58:11 +08:00
|
|
|
if (currentDigit >= lastDigit) {
|
|
|
|
return 10 + num;
|
|
|
|
}
|
2016-01-07 14:21:29 +08:00
|
|
|
return 20 + num;
|
|
|
|
}
|
|
|
|
if (currentDigit <= lastDigit) {
|
|
|
|
return 10 + num;
|
2015-11-23 22:58:11 +08:00
|
|
|
}
|
2016-01-07 14:21:29 +08:00
|
|
|
return num;
|
2019-06-19 19:09:08 +08:00
|
|
|
};
|
2019-06-16 17:15:52 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const renderCurrentNumber = (prefixCls: string, num: number | string, i: number) => {
|
2019-04-02 19:01:12 +08:00
|
|
|
if (typeof num === 'number') {
|
2020-04-25 20:53:21 +08:00
|
|
|
const position = getPositionByNum(num, i);
|
|
|
|
const removeTransition = animateStarted || getNumberArray(lastCount)[i] === undefined;
|
2019-08-15 09:47:04 +08:00
|
|
|
return React.createElement(
|
2019-04-02 19:01:12 +08:00
|
|
|
'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
|
|
|
},
|
2020-02-05 13:52:26 +08:00
|
|
|
renderNumberList(position, `${prefixCls}-only-unit`),
|
2019-04-02 19:01:12 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span key="symbol" className={`${prefixCls}-symbol`}>
|
|
|
|
{num}
|
|
|
|
</span>
|
2018-12-07 20:02:01 +08:00
|
|
|
);
|
2020-04-25 20:53:21 +08:00
|
|
|
};
|
2015-11-23 22:58:11 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const renderNumberElement = (prefixCls: string) => {
|
2019-01-09 22:32:06 +08:00
|
|
|
if (count && Number(count) % 1 === 0) {
|
|
|
|
return getNumberArray(count)
|
2020-04-25 20:53:21 +08:00
|
|
|
.map((num, i) => renderCurrentNumber(prefixCls, num, i))
|
2019-01-09 22:32:06 +08:00
|
|
|
.reverse();
|
2015-11-23 22:58:11 +08:00
|
|
|
}
|
2019-01-09 22:32:06 +08:00
|
|
|
return count;
|
2020-04-25 20:53:21 +08:00
|
|
|
};
|
2015-11-23 22:58:11 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const renderScrollNumber = ({ getPrefixCls }: ConfigConsumerProps) => {
|
2018-12-05 19:12:18 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2018-12-07 20:02:01 +08:00
|
|
|
className,
|
|
|
|
style,
|
|
|
|
title,
|
|
|
|
component = 'sup',
|
|
|
|
displayComponent,
|
2020-04-25 20:53:21 +08:00
|
|
|
} = props;
|
2016-07-08 10:14:45 +08:00
|
|
|
// fix https://fb.me/react-unknown-prop
|
2020-04-25 20:53:21 +08:00
|
|
|
const restProps = omit(props, [
|
2016-07-08 10:14:45 +08:00
|
|
|
'count',
|
|
|
|
'onAnimated',
|
2016-07-08 16:34:02 +08:00
|
|
|
'component',
|
|
|
|
'prefixCls',
|
2018-10-16 15:07:24 +08:00
|
|
|
'displayComponent',
|
2017-07-03 16:57:11 +08:00
|
|
|
]);
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('scroll-number', customizePrefixCls);
|
2017-07-03 16:57:11 +08:00
|
|
|
const newProps = {
|
|
|
|
...restProps,
|
2017-08-31 10:55:25 +08:00
|
|
|
className: classNames(prefixCls, className),
|
|
|
|
title: title as string,
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2017-02-03 14:20:42 +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' }} />
|
2017-07-03 16:57:11 +08:00
|
|
|
if (style && style.borderColor) {
|
2019-01-24 12:17:38 +08:00
|
|
|
newProps.style = {
|
|
|
|
...style,
|
|
|
|
boxShadow: `0 0 0 1px ${style.borderColor} inset`,
|
|
|
|
};
|
2017-02-03 14:20:42 +08:00
|
|
|
}
|
2018-10-16 15:07:24 +08:00
|
|
|
if (displayComponent) {
|
|
|
|
return React.cloneElement(displayComponent, {
|
2018-12-18 18:31:12 +08:00
|
|
|
className: classNames(
|
|
|
|
`${prefixCls}-custom-component`,
|
|
|
|
displayComponent.props && displayComponent.props.className,
|
|
|
|
),
|
2018-10-16 15:07:24 +08:00
|
|
|
});
|
|
|
|
}
|
2020-04-25 20:53:21 +08:00
|
|
|
return React.createElement(component as any, newProps, renderNumberElement(prefixCls));
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
return <ConfigConsumer>{renderScrollNumber}</ConfigConsumer>;
|
|
|
|
};
|
2020-03-05 13:29:53 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
ScrollNumber.defaultProps = {
|
|
|
|
count: null,
|
|
|
|
onAnimated() {},
|
|
|
|
};
|
2019-03-07 16:03:02 +08:00
|
|
|
|
|
|
|
export default ScrollNumber;
|