2015-11-23 22:58:11 +08:00
|
|
|
import React, { createElement } from 'react';
|
|
|
|
import assign from 'object-assign';
|
|
|
|
|
|
|
|
function getNumberArray(num) {
|
|
|
|
return num ?
|
2015-11-24 15:29:57 +08:00
|
|
|
num.toString().split('').reverse().map(i => Number(i)) : [];
|
2015-11-23 22:58:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class AntScrollNumber extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2015-11-24 15:29:57 +08:00
|
|
|
animateStarted: true,
|
|
|
|
count: props.count
|
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;
|
|
|
|
} else {
|
|
|
|
return 20 + num;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (currentDigit <= lastDigit) {
|
|
|
|
return 10 + num;
|
|
|
|
} else {
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if ('count' in nextProps && nextProps.count) {
|
2015-11-24 15:29:57 +08:00
|
|
|
if (this.lastCount === this.state.count) {
|
|
|
|
return;
|
2015-11-23 22:58:11 +08:00
|
|
|
}
|
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() {
|
|
|
|
const childrenToReturn = [];
|
|
|
|
for (let i = 0; i < 30; i++) {
|
|
|
|
childrenToReturn.push(<p key={i}>{i % 10}</p>);
|
|
|
|
}
|
|
|
|
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',
|
2015-11-23 22:58:11 +08:00
|
|
|
transform: 'translate3d(0, ' + (-position * height) + 'px, 0)',
|
|
|
|
height: height,
|
|
|
|
},
|
|
|
|
key: i,
|
|
|
|
}, this.renderNumberList());
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
const props = assign({}, this.props, {
|
|
|
|
className: `${this.props.prefixCls} ${this.props.className}`
|
|
|
|
});
|
2015-11-25 11:08:26 +08:00
|
|
|
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
|
|
|
|
return createElement(
|
|
|
|
this.props.component,
|
|
|
|
props,
|
|
|
|
this.renderNumberElement()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return createElement(
|
|
|
|
this.props.component,
|
|
|
|
props,
|
|
|
|
props.count
|
|
|
|
);
|
|
|
|
}
|
2015-11-23 22:58:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AntScrollNumber.defaultProps = {
|
|
|
|
prefixCls: 'ant-scroll-number',
|
|
|
|
count: null,
|
|
|
|
component: 'sup',
|
2015-11-24 15:29:57 +08:00
|
|
|
onAnimated: function() {},
|
2015-11-23 22:58:11 +08:00
|
|
|
height: 20
|
|
|
|
};
|
|
|
|
|
|
|
|
AntScrollNumber.propTypes = {
|
|
|
|
count: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.number
|
|
|
|
]),
|
|
|
|
component: React.PropTypes.string,
|
2015-11-24 15:29:57 +08:00
|
|
|
onAnimated: React.PropTypes.func,
|
2015-11-23 22:58:11 +08:00
|
|
|
height: React.PropTypes.number,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AntScrollNumber;
|