forked from oblador/react-native-progress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circle.js
219 lines (204 loc) · 5.49 KB
/
Circle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Animated, StyleSheet, Text, View } from 'react-native';
import { Svg } from 'react-native-svg';
import Arc from './Shapes/Arc';
import withAnimation from './withAnimation';
const CIRCLE = Math.PI * 2;
const AnimatedSvg = Animated.createAnimatedComponent(Svg);
const AnimatedArc = Animated.createAnimatedComponent(Arc);
const styles = StyleSheet.create({
container: {
backgroundColor: 'transparent',
overflow: 'hidden',
},
});
export class ProgressCircle extends Component {
static propTypes = {
animated: PropTypes.bool,
borderColor: PropTypes.string,
borderWidth: PropTypes.number,
color: PropTypes.string,
children: PropTypes.node,
direction: PropTypes.oneOf(['clockwise', 'counter-clockwise']),
fill: PropTypes.string,
formatText: PropTypes.func,
indeterminate: PropTypes.bool,
progress: PropTypes.oneOfType([
PropTypes.number,
PropTypes.instanceOf(Animated.Value),
]),
rotation: PropTypes.instanceOf(Animated.Value),
showsText: PropTypes.bool,
size: PropTypes.number,
style: PropTypes.any,
strokeCap: PropTypes.oneOf(['butt', 'square', 'round']),
textStyle: PropTypes.any,
thickness: PropTypes.number,
unfilledColor: PropTypes.string,
endAngle: PropTypes.number,
allowFontScaling: PropTypes.bool,
};
static defaultProps = {
borderWidth: 1,
color: 'rgba(0, 122, 255, 1)',
direction: 'clockwise',
formatText: progress => `${Math.round(progress * 100)}%`,
progress: 0,
showsText: false,
size: 40,
thickness: 3,
endAngle: 0.9,
allowFontScaling: true,
};
constructor(props, context) {
super(props, context);
this.progressValue = 0;
}
componentDidMount() {
if (this.props.animated) {
this.props.progress.addListener(event => {
this.progressValue = event.value;
if (this.props.showsText || this.progressValue === 1) {
this.forceUpdate();
}
});
}
}
render() {
const {
animated,
borderColor,
borderWidth,
color,
children,
direction,
fill,
formatText,
indeterminate,
progress,
rotation,
showsText,
size,
style,
strokeCap,
textStyle,
thickness,
unfilledColor,
endAngle,
allowFontScaling,
...restProps
} = this.props;
const border = borderWidth || (indeterminate ? 1 : 0);
const radius = size / 2 - border;
const offset = {
top: border,
left: border,
};
const textOffset = border + thickness;
const textSize = size - textOffset * 2;
const Surface = rotation ? AnimatedSvg : Svg;
const Shape = animated ? AnimatedArc : Arc;
const progressValue = animated ? this.progressValue : progress;
const angle = animated
? Animated.multiply(progress, CIRCLE)
: progress * CIRCLE;
return (
<View style={[styles.container, style]} {...restProps}>
<Surface
width={size}
height={size}
style={
indeterminate && rotation
? {
transform: [
{
rotate: rotation.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
}),
},
],
}
: undefined
}
>
{unfilledColor && progressValue !== 1 ? (
<Shape
fill={fill}
radius={radius}
offset={offset}
startAngle={angle}
endAngle={CIRCLE}
direction={direction}
stroke={unfilledColor}
strokeWidth={thickness}
/>
) : (
false
)}
{!indeterminate ? (
<Shape
fill={fill}
radius={radius}
offset={offset}
startAngle={0}
endAngle={angle}
direction={direction}
stroke={color}
strokeCap={strokeCap}
strokeWidth={thickness}
/>
) : (
false
)}
{border ? (
<Arc
fill={fill}
radius={size / 2}
startAngle={0}
endAngle={(indeterminate ? endAngle * 2 : 2) * Math.PI}
stroke={borderColor || color}
strokeCap={strokeCap}
strokeWidth={border}
/>
) : (
false
)}
</Surface>
{!indeterminate && showsText ? (
<View
style={{
position: 'absolute',
left: textOffset,
top: textOffset,
width: textSize,
height: textSize,
borderRadius: textSize / 2,
alignItems: 'center',
justifyContent: 'center',
}}
>
<Text
style={[
{
color,
fontSize: textSize / 4.5,
fontWeight: '300',
},
textStyle,
]}
allowFontScaling={allowFontScaling}
>
{formatText(progressValue)}
</Text>
</View>
) : (
false
)}
{children}
</View>
);
}
}
export default withAnimation(ProgressCircle);