diff --git a/.travis.yml b/.travis.yml index 1a6ff13..aa9e240 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ notifications: - afc163@gmail.com node_js: -- 4.0.0 +- 6.0.0 before_install: - | diff --git a/HISTORY.md b/HISTORY.md index 79844f8..0526bb1 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,9 @@ --- +## 2.1.0 +- Add `gapWidth` `gapPosition` props. + ## 2.0.0 - refactor code diff --git a/README.md b/README.md index c24e063..a2fe81f 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,24 @@ ReactDOM.render(
style object will be added to svg element + + percent + Number + 0 + the percent of the progress + + + gapWidth + Number + 0 + the gap width of half circle + + + gapPosition + String + top + the gap position, value: top, bottom, left, right. + diff --git a/examples/simple.js b/examples/simple.js index 725b808..5e77f13 100644 --- a/examples/simple.js +++ b/examples/simple.js @@ -13,8 +13,9 @@ const Example = React.createClass({ }, changeState() { const colorMap = ['#3FC7FA', '#85D262', '#FE8C6A']; + const value = parseInt(Math.random() * 100, 10); this.setState({ - percent: parseInt(Math.random() * 100, 10), + percent: value, color: colorMap[parseInt(Math.random() * 3, 10)], }); }, @@ -25,6 +26,7 @@ const Example = React.createClass({ const circleContainerStyle = { width: '250px', height: '250px', + display: 'inline-block', }; return (
@@ -41,6 +43,45 @@ const Example = React.createClass({ strokeColor={this.state.color} />
+
+ +
+
+ +
+
+ +
+
+ +

diff --git a/package.json b/package.json index bfabf6d..b962630 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rc-progress", - "version": "2.0.6", + "version": "2.1.0", "description": "progress ui component for react", "keywords": [ "react", diff --git a/src/Circle.js b/src/Circle.js index 790d4d9..c126e19 100644 --- a/src/Circle.js +++ b/src/Circle.js @@ -1,27 +1,60 @@ /* eslint-disable react/prop-types */ -import React from 'react'; +import React, { PropTypes } from 'react'; import mixin from './mixin'; export default React.createClass({ + propTypes: { + gapPosition: PropTypes.oneOf(['top', 'bottom', 'left', 'right']), + }, mixins: [mixin], + render() { const { prefixCls, strokeWidth, trailWidth, strokeColor, trailColor, strokeLinecap, percent, style, className, + gapWidth = 0, gapPosition, ...restProps, } = this.props; const radius = (50 - strokeWidth / 2); - const pathString = `M 50,50 m 0,-${radius} - a ${radius},${radius} 0 1 1 0,${2 * radius} - a ${radius},${radius} 0 1 1 0,-${2 * radius}`; + let beginPositionX = 0; + let beginPositionY = -radius; + let endPositionX = 0; + let endPositionY = -2 * radius; + switch (gapPosition) { + case 'left': + beginPositionX = -radius; + beginPositionY = 0; + endPositionX = 2 * radius; + endPositionY = 0; + break; + case 'right': + beginPositionX = radius; + beginPositionY = 0; + endPositionX = -2 * radius; + endPositionY = 0; + break; + case 'bottom': + beginPositionY = radius; + endPositionY = 2 * radius; + break; + default: + } + const pathString = `M 50,50 m ${beginPositionX},${beginPositionY} + a ${radius},${radius} 0 1 1 ${endPositionX},${-endPositionY} + a ${radius},${radius} 0 1 1 ${-endPositionX},${endPositionY}`; const len = Math.PI * 2 * radius; - const pathStyle = { - strokeDasharray: `${len}px ${len}px`, - strokeDashoffset: `${((100 - percent) / 100 * len)}px`, + const trailPathStyle = { + strokeDasharray: `${len - gapWidth}px ${len}px`, + strokeDashoffset: `-${gapWidth / 2}px`, transition: 'stroke-dashoffset 0.3s ease 0s, stroke 0.3s ease', }; + const strokePathStyle = { + strokeDasharray: `${percent / 100 * (len - gapWidth)}px ${len}px`, + strokeDashoffset: `-${gapWidth / 2}px`, + transition: 'stroke-dashoffset 0.3s ease 0s, stroke 0.3s ease', + }; return ( { this.path = path; }} - style={pathStyle} + ref={(path) => { + this.path = path; + }} + style={strokePathStyle} /> ); diff --git a/tests/index.js b/tests/index.js index 8d5121a..345d33e 100644 --- a/tests/index.js +++ b/tests/index.js @@ -23,3 +23,39 @@ describe('circle progress', () => { ReactDOM.unmountComponentAtNode(div); }); }); + +describe('half circle progress', () => { + it('work', () => { + const div = document.createElement(div); + document.body.appendChild(div); + const circle = ReactDOM.render( + , + div); + expect(circle.props.percent).to.be('30'); + ReactDOM.unmountComponentAtNode(div); + }); +}); + +describe('half circle progress', () => { + it('work', () => { + const div = document.createElement(div); + document.body.appendChild(div); + const circle = ReactDOM.render( + , + div); + expect(circle.props.percent).to.be('30'); + ReactDOM.unmountComponentAtNode(div); + }); +}); + +describe('half circle progress', () => { + it('work', () => { + const div = document.createElement(div); + document.body.appendChild(div); + const circle = ReactDOM.render( + , + div); + expect(circle.props.percent).to.be('30'); + ReactDOM.unmountComponentAtNode(div); + }); +});