-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.tsx
242 lines (202 loc) · 6.75 KB
/
index.tsx
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import React,{JSXElementConstructor} from 'react';
import { View,Text, TouchableOpacity, Animated } from 'react-native';
import { FilledStar, EmptyStar } from './includes/lib';
interface RatingProps {
/**
* Max number of stars to show (Default 5)
*/
maxStars?: number,
/**
* Rating stars to show (Default 0)
*/
stars?: number,
/**
* Size of the stars
*/
size?: number,
/**
* Color of the stars
*/
color?: string,
/**
* If bordered stars are to be shown
*/
bordered?: boolean,
}
const Rating: React.FC<RatingProps> = ({
maxStars,
stars,
size,
color,
bordered
}) => {
const _number = maxStars ?? 5;
const _stars = stars ?? 0;
const _size = size ?? 25;
const _color = color ?? "#FFDF00";
const _bordered = bordered ?? false;
return(
<View style={{flexDirection: 'row', alignItems: 'center'}}>
{
Array(_number).fill('\u4215485').map((item, index) => {
if(_bordered) {
return (
<EmptyStar
key={index}
size={_size}
color={
(_stars <= (index+1)) ? "#BABABA" :_color
}
/>
)
}else{
if(_stars >= (index+1)){
return <FilledStar key={index} size={_size} color={_color} />
}else{
return <EmptyStar key={index} size={_size} color={_color} />
}
}
})
}
</View>
)
}
interface RatingInputProps {
/**
* State variable that will store the rating
*/
rating: number,
/**
* Function to set the rating which will be stored in your local state
*/
setRating: React.Dispatch<React.SetStateAction<undefined>>,
/**
* Max number of stars to show (Default 5)
*/
maxStars?: number,
/**
* Rating stars to show (Default 0)
*/
stars?: number,
/**
* Size of the stars
*/
size?: number,
/**
* Color of the stars
*/
color?: string,
/**
* If bordered stars are to be shown
*/
bordered?: boolean,
/**
* Callback Function to call after the rating is given
*/
onRating?: () => void,
}
const RatingInput: React.FC<RatingInputProps> = ({
rating,
setRating,
maxStars,
size,
color,
bordered,
onRating = () => {}
}) => {
const _number = maxStars ?? 5;
const _size = size ?? 25;
const _color = color ?? "#FFDF00";
const _bordered = bordered ?? false;
const [_firstRender, setFirstRender] = React.useState(true);
const _anim = React.useRef(new Animated.Value(1)).current;
const _up = () => {
Animated.timing(_anim, {
toValue: 1.5,
duration: 150,
useNativeDriver: true
}).start(({finished}) => {
_down();
});
};
const _down = () => {
Animated.timing(_anim, {
toValue: 1,
duration: 50,
useNativeDriver: true
}).start();
};
React.useEffect(() => {
if(!_firstRender){
onRating();
}
}, [rating])
return(
<View style={{flexDirection: 'row'}}>
{
Array(_number).fill('\u4215488').map((item, index) => {
if(_bordered) {
return(
<Animated.View key={index}>
<TouchableOpacity
onPress={
() => {
_up();
setRating(index+1);
setFirstRender(false);
}
}
style={(rating == (index+1)) ? {transform: [{scale: _anim}]} : null}
>
<EmptyStar
size={_size}
color={
(rating < (index+1)) ? "#BABABA" : _color
}
/>
</TouchableOpacity>
</Animated.View>
)
}else{
if(rating < (index+1)){
return(
<Animated.View key={index}>
<TouchableOpacity
onPress={
() => {
_up();
setRating(index+1);
setFirstRender(false);
}
}
style={(rating == (index+1)) ? {transform: [{scale: _anim}]} : null}
>
<EmptyStar size={_size} color={_color} />
</TouchableOpacity>
</Animated.View>
)
}else{
return(
<Animated.View key={index}>
<TouchableOpacity
onPress={
() => {
_up()
setRating(index+1);
setFirstRender(false);
}
}
style={(rating == (index+1)) ? {transform: [{scale: _anim}]} : null}
>
<FilledStar size={_size} color={_color} />
</TouchableOpacity>
</Animated.View>
)
}
}
})
}
</View>
)
}
export { Rating, RatingInput };