fix progress format, ref #894

This commit is contained in:
afc163 2016-01-20 20:20:24 +08:00
parent 4d228baa57
commit 73596f0ba8
7 changed files with 37 additions and 21 deletions

View File

@ -7,13 +7,13 @@
--- ---
````jsx ````jsx
import { Progress, Icon } from 'antd'; import { Progress } from 'antd';
const ProgressCircle = Progress.Circle; const ProgressCircle = Progress.Circle;
ReactDOM.render( ReactDOM.render(
<div> <div>
<ProgressCircle percent={30} width={80} /> <ProgressCircle percent={30} width={80} />
<ProgressCircle percent={70} width={80} status="exception" format={() => <Icon type="exclamation" />} /> <ProgressCircle percent={70} width={80} status="exception" />
<ProgressCircle percent={100} width={80} /> <ProgressCircle percent={100} width={80} />
</div> </div>
, mountNode); , mountNode);

View File

@ -7,13 +7,13 @@
--- ---
````jsx ````jsx
import { Progress, Icon } from 'antd'; import { Progress } from 'antd';
const ProgressCircle = Progress.Circle; const ProgressCircle = Progress.Circle;
ReactDOM.render( ReactDOM.render(
<div> <div>
<ProgressCircle percent={75} /> <ProgressCircle percent={75} />
<ProgressCircle percent={70} status="exception" format={() => <Icon type="exclamation" />} /> <ProgressCircle percent={70} status="exception" />
<ProgressCircle percent={100} /> <ProgressCircle percent={100} />
</div> </div>
, mountNode); , mountNode);

View File

@ -7,13 +7,12 @@
--- ---
````jsx ````jsx
import { Progress, Icon } from 'antd'; import { Progress } from 'antd';
const ProgressCircle = Progress.Circle; const ProgressCircle = Progress.Circle;
ReactDOM.render( ReactDOM.render(
<div> <div>
<ProgressCircle percent={75} format={percent => percent / 10.0 + '折' } /> <ProgressCircle percent={75} format={percent => percent / 10.0 + '折' } />
<ProgressCircle percent={70} status="exception" format={() => <Icon type="exclamation" />} />
<ProgressCircle percent={100} format={() => '成功'} /> <ProgressCircle percent={100} format={() => '成功'} />
</div> </div>
, mountNode); , mountNode);

View File

@ -7,14 +7,14 @@
--- ---
````jsx ````jsx
import { Progress, Icon } from 'antd'; import { Progress } from 'antd';
const ProgressLine = Progress.Line; const ProgressLine = Progress.Line;
ReactDOM.render( ReactDOM.render(
<div style={{ width: 170 }}> <div style={{ width: 170 }}>
<ProgressLine percent={30} strokeWidth={5} /> <ProgressLine percent={30} strokeWidth={5} />
<ProgressLine percent={50} strokeWidth={5} status="active" /> <ProgressLine percent={50} strokeWidth={5} status="active" />
<ProgressLine percent={70} strokeWidth={5} status="exception" format={() => <Icon type="exclamation" />} /> <ProgressLine percent={70} strokeWidth={5} status="exception" />
<ProgressLine percent={100} strokeWidth={5} /> <ProgressLine percent={100} strokeWidth={5} />
</div> </div>
, mountNode); , mountNode);

View File

@ -7,14 +7,14 @@
--- ---
````jsx ````jsx
import { Progress, Icon } from 'antd'; import { Progress } from 'antd';
const ProgressLine = Progress.Line; const ProgressLine = Progress.Line;
ReactDOM.render( ReactDOM.render(
<div> <div>
<ProgressLine percent={30} /> <ProgressLine percent={30} />
<ProgressLine percent={50} status="active" /> <ProgressLine percent={50} status="active" />
<ProgressLine percent={70} status="exception" format={() => <Icon type="exclamation" />} /> <ProgressLine percent={70} status="exception" />
<ProgressLine percent={100} /> <ProgressLine percent={100} />
<ProgressLine percent={50} showInfo={false} /> <ProgressLine percent={50} showInfo={false} />
</div> </div>

View File

@ -1,6 +1,7 @@
import { Circle as Progresscircle } from 'rc-progress'; import { Circle as Progresscircle } from 'rc-progress';
import React from 'react'; import React from 'react';
import assign from 'object-assign'; import assign from 'object-assign';
import warning from 'warning';
import Icon from '../icon'; import Icon from '../icon';
const prefixCls = 'ant-progress'; const prefixCls = 'ant-progress';
@ -29,7 +30,6 @@ let Line = React.createClass({
percent: 0, percent: 0,
strokeWidth: 10, strokeWidth: 10,
status: 'normal', // exception active status: 'normal', // exception active
format: '${percent}%',
showInfo: true, showInfo: true,
trailColor: '#e9e9e9' trailColor: '#e9e9e9'
}; };
@ -43,8 +43,15 @@ let Line = React.createClass({
let progressInfo; let progressInfo;
let fullCls = ''; let fullCls = '';
let text = props.format;
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}%`;
if (typeof props.format === 'string') { if (typeof props.format === 'string') {
//
text = props.format.replace('${percent}', props.percent); text = props.format.replace('${percent}', props.percent);
} else if (typeof props.format === 'function') { } else if (typeof props.format === 'function') {
text = props.format(props.percent); text = props.format(props.percent);
@ -53,12 +60,14 @@ let Line = React.createClass({
if (props.showInfo === true) { if (props.showInfo === true) {
if (props.status === 'exception') { if (props.status === 'exception') {
progressInfo = ( progressInfo = (
<span className={prefixCls + '-line-text'}>{text}</span> <span className={prefixCls + '-line-text'}>
{props.format ? text : <Icon type="exclamation" />}
</span>
); );
} else if (props.status === 'success') { } else if (props.status === 'success') {
progressInfo = ( progressInfo = (
<span className={prefixCls + '-line-text'}> <span className={prefixCls + '-line-text'}>
<Icon type="check" /> {props.format ? text : <Icon type="check" />}
</span> </span>
); );
} else { } else {
@ -105,7 +114,6 @@ let Circle = React.createClass({
width: 132, width: 132,
percent: 0, percent: 0,
strokeWidth: 6, strokeWidth: 6,
format: '${percent}%',
status: 'normal', // exception status: 'normal', // exception
trailColor: '#f9f9f9', trailColor: '#f9f9f9',
}; };
@ -123,20 +131,29 @@ let Circle = React.createClass({
fontSize: props.width * 0.16 + 6 fontSize: props.width * 0.16 + 6
}; };
let progressInfo; let progressInfo;
let text = props.format; 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}');
}
if (typeof props.format === 'string') { if (typeof props.format === 'string') {
text = props.format.replace('${percent}', props.percent); //
text = '${percent}%'.replace('', props.percent);
} else if (typeof props.format === 'function') { } else if (typeof props.format === 'function') {
text = props.format(props.percent); text = props.format(props.percent);
} }
if (props.status === 'exception') { if (props.status === 'exception') {
progressInfo = ( progressInfo = (
<span className={prefixCls + '-circle-text'}>{text}</span> <span className={prefixCls + '-circle-text'}>
{props.format ? text : <Icon type="exclamation" />}
</span>
); );
} else if (props.status === 'success') { } else if (props.status === 'success') {
progressInfo = ( progressInfo = (
<span className={prefixCls + '-circle-text'}> <span className={prefixCls + '-circle-text'}>
{text ? text : <Icon type="check" />} {props.format ? text : <Icon type="check" />}
</span> </span>
); );
} else { } else {

View File

@ -22,7 +22,7 @@
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
|----------|----------------|----------|---------------| |----------|----------------|----------|---------------|
| percent | 百分比 | number | 0 | | percent | 百分比 | number | 0 |
| format | 数字的模板 | string | "${percent}%" | | format | 内容的模板函数 | function(percent) | 无 |
| status | 状态可选normal、exception、active | string | normal | | status | 状态可选normal、exception、active | string | normal |
| strokeWidth | 进度条线的宽度单位是px | number | 1 | | strokeWidth | 进度条线的宽度单位是px | number | 1 |
| showInfo | 是否显示进度数值和状态图标 | bool | true | | showInfo | 是否显示进度数值和状态图标 | bool | true |
@ -32,7 +32,7 @@
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
|----------|----------------|----------|---------------| |----------|----------------|----------|---------------|
| percent | 百分比 | number | 0 | | percent | 百分比 | number | 0 |
| format | 数字的模板 | string | "${percent}%" | | format | 内容的模板函数 | function(percent) | 无 |
| status | 状态可选normal、exception | string | normal | | status | 状态可选normal、exception | string | normal |
| strokeWidth | 进度条线的宽度,单位是进度条画布宽度的百分比 | number | 1 | | strokeWidth | 进度条线的宽度,单位是进度条画布宽度的百分比 | number | 1 |
| width | 必填进度条画布宽度单位px。这里没有提供height属性设置Line型高度就是strokeWidthCircle型高度等于width | number | null | | width | 必填进度条画布宽度单位px。这里没有提供height属性设置Line型高度就是strokeWidthCircle型高度等于width | number | null |