Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
138 lines (122 loc) · 3.31 KB
/
Copy pathindex.js
File metadata and controls
138 lines (122 loc) · 3.31 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
'use strict';
// React and react native imports
import React, {
Component,
PropTypes,
StyleSheet,
View
} from 'react-native';
// Third party imports
import Button from 'react-native-button';
import EntypoIcons from 'react-native-vector-icons/Entypo';
import EvilIconsIcons from 'react-native-vector-icons/EvilIcons';
import FontAwesomeIcons from 'react-native-vector-icons/FontAwesome';
import FoundationIcons from 'react-native-vector-icons/Foundation';
import IoniconsIcons from 'react-native-vector-icons/Ionicons';
import MaterialIconsIcons from 'react-native-vector-icons/MaterialIcons';
import OcticonsIcons from 'react-native-vector-icons/Octicons';
import ZocialIcons from 'react-native-vector-icons/Zocial';
const iconSets = {
Entypo: EntypoIcons,
EvilIcons: EvilIconsIcons,
FontAwesome: FontAwesomeIcons,
Foundation: FoundationIcons,
Ionicons: IoniconsIcons,
MaterialIcons: MaterialIconsIcons,
Octicons: OcticonsIcons,
Zocial: ZocialIcons
};
class StarRating extends Component {
constructor(props) {
super(props);
// Round rating down to nearest .5 star
const roundedRating = this.round(this.props.rating);
this.state = {
maxStars: this.props.maxStars,
rating: this.round(this.props.rating)
};
}
componentWillReceiveProps(nextProps) {
this.setState({
rating: this.round(nextProps.rating)
});
}
round(number) {
return (Math.round(number * 2) / 2);
}
pressStarButton(rating) {
this.props.selectedStar(rating);
this.setState({
rating: rating
});
}
render() {
var starsLeft = this.state.rating;
const starButtons = [];
for (var i = 0; i < this.state.maxStars; i++) {
const Icon = iconSets[this.props.iconSet];
var starIconName = this.props.emptyStar;
if (starsLeft >= 1) {
starIconName = this.props.fullStar;
} else if (starsLeft === 0.5) {
starIconName = this.props.halfStar;
}
if(this.props.showEmpty || starsLeft > 0) {
starButtons.push(
<Button
activeOpacity={0.20}
disabled={this.props.disabled}
key={i + 1}
onPress={this.pressStarButton.bind(this, i + 1)}
style={{
height: this.props.starSize,
width: this.props.starSize,
}}
>
<Icon
name={starIconName}
size={this.props.starSize}
color={this.props.starColor}
/>
</Button>
);
}
starsLeft--;
}
return (
<View style={styles.starRatingContainer}>
{starButtons}
</View>
);
}
};
StarRating.propTypes = {
disabled: PropTypes.bool,
emptyStar: PropTypes.string,
fullStar: PropTypes.string,
halfStar: PropTypes.string,
iconSet: PropTypes.string,
maxStars: PropTypes.number,
rating: PropTypes.number,
selectedStar: PropTypes.func.isRequired,
starColor: PropTypes.string,
starSize: PropTypes.number
}
StarRating.defaultProps = {
disabled: false,
emptyStar: 'star-o',
fullStar: 'star',
halfStar: 'star-half-o',
iconSet: 'FontAwesome',
maxStars: 5,
rating: 0,
starColor: 'black',
starSize: 40
}
const styles = StyleSheet.create({
starRatingContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
}
});
export default StarRating;