-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
225 lines (209 loc) · 7.6 KB
/
index.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {
View,
Image,
TouchableOpacity,
ImageBackground
} from 'react-native'
export default class StarReview extends Component {
constructor(props) {
super(props)
// 'value' and 'rating' are deprecated and have become 'display' and 'default'
this.displayValue = props.display || props.value
this.defaultRating = props.default || props.rating
this.state = {
rating: this.defaultRating
}
this.isReactElement = React.isValidElement
}
componentDidUpdate(prevProps) {
let previousDefaultRating = prevProps.rating || prevProps.default
let defaultRating = this.props.rating || this.props.default
if (previousDefaultRating !== defaultRating) {
this.setState({
rating: defaultRating,
})
}
}
createPartialStar(partial, blockStyle, emptyBlockStyle, starStyle) {
return this.props.opacity ?
this.isReactElement(this.props.fullStar) ?
<View style={{opacity: partial}}>
{this.props.fullStar}
</View>
:
<ImageBackground style={starStyle} source={this.props.emptyStar}>
<Image style={{
height: this.props.starSize,
width: this.props.starSize,
opacity: partial,
backgroundColor:'transparent'}} source={this.props.fullStar}/>
</ImageBackground>
:
this.isReactElement(this.props.fullStar) ?
<View style={{flexDirection: 'row'}}>
<View>{this.props.emptyStar}</View>
<View style={{maxWidth: `${Math.round(partial * 100)}%`, overflow: 'hidden', position: 'absolute'}}>{this.props.fullStar}</View>
</View>
:
<ImageBackground style={starStyle} source={this.props.emptyStar}>
<View style={{flexDirection: 'row'}}>
<View style={blockStyle}>
<Image style={{height: this.props.starSize, width: this.props.starSize, backgroundColor: 'transparent', position: 'absolute' }} source={this.props.fullStar}/>
</View>
<View style={emptyBlockStyle}></View>
</View>
</ImageBackground>
}
displayMode() {
const partial = this.displayValue - Math.floor(this.displayValue)
const emptyBlockStyle = {height: this.props.starSize, width: this.props.starSize * (1.0 - partial), backgroundColor: 'transparent'}
const blockStyle = {height: this.props.starSize, width: this.props.starSize * partial, backgroundColor: 'transparent', overflow: 'hidden'}
const starStyle = {height: this.props.starSize, width: this.props.starSize, backgroundColor: this.props.backingColor}
const spacingStyle = {paddingLeft: this.props.spacing / 2, paddingRight: this.props.spacing / 2}
const stars = []
for (let i = 1; i < this.props.count + 1; i++) {
if (i == Math.floor(this.displayValue) + 1) {
//partial star
const partialStarComponent =
<View key={i} style={spacingStyle}>
{this.createPartialStar(partial, blockStyle, emptyBlockStyle, starStyle)}
</View>
stars.push(partialStarComponent)
} else if (i > Math.floor(this.displayValue) + 1) {
//empty stars
const emptyStarComponent = this.isReactElement(this.props.emptyStar) ?
<View style={spacingStyle} key={i}>{this.props.emptyStar}</View>
:
<View key={i} style={spacingStyle}>
<Image style={starStyle} source={this.props.emptyStar}/>
</View>
stars.push(emptyStarComponent)
} else {
//filled stars
const starComponent = this.isReactElement(this.props.fullStar) ?
<View style={spacingStyle} key={i}>{this.props.fullStar}</View>
:
<View key={i} style={spacingStyle}>
<Image style={starStyle} source={this.props.fullStar}/>
</View>
stars.push(starComponent)
}
}
return (
<View>
<View style={{flexDirection: 'row', justifyContent: 'center'}}>{stars}</View>
</View>
)
}
createHalfStarMember(index, star, halfStar) {
// if not a halfStar, either fullStar or emptyStar
let starComponent = halfStar || star
starComponent = this.isReactElement(starComponent) ?
starComponent
:
<ImageBackground style={{width: this.props.starSize, height: this.props.starSize}} source={star}>
<Image style={{width: this.props.starSize, height: this.props.starSize}} source={halfStar}/>
</ImageBackground>
return (
<View key={index} style={{paddingLeft: this.props.spacing/2, paddingRight: this.props.spacing/2}}>
{starComponent}
<View style={{flexDirection: 'row', position: 'absolute'}}>
<TouchableOpacity style={{height: this.props.starSize, width: this.props.starSize/2}} disabled={this.props.disabled} onPress={()=>{
this.setState({rating: index - 0.5})
this.props.update(index - 0.5)
}}/>
<TouchableOpacity style={{height: this.props.starSize, width: this.props.starSize/2}} disabled={this.props.disabled} onPress={()=>{
this.setState({rating: index})
this.props.update(index)
}}/>
</View>
</View>
)
}
halfRatingMode() {
const stars = []
for (let i = 1; i < this.props.count + 1; i++) {
const star = (i <= this.state.rating) ? this.props.fullStar : this.props.emptyStar
const halfStar = (this.state.rating + 0.5 == i) ? this.props.halfStar : null
stars.push(
this.createHalfStarMember(i, star, halfStar)
)
}
return (
<View style={{flexDirection: 'row', justifyContent: 'center'}}>
{stars}
</View>
)
}
createFullStarMember(index, star) {
const starComponent = this.isReactElement(star) ?
star
:
<Image style={{width: this.props.starSize, height: this.props.starSize}} source={star}/>
return (
<View key={index} style={{paddingLeft: this.props.spacing/2, paddingRight: this.props.spacing/2}}>
<TouchableOpacity disabled={this.props.disabled} onPress={()=>{
this.setState({rating: index})
this.props.update(index)
}}>
{starComponent}
</TouchableOpacity>
</View>
)
}
fullRatingMode() {
const stars = []
for (let i = 1; i < this.props.count + 1; i++) {
const star = (i <= this.state.rating) ? this.props.fullStar : this.props.emptyStar
stars.push(
this.createFullStarMember(i, star)
)
}
return (
<View style={{flexDirection: 'row', justifyContent: 'center'}}>
{stars}
</View>
)
}
render() {
const view = this.displayValue == null ?
(this.props.half ? this.halfRatingMode() : this.fullRatingMode())
:
this.displayMode()
return (
<View>
{view}
</View>
)
}
}
StarReview.propTypes = {
display: PropTypes.number,
count: PropTypes.number,
default: PropTypes.number,
emptyStar: PropTypes.oneOfType([PropTypes.number, PropTypes.object]).isRequired,
fullStar: PropTypes.oneOfType([PropTypes.number, PropTypes.object]).isRequired,
halfStar: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
update: PropTypes.func,
starSize: PropTypes.number,
opacity: PropTypes.bool,
half: PropTypes.bool,
spacing: PropTypes.number,
disabled: PropTypes.bool,
}
StarReview.defaultProps = {
fullStar: require('./example-images/starFilled.png'),
halfStar: require('./example-images/starHalf.png'),
emptyStar: require('./example-images/starEmpty.png'),
disabled: false,
display: null,
count: 5,
default: 0,
starSize: 30,
update: () => {},
opacity: false,
half: false,
spacing: 0
}