ant-design/components/progress/index.jsx

182 lines
5.0 KiB
React
Raw Normal View History

import { Circle as Progresscircle } from 'rc-progress';
import React from 'react';
import assign from 'object-assign';
2016-01-20 20:20:24 +08:00
import warning from 'warning';
2015-11-20 11:05:34 +08:00
import Icon from '../icon';
2015-06-14 12:55:44 +08:00
2015-07-29 22:20:05 +08:00
const prefixCls = 'ant-progress';
const statusColorMap = {
normal: '#2db7f5',
exception: '#ff6600',
success: '#87d068'
2015-07-29 22:20:05 +08:00
};
2015-09-01 16:18:46 +08:00
let Line = React.createClass({
2015-07-30 17:27:41 +08:00
propTypes: {
status: React.PropTypes.oneOf(['normal', 'exception', 'active', 'success']),
showInfo: React.PropTypes.bool,
percent: React.PropTypes.number,
2015-11-19 13:42:10 +08:00
strokeWidth: React.PropTypes.number,
2016-01-20 19:50:45 +08:00
trailColor: React.PropTypes.string,
format: React.PropTypes.oneOfType([
React.PropTypes.node,
React.PropTypes.string,
React.PropTypes.func,
]),
2015-07-30 17:27:41 +08:00
},
2015-07-06 10:58:34 +08:00
getDefaultProps() {
2015-06-14 12:55:44 +08:00
return {
percent: 0,
2015-07-30 17:27:41 +08:00
strokeWidth: 10,
status: 'normal', // exception active
2015-11-19 13:42:10 +08:00
showInfo: true,
2016-01-20 19:50:45 +08:00
trailColor: '#e9e9e9'
2015-06-15 11:48:30 +08:00
};
2015-06-14 12:55:44 +08:00
},
render() {
2015-09-01 16:18:46 +08:00
let props = assign({}, this.props);
2015-07-08 20:53:11 +08:00
2015-09-01 16:18:46 +08:00
if (parseInt(props.percent, 10) === 100) {
2015-07-08 20:53:11 +08:00
props.status = 'success';
2015-06-14 19:50:23 +08:00
}
2015-09-01 16:18:46 +08:00
let progressInfo;
let fullCls = '';
2016-01-20 20:20:24 +08:00
if (props.format) {
warning(typeof props.format === 'function',
'antd.Progress props.format type is function, change format={xxx} to format={() => xxx}');
}
let text = props.format || `${props.percent}%`;
2016-01-20 19:50:45 +08:00
if (typeof props.format === 'string') {
2016-01-20 20:20:24 +08:00
// 向下兼容原来的字符串替换方式
2016-01-20 19:50:45 +08:00
text = props.format.replace('${percent}', props.percent);
} else if (typeof props.format === 'function') {
text = props.format(props.percent);
}
2015-11-19 13:42:10 +08:00
if (props.showInfo === true) {
2015-07-30 17:27:41 +08:00
if (props.status === 'exception') {
progressInfo = (
<span className={`${prefixCls}-line-text`}>
2016-01-20 20:20:24 +08:00
{props.format ? text : <Icon type="exclamation" />}
</span>
2015-07-30 17:27:41 +08:00
);
} else if (props.status === 'success') {
progressInfo = (
<span className={`${prefixCls}-line-text`}>
2016-01-20 20:20:24 +08:00
{props.format ? text : <Icon type="check" />}
2015-07-30 17:27:41 +08:00
</span>
);
} else {
progressInfo = (
<span className={`${prefixCls}-line-text`}>{text}</span>
2015-07-30 17:27:41 +08:00
);
}
2015-11-09 11:13:31 +08:00
} else {
fullCls = ` ${prefixCls}-line-wrap-full`;
2015-06-14 12:55:44 +08:00
}
2015-09-01 16:18:46 +08:00
let percentStyle = {
width: `${props.percent}%`,
2015-07-29 22:20:05 +08:00
height: props.strokeWidth
};
2015-06-14 12:55:44 +08:00
return (
<div className={`${prefixCls}-line-wrap clearfix status-${props.status}${fullCls}`} style={props.style}>
2015-06-14 12:55:44 +08:00
{progressInfo}
<div className={`${prefixCls}-line-outer`}>
<div className={`${prefixCls}-line-inner`}>
<div className={`${prefixCls}-line-bg`} style={percentStyle}></div>
2015-07-29 22:20:05 +08:00
</div>
</div>
2015-06-14 12:55:44 +08:00
</div>
);
}
});
2015-09-01 16:18:46 +08:00
let Circle = React.createClass({
2015-10-26 20:14:41 +08:00
propTypes: {
status: React.PropTypes.oneOf(['normal', 'exception', 'success']),
percent: React.PropTypes.number,
strokeWidth: React.PropTypes.number,
2015-11-19 13:42:10 +08:00
width: React.PropTypes.number,
2016-01-20 19:50:45 +08:00
trailColor: React.PropTypes.string,
format: React.PropTypes.oneOfType([
React.PropTypes.node,
React.PropTypes.string,
React.PropTypes.func,
]),
2015-10-26 20:14:41 +08:00
},
getDefaultProps() {
2015-06-14 12:55:44 +08:00
return {
2015-06-15 17:30:24 +08:00
width: 132,
2015-06-14 12:55:44 +08:00
percent: 0,
2015-06-15 17:30:24 +08:00
strokeWidth: 6,
2015-11-19 13:42:10 +08:00
status: 'normal', // exception
2016-01-20 19:50:45 +08:00
trailColor: '#f9f9f9',
2015-06-15 11:48:30 +08:00
};
2015-06-14 12:55:44 +08:00
},
render() {
2015-09-01 16:18:46 +08:00
let props = assign({}, this.props);
2015-07-08 20:53:11 +08:00
2015-09-01 16:18:46 +08:00
if (parseInt(props.percent, 10) === 100) {
2015-07-08 20:53:11 +08:00
props.status = 'success';
2015-06-14 20:07:21 +08:00
}
2015-09-01 16:18:46 +08:00
let style = {
width: props.width,
height: props.width,
fontSize: props.width * 0.16 + 6
2015-06-15 11:48:30 +08:00
};
2015-09-01 16:18:46 +08:00
let progressInfo;
2016-01-20 20:20:24 +08:00
let text = props.format || `${props.percent}%`;
if (props.format) {
warning(typeof props.format === 'function',
'antd.Progress props.format type is function, change format={xxx} to format={() => xxx}');
}
2016-01-20 19:50:45 +08:00
if (typeof props.format === 'string') {
2016-01-20 20:20:24 +08:00
// 向下兼容原来的字符串替换方式
2016-01-26 17:34:45 +08:00
text = props.format.replace('${percent}', props.percent);
2016-01-20 19:50:45 +08:00
} else if (typeof props.format === 'function') {
text = props.format(props.percent);
}
2016-01-26 17:34:45 +08:00
2015-07-08 20:53:11 +08:00
if (props.status === 'exception') {
2015-06-14 12:55:44 +08:00
progressInfo = (
<span className={`${prefixCls}-circle-text`}>
2016-01-20 20:20:24 +08:00
{props.format ? text : <Icon type="exclamation" />}
</span>
2015-06-15 11:48:30 +08:00
);
2015-07-08 20:53:11 +08:00
} else if (props.status === 'success') {
2015-06-15 16:16:51 +08:00
progressInfo = (
<span className={`${prefixCls}-circle-text`}>
2016-01-20 20:20:24 +08:00
{props.format ? text : <Icon type="check" />}
2015-06-15 16:16:51 +08:00
</span>
);
2015-07-06 10:58:34 +08:00
} else {
2015-06-14 12:55:44 +08:00
progressInfo = (
<span className={`${prefixCls}-circle-text`}>{text}</span>
2015-06-15 11:48:30 +08:00
);
2015-06-14 12:55:44 +08:00
}
return (
<div className={`${prefixCls}-circle-wrap status-${props.status}`} style={props.style}>
<div className={`${prefixCls}-circle-inner`} style={style}>
2015-07-08 20:53:11 +08:00
<Progresscircle percent={props.percent} strokeWidth={props.strokeWidth}
2016-01-20 19:50:45 +08:00
strokeColor={statusColorMap[props.status]} trailColor={props.trailColor} />
2015-06-14 20:07:21 +08:00
{progressInfo}
</div>
2015-06-14 12:55:44 +08:00
</div>
);
}
});
export default {
Line,
Circle,
2015-06-14 12:55:44 +08:00
};