ant-design/components/badge/AntNumber.jsx

131 lines
3.3 KiB
React
Raw Normal View History

2015-11-20 21:44:05 +08:00
import React, { createElement } from 'react';
import { findDOMNode } from 'react-dom';
import { toArrayChildren, getPartNumber, getTranslateY } from './utils';
2015-11-19 16:58:56 +08:00
import assign from 'object-assign';
class AntNumber extends React.Component {
constructor(props) {
super(props);
const children = toArrayChildren(this.props.children);
2015-11-20 14:41:11 +08:00
this.endSetState = false;
this.count = this.props.count;
this.data = getPartNumber(this.count);
this.timeout = null;
2015-11-19 16:58:56 +08:00
this.state = {
children,
};
}
2015-11-20 21:44:05 +08:00
getNumberOnly(index, style) {
2015-11-19 16:58:56 +08:00
const childrenToReturn = [];
2015-11-20 14:41:11 +08:00
for (let i = 0; i < 30; i++) {
let count = i >= 10 ? i % 10 : i;
2015-11-20 21:44:05 +08:00
childrenToReturn.push(<p key={i}>{count}</p>);
2015-11-19 16:58:56 +08:00
}
2015-11-20 21:44:05 +08:00
return createElement('span', {
className: `${this.props.prefixCls}-only`,
style: style,
key: index,
}, childrenToReturn);
2015-11-19 16:58:56 +08:00
}
2015-11-20 14:41:11 +08:00
setEndState(style) {
this.endSetState = true;
style.transition = 'none';
}
2015-11-19 16:58:56 +08:00
getNumberElement(props) {
const count = props.count;
if (!count || count === '') {
return null;
}
2015-11-20 14:41:11 +08:00
const length = count.toString().length;
const data = getPartNumber(count);
2015-11-20 16:00:59 +08:00
const height = findDOMNode(this).offsetHeight;
2015-11-19 16:58:56 +08:00
let childrenWap = [];
2015-11-19 17:02:28 +08:00
let i = 0;
2015-11-19 16:58:56 +08:00
while (i < length) {
2015-11-20 16:00:59 +08:00
const oneData = Number(count.toString()[i]);
2015-11-20 14:41:11 +08:00
const style = {};
let translateY = -(oneData + 10) * height;
//判断状态
2015-11-20 16:00:59 +08:00
translateY = getTranslateY(count, this.count, data, this.data, i, height, length - 1) || translateY;
2015-11-20 14:41:11 +08:00
if (count !== this.count) {
this.setEndState(style);
}
2015-11-20 16:00:59 +08:00
style.transform = `translateY(${translateY}px)`;
const children = this.getNumberOnly(i, style);
childrenWap.push(children);
i++;
}
2015-11-20 14:41:11 +08:00
this.data = data;
this.count = count;
2015-11-19 16:58:56 +08:00
return childrenWap;
}
updateChildren(props) {
if (typeof props.count === 'string') {
2015-11-20 14:41:11 +08:00
this.data = getPartNumber(this.props.max);
this.count = this.props.max;
2015-11-19 16:58:56 +08:00
return this.setState({
2015-11-20 14:41:11 +08:00
children: [props.count],
2015-11-19 16:58:56 +08:00
});
}
2015-11-20 14:41:11 +08:00
const newChildren = this.getNumberElement(props);
2015-11-19 16:58:56 +08:00
if (newChildren && newChildren.length) {
this.setState({
2015-11-20 14:41:11 +08:00
children: newChildren,
}, ()=> {
if (this.endSetState) {
this.updateChildren(props);
this.endSetState = false;
}
2015-11-19 16:58:56 +08:00
});
}
}
2015-11-20 14:41:11 +08:00
animEnd() {
clearTimeout(this.timeout);
this.timeout = setTimeout(()=> {
if (typeof this.props.callback === 'function') {
this.props.callback();
}
}, 300);
}
2015-11-19 16:58:56 +08:00
componentDidMount() {
this.updateChildren(this.props);
}
componentWillReceiveProps(nextProps) {
this.updateChildren(nextProps);
2015-11-20 14:41:11 +08:00
this.animEnd();
2015-11-19 16:58:56 +08:00
}
render() {
const childrenToRender = this.state.children;
2015-11-20 16:00:59 +08:00
const props = assign({}, this.props, {className: `${this.props.prefixCls} ${this.props.className}`});
2015-11-19 16:58:56 +08:00
return createElement(this.props.component, props, childrenToRender);
}
}
2015-11-20 21:44:05 +08:00
2015-11-19 16:58:56 +08:00
AntNumber.defaultProps = {
prefixCls: 'ant-number',
count: null,
2015-11-20 14:41:11 +08:00
max: 99,
component: 'sup',
callback: null,
2015-11-19 16:58:56 +08:00
};
AntNumber.propTypes = {
count: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number
]),
2015-11-20 14:41:11 +08:00
component: React.PropTypes.string,
callback: React.PropTypes.func,
max: React.PropTypes.number,
2015-11-19 16:58:56 +08:00
};
export default AntNumber;