-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStarRating.jsx
40 lines (35 loc) · 1009 Bytes
/
StarRating.jsx
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
import { useState } from "react";
import { FaStar } from "react-icons/fa";
import styles from "./styles.module.css";
export default function StarRating({ noOfStars = 3 }) {
const [rating, setRating] = useState(0);
const [hover, setHover] = useState(0);
function handleClick(currentIndex) {
setRating(currentIndex);
}
function handleMouseEnter(currentIndex) {
setHover(currentIndex);
}
function handleMouseLeave(currentIndex) {
setHover(rating);
}
return (
<div className={styles.wrapper}>
{[...Array(noOfStars)].map((_, index) => {
index++;
return (
<FaStar
key={index}
className={
index <= (hover || rating) ? styles.active : styles.inactive
}
onClick={() => handleClick(index)}
onMouseEnter={() => handleMouseEnter(index)}
onMouseLeave={() => handleMouseLeave(index)}
size={40}
></FaStar>
);
})}
</div>
);
}