2017-05-09 13:40:33 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-03-31 17:46:35 +08:00
|
|
|
import Icon from '../icon';
|
2016-04-05 16:51:26 +08:00
|
|
|
import { Circle } from 'rc-progress';
|
|
|
|
import classNames from 'classnames';
|
2017-11-21 15:37:39 +08:00
|
|
|
|
2016-04-05 16:51:26 +08:00
|
|
|
const statusColorMap = {
|
2016-11-13 18:57:45 +08:00
|
|
|
normal: '#108ee9',
|
2016-04-05 16:51:26 +08:00
|
|
|
exception: '#ff5500',
|
2016-05-11 09:32:33 +08:00
|
|
|
success: '#87d068',
|
2016-04-05 16:51:26 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2018-04-16 10:58:17 +08:00
|
|
|
export type ProgressType = 'line' | 'circle' | 'dashboard';
|
|
|
|
export type ProgressSize = 'default' | 'small';
|
|
|
|
|
2016-08-09 15:04:02 +08:00
|
|
|
export interface ProgressProps {
|
2016-12-19 15:19:15 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2018-04-16 10:58:17 +08:00
|
|
|
type?: ProgressType;
|
2016-08-09 15:04:02 +08:00
|
|
|
percent?: number;
|
2018-01-19 15:22:37 +08:00
|
|
|
successPercent?: number;
|
2016-08-09 15:04:02 +08:00
|
|
|
format?: (percent: number) => string;
|
|
|
|
status?: 'success' | 'active' | 'exception';
|
|
|
|
showInfo?: boolean;
|
|
|
|
strokeWidth?: number;
|
2016-12-19 15:19:15 +08:00
|
|
|
trailColor?: string;
|
2016-08-09 15:04:02 +08:00
|
|
|
width?: number;
|
|
|
|
style?: React.CSSProperties;
|
2017-03-17 23:57:34 +08:00
|
|
|
gapDegree?: number;
|
|
|
|
gapPosition?: 'top' | 'bottom' | 'left' | 'right';
|
2018-04-16 10:58:17 +08:00
|
|
|
size?: ProgressSize;
|
2016-08-09 15:04:02 +08:00
|
|
|
}
|
|
|
|
|
2018-04-18 10:45:07 +08:00
|
|
|
const validProgress = (progress: number | undefined) => {
|
|
|
|
if (!progress || progress < 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (progress > 100) {
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
return progress;
|
|
|
|
};
|
|
|
|
|
2017-11-21 15:37:39 +08:00
|
|
|
export default class Progress extends React.Component<ProgressProps, {}> {
|
2016-08-09 15:04:02 +08:00
|
|
|
static Line: any;
|
|
|
|
static Circle: any;
|
|
|
|
|
2016-03-31 17:46:35 +08:00
|
|
|
static defaultProps = {
|
2018-04-16 10:58:17 +08:00
|
|
|
type: 'line' as ProgressType,
|
2016-03-31 17:46:35 +08:00
|
|
|
percent: 0,
|
|
|
|
showInfo: true,
|
2016-04-05 16:51:26 +08:00
|
|
|
trailColor: '#f3f3f3',
|
|
|
|
prefixCls: 'ant-progress',
|
2018-04-16 10:58:17 +08:00
|
|
|
size: 'default' as ProgressSize,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
static propTypes = {
|
2016-04-05 16:51:26 +08:00
|
|
|
status: PropTypes.oneOf(['normal', 'exception', 'active', 'success']),
|
2017-03-17 23:57:34 +08:00
|
|
|
type: PropTypes.oneOf(['line', 'circle', 'dashboard']),
|
2016-04-05 16:51:26 +08:00
|
|
|
showInfo: PropTypes.bool,
|
|
|
|
percent: PropTypes.number,
|
2016-04-05 16:55:52 +08:00
|
|
|
width: PropTypes.number,
|
2016-04-05 16:51:26 +08:00
|
|
|
strokeWidth: PropTypes.number,
|
|
|
|
trailColor: PropTypes.string,
|
|
|
|
format: PropTypes.func,
|
2017-03-17 23:57:34 +08:00
|
|
|
gapDegree: PropTypes.number,
|
2017-09-27 22:32:49 +08:00
|
|
|
default: PropTypes.oneOf(['default', 'small']),
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
render() {
|
2016-12-19 15:19:15 +08:00
|
|
|
const props = this.props;
|
|
|
|
const {
|
2018-01-19 15:22:37 +08:00
|
|
|
prefixCls, className, percent = 0, status, format, trailColor, size, successPercent,
|
2017-03-17 23:57:34 +08:00
|
|
|
type, strokeWidth, width, showInfo, gapDegree = 0, gapPosition, ...restProps,
|
2016-12-19 15:19:15 +08:00
|
|
|
} = props;
|
2018-03-11 00:11:52 +08:00
|
|
|
const progressStatus = parseInt((successPercent ? successPercent.toString() : percent.toString()), 10) >= 100 &&
|
|
|
|
!('status' in props) ? 'success' : (status || 'normal');
|
2016-03-31 17:46:35 +08:00
|
|
|
let progressInfo;
|
2016-04-05 16:51:26 +08:00
|
|
|
let progress;
|
|
|
|
const textFormatter = format || (percentNumber => `${percentNumber}%`);
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2016-04-05 16:51:26 +08:00
|
|
|
if (showInfo) {
|
|
|
|
let text;
|
2017-03-17 23:57:34 +08:00
|
|
|
const iconType = (type === 'circle' || type === 'dashboard') ? '' : '-circle';
|
2016-04-05 16:51:26 +08:00
|
|
|
if (progressStatus === 'exception') {
|
2018-04-18 11:10:32 +08:00
|
|
|
text = format ? textFormatter(validProgress(percent)) : <Icon type={`cross${iconType}`} />;
|
2016-04-05 16:51:26 +08:00
|
|
|
} else if (progressStatus === 'success') {
|
2018-04-18 11:10:32 +08:00
|
|
|
text = format ? textFormatter(validProgress(percent)) : <Icon type={`check${iconType}`} />;
|
2016-03-31 17:46:35 +08:00
|
|
|
} else {
|
2018-04-18 11:10:32 +08:00
|
|
|
text = textFormatter(validProgress(percent));
|
2016-03-31 17:46:35 +08:00
|
|
|
}
|
2016-04-05 16:51:26 +08:00
|
|
|
progressInfo = <span className={`${prefixCls}-text`}>{text}</span>;
|
2016-03-31 17:46:35 +08:00
|
|
|
}
|
|
|
|
|
2016-04-05 16:51:26 +08:00
|
|
|
if (type === 'line') {
|
|
|
|
const percentStyle = {
|
2018-04-18 10:45:07 +08:00
|
|
|
width: `${validProgress(percent)}%`,
|
2017-10-11 12:01:27 +08:00
|
|
|
height: strokeWidth || (size === 'small' ? 6 : 8),
|
2016-04-05 16:51:26 +08:00
|
|
|
};
|
2018-01-19 15:22:37 +08:00
|
|
|
const successPercentStyle = {
|
2018-04-18 10:45:07 +08:00
|
|
|
width: `${validProgress(successPercent)}%`,
|
2018-01-19 15:22:37 +08:00
|
|
|
height: strokeWidth || (size === 'small' ? 6 : 8),
|
|
|
|
};
|
|
|
|
const successSegment = successPercent !== undefined
|
|
|
|
? <div className={`${prefixCls}-success-bg`} style={successPercentStyle} />
|
|
|
|
: null;
|
2016-04-05 16:51:26 +08:00
|
|
|
progress = (
|
|
|
|
<div>
|
|
|
|
<div className={`${prefixCls}-outer`}>
|
|
|
|
<div className={`${prefixCls}-inner`}>
|
2016-11-24 14:09:43 +08:00
|
|
|
<div className={`${prefixCls}-bg`} style={percentStyle} />
|
2018-01-19 15:22:37 +08:00
|
|
|
{successSegment}
|
2016-04-05 16:51:26 +08:00
|
|
|
</div>
|
2016-03-31 17:46:35 +08:00
|
|
|
</div>
|
2016-04-05 16:51:26 +08:00
|
|
|
{progressInfo}
|
2016-03-31 17:46:35 +08:00
|
|
|
</div>
|
2016-04-05 16:51:26 +08:00
|
|
|
);
|
2017-03-17 23:57:34 +08:00
|
|
|
} else if (type === 'circle' || type === 'dashboard') {
|
2017-09-27 22:32:49 +08:00
|
|
|
const circleSize = width || 120;
|
2016-04-05 16:51:26 +08:00
|
|
|
const circleStyle = {
|
|
|
|
width: circleSize,
|
|
|
|
height: circleSize,
|
2017-09-27 22:32:49 +08:00
|
|
|
fontSize: circleSize * 0.15 + 6,
|
2016-04-05 16:51:26 +08:00
|
|
|
};
|
2016-09-30 15:21:03 +08:00
|
|
|
const circleWidth = strokeWidth || 6;
|
2017-03-17 23:57:34 +08:00
|
|
|
const gapPos = gapPosition || type === 'dashboard' && 'bottom' || 'top';
|
|
|
|
const gapDeg = gapDegree || type === 'dashboard' && 75;
|
2016-04-05 16:51:26 +08:00
|
|
|
progress = (
|
|
|
|
<div className={`${prefixCls}-inner`} style={circleStyle}>
|
2016-11-23 17:53:10 +08:00
|
|
|
<Circle
|
2018-04-18 10:45:07 +08:00
|
|
|
percent={validProgress(percent)}
|
2016-11-23 17:53:10 +08:00
|
|
|
strokeWidth={circleWidth}
|
|
|
|
trailWidth={circleWidth}
|
2017-11-21 15:37:39 +08:00
|
|
|
strokeColor={(statusColorMap as any)[progressStatus]}
|
2016-11-23 17:53:10 +08:00
|
|
|
trailColor={trailColor}
|
2017-02-22 19:33:15 +08:00
|
|
|
prefixCls={prefixCls}
|
2017-03-17 23:57:34 +08:00
|
|
|
gapDegree={gapDeg}
|
|
|
|
gapPosition={gapPos}
|
2016-06-06 13:54:10 +08:00
|
|
|
/>
|
2016-04-05 16:51:26 +08:00
|
|
|
{progressInfo}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-30 10:20:23 +08:00
|
|
|
const classString = classNames(prefixCls, {
|
2017-03-17 23:57:34 +08:00
|
|
|
[`${prefixCls}-${type === 'dashboard' && 'circle' || type}`]: true,
|
2016-04-05 16:51:26 +08:00
|
|
|
[`${prefixCls}-status-${progressStatus}`]: true,
|
|
|
|
[`${prefixCls}-show-info`]: showInfo,
|
2017-09-27 22:32:49 +08:00
|
|
|
[`${prefixCls}-${size}`]: size,
|
2016-11-30 10:20:23 +08:00
|
|
|
}, className);
|
2016-04-05 16:51:26 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div {...restProps} className={classString}>
|
|
|
|
{progress}
|
2016-03-31 17:46:35 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|