Skip to content

Commit 9e6160f

Browse files
committed
refactor progress
1 parent ce8124e commit 9e6160f

File tree

9 files changed

+236
-319
lines changed

9 files changed

+236
-319
lines changed

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
---
44

5+
## 2.6.0
6+
7+
- Refactor to hooks.
8+
59
## 2.5.0
610

711
- Progress.Circle supports gradient color now. #73

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ Progress Bar.
1212

1313
[npm-image]: http://img.shields.io/npm/v/rc-progress.svg?style=flat-square
1414
[npm-url]: http://npmjs.org/package/rc-progress
15-
[travis-image]: https://img.shields.io/travis/react-component/progress.svg?style=flat-square
15+
[travis-image]: https://img.shields.io/travis/react-component/progress/master?style=flat-square
1616
[travis-url]: https://travis-ci.org/react-component/progress
17+
[circleci-image]: https://img.shields.io/circleci/react-component/progress/master?style=flat-square
18+
[circleci-url]: https://circleci.com/gh/react-component/progress
1719
[coveralls-image]: https://img.shields.io/coveralls/react-component/progress.svg?style=flat-square
1820
[coveralls-url]: https://coveralls.io/r/react-component/progress?branch=master
1921
[david-url]: https://david-dm.org/react-component/progress
@@ -43,15 +45,23 @@ http://react-component.github.io/progress/
4345

4446
## Usage
4547

46-
```jsx
48+
```js
4749
import { Line, Circle } from 'rc-progress';
4850

49-
ReactDOM.render(<div>
50-
<Line percent="10" strokeWidth="4" strokeColor="#D3D3D3" />
51-
<Circle percent="10" strokeWidth="4" strokeColor="#D3D3D3" />
52-
</div>, container);
51+
export default () => (
52+
<>
53+
<Line percent="10" strokeWidth="4" strokeColor="#D3D3D3" />
54+
<Circle percent="10" strokeWidth="4" strokeColor="#D3D3D3" />
55+
</>
56+
);
5357
```
5458

59+
## Compatibility
60+
61+
| [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png" alt="IE / Edge" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>IE / Edge | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png" alt="Firefox" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Firefox | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png" alt="Chrome" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Chrome | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png" alt="Safari" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Safari | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/electron/electron_48x48.png" alt="Electron" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Electron |
62+
| --- | --- | --- | --- | --- |
63+
| IE11, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions |
64+
5565
## API
5666

5767
### props

package.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-progress",
3-
"version": "2.5.3",
3+
"version": "2.6.0",
44
"description": "progress ui component for react",
55
"keywords": [
66
"react",
@@ -9,15 +9,14 @@
99
"progress"
1010
],
1111
"homepage": "http://github.com/react-component/progress",
12-
"author": "tsjxyz@gmail.com",
1312
"repository": {
1413
"type": "git",
1514
"url": "git@github.com:react-component/progress.git"
1615
},
1716
"bugs": {
1817
"url": "http://github.com/react-component/progress/issues"
1918
},
20-
"licenses": "MIT",
19+
"license": "MIT",
2120
"main": "lib/index",
2221
"module": "es/index",
2322
"types": "./typings/index.d.ts",
@@ -42,8 +41,8 @@
4241
"build": "rc-tools run build",
4342
"gh-pages": "rc-tools run gh-pages",
4443
"start": "rc-tools run server",
45-
"compile": "rc-tools run compile --babel-runtime",
46-
"pub": "rc-tools run pub --babel-runtime",
44+
"compile": "rc-tools run compile",
45+
"pub": "rc-tools run pub",
4746
"lint": "rc-tools run lint",
4847
"lint:fix": "rc-tools run lint --fix",
4948
"prettier": "rc-tools run prettier",
@@ -69,7 +68,6 @@
6968
]
7069
},
7170
"dependencies": {
72-
"babel-runtime": "6.x",
73-
"prop-types": "^15.5.8"
71+
"classnames": "^2.2.6"
7472
}
7573
}

src/Circle.js

Lines changed: 81 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint react/prop-types: 0 */
2-
import React, { Component } from 'react';
3-
import PropTypes from 'prop-types';
4-
import enhancer from './enhancer';
5-
import { propTypes, defaultProps } from './types';
2+
import React, { useMemo } from 'react';
3+
import classNames from 'classnames';
4+
import { useTransitionDuration, defaultProps } from './common';
65

76
let gradientSeed = 0;
87

@@ -58,142 +57,105 @@ function getPathStyles(offset, percent, strokeColor, strokeWidth, gapDegree = 0,
5857
};
5958
}
6059

61-
class Circle extends Component {
62-
paths = {};
63-
64-
gradientId = 0;
65-
66-
constructor() {
67-
super();
68-
this.gradientId = gradientSeed;
60+
const Circle = ({
61+
prefixCls,
62+
strokeWidth,
63+
trailWidth,
64+
gapDegree,
65+
gapPosition,
66+
trailColor,
67+
strokeLinecap,
68+
style,
69+
className,
70+
strokeColor,
71+
percent,
72+
...restProps
73+
}) => {
74+
const gradientId = useMemo(() => {
6975
gradientSeed += 1;
70-
}
71-
72-
getStokeList() {
73-
const {
74-
prefixCls,
75-
percent,
76-
strokeColor,
77-
strokeWidth,
78-
strokeLinecap,
79-
gapDegree,
80-
gapPosition,
81-
} = this.props;
82-
const percentList = toArray(percent);
83-
const strokeColorList = toArray(strokeColor);
84-
76+
return gradientSeed;
77+
}, []);
78+
const { pathString, pathStyle } = getPathStyles(
79+
0,
80+
100,
81+
trailColor,
82+
strokeWidth,
83+
gapDegree,
84+
gapPosition,
85+
);
86+
const percentList = toArray(percent);
87+
const strokeColorList = toArray(strokeColor);
88+
const gradient = strokeColorList.find(
89+
color => Object.prototype.toString.call(color) === '[object Object]',
90+
);
91+
92+
const [paths] = useTransitionDuration(percentList);
93+
94+
const getStokeList = () => {
8595
let stackPtg = 0;
8696
return percentList.map((ptg, index) => {
8797
const color = strokeColorList[index] || strokeColorList[strokeColorList.length - 1];
8898
const stroke =
8999
Object.prototype.toString.call(color) === '[object Object]'
90-
? `url(#${prefixCls}-gradient-${this.gradientId})`
100+
? `url(#${prefixCls}-gradient-${gradientId})`
91101
: '';
92-
const { pathString, pathStyle } = getPathStyles(
93-
stackPtg,
94-
ptg,
95-
color,
96-
strokeWidth,
97-
gapDegree,
98-
gapPosition,
99-
);
100-
102+
const pathStyles = getPathStyles(stackPtg, ptg, color, strokeWidth, gapDegree, gapPosition);
101103
stackPtg += ptg;
102-
103104
return (
104105
<path
105106
key={index}
106107
className={`${prefixCls}-circle-path`}
107-
d={pathString}
108+
d={pathStyles.pathString}
108109
stroke={stroke}
109110
strokeLinecap={strokeLinecap}
110111
strokeWidth={strokeWidth}
111112
opacity={ptg === 0 ? 0 : 1}
112113
fillOpacity="0"
113-
style={pathStyle}
114-
ref={path => {
115-
this.paths[index] = path;
116-
}}
114+
style={pathStyles.pathStyle}
115+
ref={paths[index]}
117116
/>
118117
);
119118
});
120-
}
121-
122-
render() {
123-
const {
124-
prefixCls,
125-
strokeWidth,
126-
trailWidth,
127-
gapDegree,
128-
gapPosition,
129-
trailColor,
130-
strokeLinecap,
131-
style,
132-
className,
133-
strokeColor,
134-
...restProps
135-
} = this.props;
136-
const { pathString, pathStyle } = getPathStyles(
137-
0,
138-
100,
139-
trailColor,
140-
strokeWidth,
141-
gapDegree,
142-
gapPosition,
143-
);
144-
delete restProps.percent;
145-
const strokeColorList = toArray(strokeColor);
146-
const gradient = strokeColorList.find(
147-
color => Object.prototype.toString.call(color) === '[object Object]',
148-
);
149-
150-
return (
151-
<svg
152-
className={`${prefixCls}-circle ${className}`}
153-
viewBox="0 0 100 100"
154-
style={style}
155-
{...restProps}
156-
>
157-
{gradient && (
158-
<defs>
159-
<linearGradient
160-
id={`${prefixCls}-gradient-${this.gradientId}`}
161-
x1="100%"
162-
y1="0%"
163-
x2="0%"
164-
y2="0%"
165-
>
166-
{Object.keys(gradient)
167-
.sort((a, b) => stripPercentToNumber(a) - stripPercentToNumber(b))
168-
.map((key, index) => (
169-
<stop key={index} offset={key} stopColor={gradient[key]} />
170-
))}
171-
</linearGradient>
172-
</defs>
173-
)}
174-
<path
175-
className={`${prefixCls}-circle-trail`}
176-
d={pathString}
177-
stroke={trailColor}
178-
strokeLinecap={strokeLinecap}
179-
strokeWidth={trailWidth || strokeWidth}
180-
fillOpacity="0"
181-
style={pathStyle}
182-
/>
183-
{this.getStokeList().reverse()}
184-
</svg>
185-
);
186-
}
187-
}
119+
};
188120

189-
Circle.propTypes = {
190-
...propTypes,
191-
gapPosition: PropTypes.oneOf(['top', 'bottom', 'left', 'right']),
121+
return (
122+
<svg
123+
className={classNames(`${prefixCls}-circle`, className)}
124+
viewBox="0 0 100 100"
125+
style={style}
126+
{...restProps}
127+
>
128+
{gradient && (
129+
<defs>
130+
<linearGradient
131+
id={`${prefixCls}-gradient-${gradientId}`}
132+
x1="100%"
133+
y1="0%"
134+
x2="0%"
135+
y2="0%"
136+
>
137+
{Object.keys(gradient)
138+
.sort((a, b) => stripPercentToNumber(a) - stripPercentToNumber(b))
139+
.map((key, index) => (
140+
<stop key={index} offset={key} stopColor={gradient[key]} />
141+
))}
142+
</linearGradient>
143+
</defs>
144+
)}
145+
<path
146+
className={`${prefixCls}-circle-trail`}
147+
d={pathString}
148+
stroke={trailColor}
149+
strokeLinecap={strokeLinecap}
150+
strokeWidth={trailWidth || strokeWidth}
151+
fillOpacity="0"
152+
style={pathStyle}
153+
/>
154+
{getStokeList().reverse()}
155+
</svg>
156+
);
192157
};
193158

194-
Circle.defaultProps = {
195-
...defaultProps,
196-
gapPosition: 'top',
197-
};
159+
Circle.defaultProps = defaultProps;
198160

199-
export default enhancer(Circle);
161+
export default Circle;

0 commit comments

Comments
 (0)