forked from hendiko/react-native-percentage-circle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
263 lines (240 loc) · 5.93 KB
/
index.js
File metadata and controls
263 lines (240 loc) · 5.93 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/** React Native Percentage Circle
** @github https://github.com/JackPu/react-native-percentage-circle
** React Native Version >=0.25
** to fixed react native version
*
* 改模块源自 JackPu 的 react-native-percentage-circle v1.0.7
*
* 原模块在安卓手机上,当 percent 小于 50 时,进度圆环绘制会出现错误。
*
* 原因如下:
*
* 参见 React native 0.51 Known Issues(https://reactnative.cn/docs/0.51/known-issues.html)
*
* > overflow 样式在 Android 默认为 hidden 而且无法更改,且如果父容器有borderRadius圆角边框样式,
* > 那么即便开启了overflow:hidden也仍然无法把子视图超出圆角边框的部分裁切掉。
* > 这个问题只存在于Android上,iOS并没有这个问题
*
* 由于无法裁剪超出圆角边框的部分,所以即便当 percent < 50 时,设置 LeftTransformerDegree 为 0,
* View#LeftTransformer 的半圆仍会显示在页面上。
*
* 因此本模块修改了 LeftTransformer 与 RightTransformer 的视图顺序,
* 并且在 percent < 50 时,LeftTransformer 旋转 180 度,同时将其颜色修改为与底色相同。
*
*
* Tested in devices as bellow:
* * Android:
* - Device: XiaoMi 5
* - OS: Android 6.0.1 MXB48T
* - react: 15.4.1
* - react-native: 0.39.2
*
* * iOS:
* - Device: iPhone 6s
* - OS: iOS 11.4
* - react: 15.3.1
* - react-native: 0.33.1
**/
import React, { Component } from "react";
import { StyleSheet, View, Text } from "react-native";
const styles = StyleSheet.create({
circle: {
overflow: "hidden",
position: "relative",
justifyContent: "center",
alignItems: "center",
backgroundColor: "#e3e3e3"
},
leftWrap: {
overflow: "hidden",
position: "absolute",
top: 0
},
loader: {
position: "absolute",
left: 0,
top: 0,
borderRadius: 1000
},
innerCircle: {
overflow: "hidden",
position: "relative",
justifyContent: "center",
alignItems: "center"
},
text: {
fontSize: 11,
color: "#888"
}
});
const deg = number => `${number}deg`;
function Disabled(props) {
let { radius, disabledText } = props.data;
let diameter = radius * 2;
return (
<View
style={[
styles.circle,
{
width: diameter,
height: diameter,
borderRadius: radius
}
]}
>
<Text style={styles.text}>{disabledText}</Text>
</View>
);
}
function RightTransformer(props) {
let { radius, rotate, color, percent } = props.data;
let diameter = radius * 2;
let half = percent >= 50;
let rightTransformerDegree = deg(half ? 180 : percent * 3.6);
return (
<View
style={[
styles.leftWrap,
{
width: radius,
height: diameter,
left: radius,
transform: [
{ translateX: -radius / 2 },
{ rotate: deg(rotate) },
{ translateX: radius / 2 }
]
}
]}
>
<View
style={[
styles.loader,
{
left: -radius,
width: radius,
height: diameter,
borderTopRightRadius: 0,
borderBottomRightRadius: 0,
backgroundColor: color,
transform: [
{ translateX: radius / 2 },
{ rotate: rightTransformerDegree },
{ translateX: -radius / 2 }
]
}
]}
/>
</View>
);
}
function LeftTransformer(props) {
let { radius, rotate, color, bgcolor, percent } = props.data;
let diameter = radius * 2;
let half = percent >= 50;
let leftTransformerDegree = deg(half ? (percent - 50) * 3.6 : 180);
return (
<View
style={[
styles.leftWrap,
{
width: radius,
height: diameter,
left: 0,
transform: [
{ translateX: radius / 2 },
{ rotate: deg(rotate) },
{ translateX: -radius / 2 }
]
}
]}
>
<View
style={[
styles.loader,
{
left: radius,
width: radius,
height: diameter,
borderTopLeftRadius: 0,
borderBottomLeftRadius: 0,
backgroundColor: half ? color : bgcolor,
transform: [
{ translateX: -radius / 2 },
{ rotate: leftTransformerDegree },
{ translateX: radius / 2 }
]
}
]}
/>
</View>
);
}
function InnerCircle(props) {
let {
radius,
borderWidth,
innerColor,
percent,
textStyle,
children
} = props.data;
if (borderWidth < 2) borderWidth = 2;
return (
<View
style={[
styles.innerCircle,
{
width: (radius - borderWidth) * 2,
height: (radius - borderWidth) * 2,
borderRadius: radius - borderWidth,
backgroundColor: innerColor
}
]}
>
{children ? (
children
) : (
<Text style={[styles.text, ...textStyle]}>{percent}%</Text>
)}
</View>
);
}
class PercentageCircle extends Component {
render() {
let { bgcolor, disabled, radius } = this.props;
let diameter = radius * 2;
return disabled ? (
<Disabled data={this.props} />
) : (
<View
style={[
styles.circle,
{
width: diameter,
height: diameter,
borderRadius: radius,
backgroundColor: bgcolor
}
]}
>
{/* 右长方形,左半边圆 */}
<RightTransformer data={this.props} />
{/* 左长方形,右半圆 */}
<LeftTransformer data={this.props} />
{/* 圆环内部 */}
<InnerCircle data={this.props} />
</View>
);
}
}
// set some attributes default value
PercentageCircle.defaultProps = {
textStyle: [],
percent: 0,
borderWidth: 2,
bgcolor: "#e3e3e3",
innerColor: "#fff",
rotate: 0
};
module.exports = PercentageCircle;