-
Notifications
You must be signed in to change notification settings - Fork 14
/
StarRating.js
87 lines (79 loc) · 1.86 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
'use strict';
import React, {
Component,
} from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
} from 'react-native';
export default class StarRating extends Component {
static propTypes = {
disabled: React.PropTypes.bool,
maxStars: React.PropTypes.number,
rating: React.PropTypes.number,
onStarChange: React.PropTypes.func,
style: View.propTypes.style,
starSize: React.PropTypes.number,
};
static defaultProps = {
disabled: true,
maxStars: 5,
rating: 0,
};
constructor(props) {
super(props);
const roundedRating = (Math.round(this.props.rating * 2) / 2);
this.state = {
maxStars: this.props.maxStars,
rating: roundedRating
}
}
pressStarButton (rating) {
if (!this.props.disabled) {
if (rating != this.state.rating) {
if (this.props.onStarChange) {
this.props.onStarChange(rating);
}
this.setState({
rating: rating,
});
}
}
}
render() {
const starsLeft = this.state.rating;
const starButtons = [];
for (let i = 0; i < this.state.maxStars; i++) {
const starColor = (i + 1) <= starsLeft ? styles.selectedColor : styles.unSelectedColor;
const starStr = '\u2605'
starButtons.push(
<TouchableOpacity
activeOpacity={0.20}
key={i + 1}
onPress={this.pressStarButton.bind(this, (i + 1))}
>
<Text style={[starColor,{fontSize:this.props.starSize}]}>{starStr}</Text>
</TouchableOpacity>
);
}
return (
<View style={[styles.starRatingContainer]}>
{starButtons}
</View>
);
}
}
const styles = StyleSheet.create({
starRatingContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
},
selectedColor: {
color:'#FF4946'
},
unSelectedColor:{
color:'#999999'
}
});