forked from oblador/react-native-progress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Circle.js
119 lines (111 loc) · 3.01 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
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import {
ART,
Text,
View,
} from 'react-native';
import Arc from './Shapes/Arc';
export default class ProgressCircle extends Component {
static propTypes = {
borderColor: PropTypes.string,
borderWidth: PropTypes.number,
color: PropTypes.string,
direction: PropTypes.oneOf(['clockwise', 'counter-clockwise']),
formatText: PropTypes.func,
indeterminate: PropTypes.bool,
progress: PropTypes.number,
showsText: PropTypes.bool,
size: PropTypes.number,
textStyle: PropTypes.any,
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,
};
render() {
let {
borderColor,
borderWidth,
color,
children,
direction,
formatText,
indeterminate,
progress,
showsText,
size,
textStyle,
thickness,
unfilledColor,
text,
...restProps,
} = this.props;
borderWidth = borderWidth || (indeterminate ? 1 : 0);
const radius = size / 2 - borderWidth;
const offset = {
top: borderWidth,
left: borderWidth,
};
const textOffset = borderWidth + thickness;
const textSize = size - textOffset * 2;
return (
<View {...restProps}>
<ART.Surface
width={size}
height={size}>
{unfilledColor && progress !== 1 ? (<Arc
radius={radius}
offset={offset}
startAngle={progress * 2 * Math.PI}
endAngle={2 * Math.PI}
direction={direction}
stroke={unfilledColor}
strokeWidth={thickness} />) : false}
{!indeterminate && progress ? (<Arc
radius={radius}
offset={offset}
startAngle={0}
endAngle={progress * 2 * Math.PI}
direction={direction}
stroke={color}
strokeWidth={thickness} />) : false}
{borderWidth ?
(<Arc
radius={size / 2}
startAngle={0}
endAngle={(indeterminate ? 1.8 : 2) * Math.PI}
stroke={borderColor || color}
strokeWidth={borderWidth} />) : false}
</ART.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: color,
fontSize: textSize / 4.5,
fontWeight: '300',
}, textStyle]}>{text}</Text>
</View>
) : false}
{children}
</View>
);
}
}