Skip to content

Commit 2d0bc97

Browse files
author
Markus Lindqvist
authored
Merge pull request #237 from aboveyunhai/master
Add dashed type and transparency for bar fill
2 parents 5eb6b67 + 4599fb3 commit 2d0bc97

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

index.d.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ declare module 'react-native-circular-progress' {
3939
*/
4040
fill?: number;
4141

42+
/**
43+
* Current progress / fillTransparency
44+
*
45+
* @type {boolean}
46+
* @default true
47+
*/
48+
fillTransparency?: boolean;
49+
4250
/**
4351
* Color of the progress line
4452
*
@@ -49,7 +57,7 @@ declare module 'react-native-circular-progress' {
4957

5058
/**
5159
* Change the fill color from tintColor to tintColorSecondary as animation progresses.
52-
*
60+
*
5361
* @type {string}
5462
* @default 'undefined'
5563
*/
@@ -154,6 +162,14 @@ declare module 'react-native-circular-progress' {
154162
center: { x: number; y: number };
155163
}) => React.ReactNode;
156164

165+
/**
166+
* Use dashed type for fill
167+
*
168+
* @type { width: number; gap: number }
169+
* @default '{ width: 0, gap: 0 }'
170+
*/
171+
dashedFill?: { width: number; gap: number };
172+
157173
/**
158174
* Use dashed type for background
159175
*

src/CircularProgress.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,25 @@ export default class CircularProgress extends React.PureComponent {
3434
lineCap,
3535
arcSweepAngle,
3636
fill,
37+
fillTransparency,
3738
children,
3839
childrenContainerStyle,
3940
padding,
4041
renderCap,
4142
dashedBackground,
43+
dashedFill
4244
} = this.props;
4345

4446
const maxWidthCircle = backgroundWidth ? Math.max(width, backgroundWidth) : width;
4547
const sizeWithPadding = size / 2 + padding / 2;
4648
const radius = size / 2 - maxWidthCircle / 2 - padding / 2;
4749

48-
4950
const currentFillAngle = (arcSweepAngle * this.clampFill(fill)) / 100;
50-
const backgroundPath = this.circlePath(
51+
const backgroundPath = this.circlePath(
5152
sizeWithPadding,
5253
sizeWithPadding,
5354
radius,
54-
currentFillAngle,
55+
fillTransparency ? 0 : currentFillAngle,
5556
arcSweepAngle
5657
);
5758
const circlePath = this.circlePath(
@@ -86,13 +87,14 @@ export default class CircularProgress extends React.PureComponent {
8687
...childrenContainerStyle,
8788
}
8889

89-
const dashedBackgroundStyle = dashedBackground.gap > 0
90-
? dashedBackground
91-
: { width:0, gap:0 };
90+
const strokeDasharrayFill = dashedFill.gap > 0 ?
91+
Object.values(dashedFill)
92+
.map(value => parseInt(value))
93+
: null;
9294

93-
const strokeDasharray = dashedBackground.gap > 0 ?
94-
Object.values(dashedBackgroundStyle)
95-
.map(value => parseInt(value))
95+
const strokeDasharrayBackground = dashedBackground.gap > 0 ?
96+
Object.values(dashedBackground)
97+
.map(value => parseInt(value))
9698
: null;
9799

98100
return (
@@ -105,7 +107,7 @@ export default class CircularProgress extends React.PureComponent {
105107
stroke={backgroundColor}
106108
strokeWidth={backgroundWidth || width}
107109
strokeLinecap={lineCap}
108-
strokeDasharray={strokeDasharray}
110+
strokeDasharray={strokeDasharrayBackground}
109111
fill="transparent"
110112
/>
111113
)}
@@ -115,7 +117,7 @@ export default class CircularProgress extends React.PureComponent {
115117
stroke={tintColor}
116118
strokeWidth={width}
117119
strokeLinecap={lineCap}
118-
strokeDasharray={strokeDasharray}
120+
strokeDasharray={strokeDasharrayFill}
119121
fill="transparent"
120122
/>
121123
)}
@@ -132,6 +134,7 @@ CircularProgress.propTypes = {
132134
style: ViewPropTypes.style,
133135
size: PropTypes.number.isRequired,
134136
fill: PropTypes.number.isRequired,
137+
fillTransparency: PropTypes.boolean,
135138
width: PropTypes.number.isRequired,
136139
backgroundWidth: PropTypes.number,
137140
tintColor: PropTypes.string,
@@ -144,6 +147,7 @@ CircularProgress.propTypes = {
144147
padding: PropTypes.number,
145148
renderCap: PropTypes.func,
146149
dashedBackground: PropTypes.object,
150+
dashedFill: PropTypes.object
147151
};
148152

149153
CircularProgress.defaultProps = {
@@ -153,4 +157,6 @@ CircularProgress.defaultProps = {
153157
arcSweepAngle: 360,
154158
padding: 0,
155159
dashedBackground: { width: 0, gap: 0 },
160+
dashedFill: { width: 0, gap: 0 },
161+
fillTransparency: true
156162
};

0 commit comments

Comments
 (0)