forked from djchie/react-native-star-rating
-
Notifications
You must be signed in to change notification settings - Fork 0
/
star-rating.js
153 lines (136 loc) · 3.17 KB
/
star-rating.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
// React and react native imports
import React, {
Component,
} from 'react';
import {
View,
ViewPropTypes,
} from 'react-native';
import PropTypes from 'prop-types';
// Local file imports
import StarButton from './star-button';
class StarRating extends Component {
constructor(props) {
super(props);
this.onStarButtonPress = this.onStarButtonPress.bind(this);
}
onStarButtonPress(rating) {
const {
selectedStar,
} = this.props;
selectedStar(rating);
}
render() {
const {
style,
maxStars,
rating,
emptyStar,
emptyStarColor,
fullStar,
halfStar,
starColor,
disabled,
iconSet,
starSize,
starStyle,
buttonStyle,
halfStarEnabled,
reversed,
} = this.props;
const starRatingStyles = [
{
flexDirection: reversed ? 'row-reverse' : 'row',
justifyContent: 'space-between',
},
style,
];
// Round rating down to nearest .5 star
let starsLeft = Math.round(rating * 2) / 2;
const starButtons = [];
for (let i = 0; i < maxStars; i++) {
let starIconName = emptyStar;
let finalStarColor = emptyStarColor;
if (starsLeft >= 1) {
starIconName = fullStar;
finalStarColor = starColor;
} else if (starsLeft === 0.5) {
starIconName = halfStar;
finalStarColor = starColor;
}
const starButtonElement = (
<StarButton
activeOpacity={0.20}
disabled={disabled}
key={i}
rating={i + 1}
onStarButtonPress={this.onStarButtonPress}
iconSet={iconSet}
starSize={starSize}
starIconName={starIconName}
starColor={finalStarColor}
starStyle={starStyle}
buttonStyle={buttonStyle}
halfStarEnabled={halfStarEnabled}
reversed={reversed}
/>
);
starButtons.push(starButtonElement);
starsLeft -= 1;
}
return (
<View style={starRatingStyles}>
{starButtons}
</View>
);
}
}
StarRating.propTypes = {
style: ViewPropTypes.style,
disabled: PropTypes.bool,
emptyStar: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
PropTypes.number,
]),
fullStar: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
PropTypes.number,
]),
halfStar: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
PropTypes.number,
]),
iconSet: PropTypes.string,
maxStars: PropTypes.number,
rating: PropTypes.number,
selectedStar: PropTypes.func,
starColor: PropTypes.string,
emptyStarColor: PropTypes.string,
starSize: PropTypes.number,
starStyle: ViewPropTypes.style,
buttonStyle: ViewPropTypes.style,
halfStarEnabled: PropTypes.bool,
reversed: PropTypes.bool,
};
StarRating.defaultProps = {
style: {},
disabled: false,
emptyStar: 'star-o',
fullStar: 'star',
halfStar: 'star-half-o',
iconSet: 'FontAwesome',
maxStars: 5,
rating: 0,
selectedStar: () => undefined,
starColor: 'black',
emptyStarColor: 'gray',
starSize: 40,
starStyle: {},
buttonStyle: {},
halfStarEnabled: false,
reversed: false,
};
export default StarRating;