forked from oblador/react-native-progress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circle.js
194 lines (178 loc) · 4.75 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
import React, {
Component,
PropTypes,
} from 'react';
import {
Animated,
ART,
StyleSheet,
Text,
View,
} from 'react-native';
import Arc from './Shapes/Arc';
import withAnimation from './withAnimation';
const CIRCLE = Math.PI * 2;
const AnimatedSurface = Animated.createAnimatedComponent(ART.Surface);
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: React.PropTypes.node,
direction: PropTypes.oneOf(['clockwise', 'counter-clockwise']),
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: View.propTypes.style,
textStyle: Text.propTypes.style,
thickness: PropTypes.number,
unfilledColor: PropTypes.string,
};
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,
};
constructor(props, context) {
super(props, context);
this.progressValue = 0;
}
componentWillMount() {
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,
formatText,
indeterminate,
progress,
rotation,
showsText,
size,
style,
textStyle,
thickness,
unfilledColor,
...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 ? AnimatedSurface : ART.Surface;
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={{
transform: [{
rotate: indeterminate && rotation
? rotation.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
})
: '0deg',
}],
}}
>
{unfilledColor && progressValue !== 1 ? (
<Shape
radius={radius}
offset={offset}
startAngle={angle}
endAngle={CIRCLE}
direction={direction}
stroke={unfilledColor}
strokeWidth={thickness}
/>
) : false}
{!indeterminate ? (
<Shape
radius={radius}
offset={offset}
startAngle={0}
endAngle={angle}
direction={direction}
stroke={color}
strokeWidth={thickness}
/>
) : false}
{border ? (
<Arc
radius={size / 2}
startAngle={0}
endAngle={(indeterminate ? 1.8 : 2) * Math.PI}
stroke={borderColor || color}
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]}
>
{formatText(progressValue)}
</Text>
</View>
) : false}
{children}
</View>
);
}
}
export default withAnimation(ProgressCircle);