-
Notifications
You must be signed in to change notification settings - Fork 200
/
StarRating.js
190 lines (172 loc) · 4.46 KB
/
StarRating.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
// React and react native imports
import React, { Component } from 'react';
import { View, ViewPropTypes, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
import { View as AnimatableView } from 'react-native-animatable';
// Local file imports
import StarButton from './StarButton';
const ANIMATION_TYPES = [
'bounce',
'flash',
'jello',
'pulse',
'rotate',
'rubberBand',
'shake',
'swing',
'tada',
'wobble',
];
const propTypes = {
activeOpacity: PropTypes.number,
animation: PropTypes.oneOf(ANIMATION_TYPES),
buttonStyle: ViewPropTypes.style,
containerStyle: ViewPropTypes.style,
disabled: PropTypes.bool,
emptyStar: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
PropTypes.number,
]),
emptyStarColor: PropTypes.string,
fullStar: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
PropTypes.number,
]),
fullStarColor: PropTypes.string,
halfStar: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
PropTypes.number,
]),
halfStarColor: PropTypes.string,
halfStarEnabled: PropTypes.bool,
// eslint-disable-next-line react/forbid-prop-types
icoMoonJson: PropTypes.object,
iconSet: PropTypes.string,
maxStars: PropTypes.number,
rating: PropTypes.number,
reversed: PropTypes.bool,
starSize: PropTypes.number,
starStyle: ViewPropTypes.style,
selectedStar: PropTypes.func,
};
const defaultProps = {
activeOpacity: 0.2,
animation: undefined,
buttonStyle: {},
containerStyle: {},
disabled: false,
emptyStar: 'star-o',
emptyStarColor: 'gray',
fullStar: 'star',
fullStarColor: 'black',
halfStar: 'star-half-o',
halfStarColor: undefined,
halfStarEnabled: false,
icoMoonJson: undefined,
iconSet: 'FontAwesome',
maxStars: 5,
rating: 0,
reversed: false,
starSize: 40,
starStyle: {},
selectedStar: () => {},
};
class StarRating extends Component {
constructor(props) {
super(props);
this.starRef = [];
this.onStarButtonPress = this.onStarButtonPress.bind(this);
}
onStarButtonPress(rating) {
const { selectedStar } = this.props;
selectedStar(rating);
}
render() {
const {
activeOpacity,
animation,
buttonStyle,
containerStyle,
disabled,
emptyStar,
emptyStarColor,
fullStar,
fullStarColor,
halfStar,
halfStarColor,
halfStarEnabled,
icoMoonJson,
iconSet,
maxStars,
rating,
reversed,
starSize,
starStyle,
} = this.props;
const newContainerStyle = {
flexDirection: reversed ? 'row-reverse' : 'row',
justifyContent: 'space-between',
...StyleSheet.flatten(containerStyle),
};
// 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 = fullStarColor;
} else if (starsLeft === 0.5) {
starIconName = halfStar;
if (halfStarColor) {
finalStarColor = halfStarColor;
} else {
finalStarColor = fullStarColor;
}
}
const starButtonElement = (
<AnimatableView
key={i}
ref={(node) => { this.starRef.push(node); }}
>
<StarButton
activeOpacity={activeOpacity}
buttonStyle={buttonStyle}
disabled={disabled}
halfStarEnabled={halfStarEnabled}
icoMoonJson={icoMoonJson}
iconSet={iconSet}
onStarButtonPress={(event) => {
if (animation && ANIMATION_TYPES.includes(animation)) {
for (let s = 0; s <= i; s++) {
this.starRef[s][animation](1000 + (s * 200));
}
}
this.onStarButtonPress(event);
}}
rating={i + 1}
reversed={reversed}
starColor={finalStarColor}
starIconName={starIconName}
starSize={starSize}
starStyle={starStyle}
/>
</AnimatableView>
);
starButtons.push(starButtonElement);
starsLeft -= 1;
}
return (
<View style={newContainerStyle} pointerEvents={disabled ? 'none' : 'auto'}>
{starButtons}
</View>
);
}
}
StarRating.propTypes = propTypes;
StarRating.defaultProps = defaultProps;
export default StarRating;