ant-design/components/badge/ScrollNumber.tsx

144 lines
3.6 KiB
TypeScript
Raw Normal View History

2016-06-22 13:18:43 +08:00
import React, {createElement} from 'react';
import {findDOMNode} from 'react-dom';
2016-06-01 12:08:08 +08:00
import isCssAnimationSupported from '../_util/isCssAnimationSupported';
2016-06-22 13:18:43 +08:00
import assign from 'object-assign';
2016-07-08 10:14:45 +08:00
import omit from 'object.omit';
2015-11-23 22:58:11 +08:00
function getNumberArray(num) {
return num ?
num.toString()
.split('')
.reverse()
.map(i => Number(i)) : [];
2015-11-23 22:58:11 +08:00
}
export default class ScrollNumber extends React.Component {
static defaultProps = {
prefixCls: 'ant-scroll-number',
count: null,
component: 'sup',
2016-06-22 13:18:43 +08:00
onAnimated() {
},
height: 18,
2016-07-13 11:14:24 +08:00
};
static propTypes = {
count: React.PropTypes.oneOfType([
React.PropTypes.string,
2016-05-11 09:32:33 +08:00
React.PropTypes.number,
]),
component: React.PropTypes.string,
onAnimated: React.PropTypes.func,
height: React.PropTypes.number,
2016-07-13 11:14:24 +08:00
};
2015-11-23 22:58:11 +08:00
constructor(props) {
super(props);
this.state = {
2015-11-24 15:29:57 +08:00
animateStarted: true,
2016-05-11 09:32:33 +08:00
count: props.count,
2015-11-23 22:58:11 +08:00
};
}
componentDidMount() {
2016-05-26 12:52:07 +08:00
if (!isCssAnimationSupported()) {
findDOMNode(this).className += ' not-support-css-animation';
}
}
2015-11-23 22:58:11 +08:00
getPositionByNum(num, i) {
2015-11-24 15:29:57 +08:00
if (this.state.animateStarted) {
2015-11-23 22:58:11 +08:00
return 10 + num;
}
2015-11-24 15:29:57 +08:00
const currentDigit = getNumberArray(this.state.count)[i];
2015-11-23 22:58:11 +08:00
const lastDigit = getNumberArray(this.lastCount)[i];
// 同方向则在同一侧切换数字
2015-11-24 15:29:57 +08:00
if (this.state.count > this.lastCount) {
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;
2015-11-23 22:58:11 +08:00
}
componentWillReceiveProps(nextProps) {
if ('count' in nextProps) {
2016-02-02 18:23:00 +08:00
if (this.state.count === nextProps.count) {
return;
}
2015-11-24 15:29:57 +08:00
this.lastCount = this.state.count;
// 复原数字初始位置
this.setState({
animateStarted: true,
}, () => {
// 等待数字位置复原完毕
// 开始设置完整的数字
setTimeout(() => {
this.setState({
animateStarted: false,
count: nextProps.count,
}, () => {
this.props.onAnimated();
});
}, 5);
});
2015-11-23 22:58:11 +08:00
}
}
renderNumberList(position) {
2015-11-23 22:58:11 +08:00
const childrenToReturn = [];
for (let i = 0; i < 30; i++) {
const currentClassName = (position === i) ? 'current' : null;
childrenToReturn.push(<p key={i} className={currentClassName}>{i % 10}</p>);
2015-11-23 22:58:11 +08:00
}
return childrenToReturn;
}
renderCurrentNumber(num, i) {
const position = this.getPositionByNum(num, i);
const height = this.props.height;
2015-11-24 15:29:57 +08:00
const removeTransition = this.state.animateStarted ||
(getNumberArray(this.lastCount)[i] === undefined);
2015-11-23 22:58:11 +08:00
return createElement('span', {
className: `${this.props.prefixCls}-only`,
style: {
2015-11-24 15:29:57 +08:00
transition: removeTransition && 'none',
WebkitTransform: `translateY(${-position * height}px)`,
transform: `translateY(${-position * height}px)`,
2016-01-05 14:42:06 +08:00
height,
2015-11-23 22:58:11 +08:00
},
key: i,
}, this.renderNumberList(position));
2015-11-23 22:58:11 +08:00
}
renderNumberElement() {
2015-11-24 15:29:57 +08:00
const state = this.state;
if (!state.count || isNaN(state.count)) {
return state.count;
2015-11-23 22:58:11 +08:00
}
2015-11-24 15:29:57 +08:00
return getNumberArray(state.count)
.map((num, i) => this.renderCurrentNumber(num, i)).reverse();
2015-11-23 22:58:11 +08:00
}
render() {
2016-07-08 10:14:45 +08:00
// fix https://fb.me/react-unknown-prop
2016-07-08 16:34:02 +08:00
const props = assign({}, omit(this.props, [
2016-07-08 10:14:45 +08:00
'count',
'onAnimated',
2016-07-08 16:34:02 +08:00
'component',
'prefixCls',
]), {
2016-05-11 09:32:33 +08:00
className: `${this.props.prefixCls} ${this.props.className}`,
2016-06-22 13:18:43 +08:00
});
return createElement(
2016-07-11 11:50:34 +08:00
this.props.component,
props,
this.renderNumberElement()
);
2015-11-23 22:58:11 +08:00
}
}