-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStarRating.tsx
83 lines (75 loc) · 1.64 KB
/
StarRating.tsx
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
import React from "react";
import { FontAwesome } from "@expo/vector-icons";
import { View, Text, StyleSheet } from "react-native";
export default function StarRating({
rating,
count,
size,
}: {
rating: number;
count: number;
size?: number;
}) {
const whole = Math.floor(rating);
const isHalf = rating - whole >= 0.5 ? true : false;
const color = "#FFE000";
const defaultSize = 20;
function buildStars() {
const stars = [];
for (let i = 0; i < whole; i++) {
stars.push(
<FontAwesome
key={`${i}`}
name="star"
color={color}
size={size || defaultSize}
/>
);
}
if (isHalf)
stars.push(
<FontAwesome
key={`${stars.length}`}
name="star-half"
color={color}
style={{ marginRight: 4 }}
size={size || defaultSize}
/>
);
if (stars.length < 5) {
let j = 1;
for (let i = 5 - stars.length; i > 0; i--) {
stars.push(
<FontAwesome
key={`${stars.length + j}`}
name="star-o"
color={color}
size={size || defaultSize}
/>
);
j++;
}
}
return stars;
}
return (
<View style={styles.container}>
{buildStars()}
<Text style={styles.textRating}>{rating}</Text>
<Text style={styles.textCount}>({count})</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
display: "flex",
flexDirection: "row",
alignItems: "center",
marginBottom: 10,
},
textRating: {
fontWeight: "bold",
marginHorizontal: 5,
},
textCount: {},
});