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-08-05 18:38:10 +08:00
|
|
|
import omit from 'omit.js';
|
2020-03-02 12:09:38 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
|
|
|
import CheckOutlined from '@ant-design/icons/CheckOutlined';
|
|
|
|
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
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';
|
2020-06-28 22:41:59 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2019-01-18 15:19:36 +08:00
|
|
|
import Line from './Line';
|
|
|
|
import Circle from './Circle';
|
2019-11-22 10:46:33 +08:00
|
|
|
import Steps from './Steps';
|
2020-08-20 15:27:16 +08:00
|
|
|
import { validProgress, getSuccessPercent } from './utils';
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2018-12-22 21:21:42 +08:00
|
|
|
const ProgressTypes = tuple('line', 'circle', 'dashboard');
|
2019-11-28 12:34:33 +08:00
|
|
|
export type ProgressType = typeof ProgressTypes[number];
|
2018-12-22 21:21:42 +08:00
|
|
|
const ProgressStatuses = tuple('normal', 'exception', 'active', 'success');
|
2018-04-16 10:58:17 +08:00
|
|
|
export type ProgressSize = 'default' | 'small';
|
2019-03-24 17:05:24 +08:00
|
|
|
export type StringGradients = { [percentage: string]: string };
|
|
|
|
type FromToGradients = { from: string; to: string };
|
|
|
|
export type ProgressGradient = { direction?: string } & (StringGradients | FromToGradients);
|
2020-06-28 22:41:59 +08:00
|
|
|
|
|
|
|
export interface SuccessProps {
|
2020-07-01 23:32:22 +08:00
|
|
|
percent?: number;
|
|
|
|
/** @deprecated Use `percent` instead */
|
2020-06-28 22:41:59 +08:00
|
|
|
progress?: number;
|
|
|
|
strokeColor?: string;
|
|
|
|
}
|
|
|
|
|
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-11-14 14:46:07 +08:00
|
|
|
format?: (percent?: number, successPercent?: number) => React.ReactNode;
|
2019-11-28 12:34:33 +08:00
|
|
|
status?: typeof ProgressStatuses[number];
|
2016-08-09 15:04:02 +08:00
|
|
|
showInfo?: boolean;
|
|
|
|
strokeWidth?: number;
|
2019-06-19 19:41:37 +08:00
|
|
|
strokeLinecap?: 'butt' | 'square' | 'round';
|
2019-03-24 17:05:24 +08:00
|
|
|
strokeColor?: string | ProgressGradient;
|
2016-12-19 15:19:15 +08:00
|
|
|
trailColor?: string;
|
2016-08-09 15:04:02 +08:00
|
|
|
width?: number;
|
2020-06-28 22:41:59 +08:00
|
|
|
success?: SuccessProps;
|
2016-08-09 15:04:02 +08:00
|
|
|
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;
|
2019-11-22 10:46:33 +08:00
|
|
|
steps?: number;
|
2020-06-28 22:41:59 +08:00
|
|
|
/** @deprecated Use `success` instead */
|
|
|
|
successPercent?: number;
|
2016-08-09 15:04:02 +08:00
|
|
|
}
|
|
|
|
|
2019-03-24 17:05:24 +08:00
|
|
|
export default class Progress extends React.Component<ProgressProps> {
|
2016-03-31 17:46:35 +08:00
|
|
|
static defaultProps = {
|
2020-03-28 19:29:02 +08:00
|
|
|
type: 'line' as ProgressProps['type'],
|
2016-03-31 17:46:35 +08:00
|
|
|
percent: 0,
|
|
|
|
showInfo: true,
|
2019-12-03 14:00:52 +08:00
|
|
|
// null for different theme definition
|
|
|
|
trailColor: null,
|
2020-03-28 19:29:02 +08:00
|
|
|
size: 'default' as ProgressProps['size'],
|
2020-03-22 17:59:43 +08:00
|
|
|
gapDegree: undefined,
|
2020-03-28 19:29:02 +08:00
|
|
|
strokeLinecap: 'round' as ProgressProps['strokeLinecap'],
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2019-04-08 22:48:34 +08:00
|
|
|
getPercentNumber() {
|
2020-08-20 15:27:16 +08:00
|
|
|
const { percent = 0 } = this.props;
|
|
|
|
const successPercent = getSuccessPercent(this.props);
|
2019-04-08 22:48:34 +08:00
|
|
|
return parseInt(
|
2019-04-08 21:43:30 +08:00
|
|
|
successPercent !== undefined ? successPercent.toString() : percent.toString(),
|
|
|
|
10,
|
|
|
|
);
|
2019-04-08 22:48:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getProgressStatus() {
|
|
|
|
const { status } = this.props;
|
|
|
|
if (ProgressStatuses.indexOf(status!) < 0 && this.getPercentNumber() >= 100) {
|
2019-04-08 21:43:30 +08:00
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
return status || 'normal';
|
|
|
|
}
|
|
|
|
|
2019-11-28 12:34:33 +08:00
|
|
|
renderProcessInfo(prefixCls: string, progressStatus: typeof ProgressStatuses[number]) {
|
2020-08-20 15:27:16 +08:00
|
|
|
const { showInfo, format, type, percent } = this.props;
|
|
|
|
const successPercent = getSuccessPercent(this.props);
|
2019-01-18 15:19:36 +08:00
|
|
|
if (!showInfo) return null;
|
|
|
|
|
|
|
|
let text;
|
|
|
|
const textFormatter = format || (percentNumber => `${percentNumber}%`);
|
2019-08-13 14:07:17 +08:00
|
|
|
const isLineType = type === 'line';
|
2019-01-18 15:19:36 +08:00
|
|
|
if (format || (progressStatus !== 'exception' && progressStatus !== 'success')) {
|
|
|
|
text = textFormatter(validProgress(percent), validProgress(successPercent));
|
|
|
|
} else if (progressStatus === 'exception') {
|
2019-11-28 12:34:33 +08:00
|
|
|
text = isLineType ? <CloseCircleFilled /> : <CloseOutlined />;
|
2019-01-18 15:19:36 +08:00
|
|
|
} else if (progressStatus === 'success') {
|
2019-11-28 12:34:33 +08:00
|
|
|
text = isLineType ? <CheckCircleFilled /> : <CheckOutlined />;
|
2019-01-18 15:19:36 +08:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<span className={`${prefixCls}-text`} title={typeof text === 'string' ? text : undefined}>
|
|
|
|
{text}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
renderProgress = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { props } = this;
|
2019-11-28 12:34:33 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
size,
|
|
|
|
type,
|
|
|
|
steps,
|
|
|
|
showInfo,
|
2020-05-29 13:43:53 +08:00
|
|
|
strokeColor,
|
2019-11-28 12:34:33 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('progress', customizePrefixCls);
|
2019-04-08 21:43:30 +08:00
|
|
|
const progressStatus = this.getProgressStatus();
|
2019-01-18 15:19:36 +08:00
|
|
|
const progressInfo = this.renderProcessInfo(prefixCls, progressStatus);
|
2020-06-28 22:41:59 +08:00
|
|
|
|
|
|
|
devWarning(
|
2020-07-01 23:32:22 +08:00
|
|
|
!('successPercent' in props),
|
2020-06-28 22:41:59 +08:00
|
|
|
'Progress',
|
2020-08-20 15:27:16 +08:00
|
|
|
'`successPercent` is deprecated. Please use `success.percent` instead.',
|
2020-06-28 22:41:59 +08:00
|
|
|
);
|
|
|
|
|
2019-04-08 21:43:30 +08:00
|
|
|
let progress;
|
2019-01-18 15:19:36 +08:00
|
|
|
// Render progress shape
|
2016-04-05 16:51:26 +08:00
|
|
|
if (type === 'line') {
|
2019-11-22 10:46:33 +08:00
|
|
|
progress = steps ? (
|
2020-05-29 13:43:53 +08:00
|
|
|
<Steps
|
|
|
|
{...this.props}
|
|
|
|
strokeColor={typeof strokeColor === 'string' ? strokeColor : undefined}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
steps={steps}
|
|
|
|
>
|
2016-04-05 16:51:26 +08:00
|
|
|
{progressInfo}
|
2019-11-28 12:34:33 +08:00
|
|
|
</Steps>
|
|
|
|
) : (
|
2020-11-03 16:22:18 +08:00
|
|
|
<Line {...this.props} prefixCls={prefixCls} direction={direction}>
|
2019-11-28 12:34:33 +08:00
|
|
|
{progressInfo}
|
|
|
|
</Line>
|
|
|
|
);
|
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,
|
|
|
|
{
|
2019-11-22 10:46:33 +08:00
|
|
|
[`${prefixCls}-${(type === 'dashboard' && 'circle') || (steps && 'steps') || type}`]: true,
|
2018-12-07 20:02:01 +08:00
|
|
|
[`${prefixCls}-status-${progressStatus}`]: true,
|
|
|
|
[`${prefixCls}-show-info`]: showInfo,
|
|
|
|
[`${prefixCls}-${size}`]: size,
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2018-12-07 20:02:01 +08:00
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2016-04-05 16:51:26 +08:00
|
|
|
|
|
|
|
return (
|
2019-08-05 18:38:10 +08:00
|
|
|
<div
|
|
|
|
{...omit(restProps, [
|
|
|
|
'status',
|
|
|
|
'format',
|
|
|
|
'trailColor',
|
|
|
|
'strokeWidth',
|
|
|
|
'width',
|
|
|
|
'gapDegree',
|
|
|
|
'gapPosition',
|
|
|
|
'strokeColor',
|
|
|
|
'strokeLinecap',
|
|
|
|
'percent',
|
2019-11-22 10:46:33 +08:00
|
|
|
'steps',
|
2020-06-28 22:41:59 +08:00
|
|
|
'success',
|
|
|
|
'successPercent',
|
2019-08-05 18:38:10 +08:00
|
|
|
])}
|
|
|
|
className={classString}
|
|
|
|
>
|
2016-04-05 16:51:26 +08:00
|
|
|
{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
|
|
|
}
|