2018-08-07 21:07:52 +08:00
|
|
|
import * as PropTypes from 'prop-types';
|
2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-04-05 16:51:26 +08:00
|
|
|
import classNames from 'classnames';
|
2019-01-18 15:19:36 +08:00
|
|
|
import Icon from '../icon';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2018-12-22 21:21:42 +08:00
|
|
|
import { tuple } from '../_util/type';
|
2019-01-18 15:19:36 +08:00
|
|
|
import Line from './Line';
|
|
|
|
import Circle from './Circle';
|
|
|
|
import { validProgress } from './utils';
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2018-12-22 21:21:42 +08:00
|
|
|
const ProgressTypes = tuple('line', 'circle', 'dashboard');
|
|
|
|
export type ProgressType = (typeof ProgressTypes)[number];
|
|
|
|
const ProgressStatuses = tuple('normal', 'exception', 'active', 'success');
|
2018-04-16 10:58:17 +08:00
|
|
|
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;
|
2018-11-14 14:46:07 +08:00
|
|
|
format?: (percent?: number, successPercent?: number) => React.ReactNode;
|
2018-12-22 21:21:42 +08:00
|
|
|
status?: (typeof ProgressStatuses)[number];
|
2016-08-09 15:04:02 +08:00
|
|
|
showInfo?: boolean;
|
|
|
|
strokeWidth?: number;
|
2018-08-01 00:19:06 +08:00
|
|
|
strokeLinecap?: string;
|
2018-06-06 16:18:00 +08:00
|
|
|
strokeColor?: string;
|
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
|
|
|
}
|
|
|
|
|
2017-11-21 15:37:39 +08:00
|
|
|
export default class Progress extends React.Component<ProgressProps, {}> {
|
2016-03-31 17:46:35 +08:00
|
|
|
static defaultProps = {
|
2019-01-18 15:19:36 +08:00
|
|
|
type: 'line',
|
2016-03-31 17:46:35 +08:00
|
|
|
percent: 0,
|
|
|
|
showInfo: true,
|
2016-04-05 16:51:26 +08:00
|
|
|
trailColor: '#f3f3f3',
|
2019-01-18 15:19:36 +08:00
|
|
|
size: 'default',
|
|
|
|
gapDegree: 0,
|
|
|
|
strokeLinecap: 'round',
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
static propTypes = {
|
2018-12-22 21:21:42 +08:00
|
|
|
status: PropTypes.oneOf(ProgressStatuses),
|
|
|
|
type: PropTypes.oneOf(ProgressTypes),
|
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,
|
2018-08-01 00:19:06 +08:00
|
|
|
strokeLinecap: PropTypes.oneOf(['round', 'square']),
|
2018-06-06 16:18:00 +08:00
|
|
|
strokeColor: PropTypes.string,
|
2016-04-05 16:51:26 +08:00
|
|
|
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
|
|
|
|
2019-01-18 15:19:36 +08:00
|
|
|
renderProcessInfo(prefixCls: string, progressStatus: (typeof ProgressStatuses)[number]) {
|
|
|
|
const { showInfo, format, type, percent, successPercent } = this.props;
|
|
|
|
if (!showInfo) return null;
|
|
|
|
|
|
|
|
let text;
|
|
|
|
const textFormatter = format || (percentNumber => `${percentNumber}%`);
|
|
|
|
const iconType = type === 'circle' || type === 'dashboard' ? '' : '-circle';
|
|
|
|
if (format || (progressStatus !== 'exception' && progressStatus !== 'success')) {
|
|
|
|
text = textFormatter(validProgress(percent), validProgress(successPercent));
|
|
|
|
} else if (progressStatus === 'exception') {
|
|
|
|
text = <Icon type={`close${iconType}`} theme={type === 'line' ? 'filled' : 'outlined'} />;
|
|
|
|
} else if (progressStatus === 'success') {
|
|
|
|
text = <Icon type={`check${iconType}`} theme={type === 'line' ? 'filled' : 'outlined'} />;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<span className={`${prefixCls}-text`} title={typeof text === 'string' ? text : undefined}>
|
|
|
|
{text}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderProgress = ({ getPrefixCls }: ConfigConsumerProps) => {
|
2016-12-19 15:19:15 +08:00
|
|
|
const props = this.props;
|
|
|
|
const {
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2018-12-07 20:02:01 +08:00
|
|
|
className,
|
|
|
|
percent = 0,
|
|
|
|
status,
|
|
|
|
format,
|
|
|
|
trailColor,
|
|
|
|
size,
|
|
|
|
successPercent,
|
|
|
|
type,
|
|
|
|
strokeWidth,
|
|
|
|
width,
|
|
|
|
showInfo,
|
|
|
|
gapDegree = 0,
|
|
|
|
gapPosition,
|
|
|
|
strokeColor,
|
|
|
|
strokeLinecap = 'round',
|
2018-06-06 16:18:00 +08:00
|
|
|
...restProps
|
2016-12-19 15:19:15 +08:00
|
|
|
} = props;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('progress', customizePrefixCls);
|
2018-12-07 20:02:01 +08:00
|
|
|
const progressStatus =
|
|
|
|
parseInt(successPercent ? successPercent.toString() : percent.toString(), 10) >= 100 &&
|
|
|
|
!('status' in props)
|
|
|
|
? 'success'
|
|
|
|
: status || 'normal';
|
2016-04-05 16:51:26 +08:00
|
|
|
let progress;
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2019-01-18 15:19:36 +08:00
|
|
|
const progressInfo = this.renderProcessInfo(prefixCls, progressStatus);
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2019-01-18 15:19:36 +08:00
|
|
|
// Render progress shape
|
2016-04-05 16:51:26 +08:00
|
|
|
if (type === 'line') {
|
|
|
|
progress = (
|
2019-01-18 15:19:36 +08:00
|
|
|
<Line {...this.props} prefixCls={prefixCls}>
|
2016-04-05 16:51:26 +08:00
|
|
|
{progressInfo}
|
2019-01-18 15:19:36 +08:00
|
|
|
</Line>
|
2016-04-05 16:51:26 +08:00
|
|
|
);
|
2017-03-17 23:57:34 +08:00
|
|
|
} else if (type === 'circle' || type === 'dashboard') {
|
2016-04-05 16:51:26 +08:00
|
|
|
progress = (
|
2019-01-18 15:19:36 +08:00
|
|
|
<Circle {...this.props} prefixCls={prefixCls} progressStatus={progressStatus}>
|
2016-04-05 16:51:26 +08:00
|
|
|
{progressInfo}
|
2019-01-18 15:19:36 +08:00
|
|
|
</Circle>
|
2016-04-05 16:51:26 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
const classString = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${(type === 'dashboard' && 'circle') || type}`]: true,
|
|
|
|
[`${prefixCls}-status-${progressStatus}`]: true,
|
|
|
|
[`${prefixCls}-show-info`]: showInfo,
|
|
|
|
[`${prefixCls}-${size}`]: size,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2016-04-05 16:51:26 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div {...restProps} className={classString}>
|
|
|
|
{progress}
|
2016-03-31 17:46:35 +08:00
|
|
|
</div>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderProgress}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-03-31 17:46:35 +08:00
|
|
|
}
|