source: CookCraft-FrontEnd/CookCraft-FrontEnd-master/cookcraft-app/src/components/RecipesComponents/StarRating.jsx@ f08e256

Last change on this file since f08e256 was d7b7f00, checked in by Gorazd Biskoski <gorazdbiskoskii@…>, 4 weeks ago

Add project

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import React, { useState } from 'react';
2import { FaStar } from 'react-icons/fa';
3import styles from '../../css/RecipesCss/recipe-card-style.module.css';
4
5const StarRating = ({ rating, setRating, readOnly = false }) => {
6 const [hoverRating, setHoverRating] = useState(0);
7
8 const handleMouseOver = (starValue) => {
9 if (!readOnly) {
10 setHoverRating(starValue);
11 }
12 };
13
14 const handleMouseLeave = () => {
15 if (!readOnly) {
16 setHoverRating(0);
17 }
18 };
19
20 const handleClick = (starValue) => {
21 if (!readOnly && setRating) {
22 setRating(starValue);
23 }
24 };
25
26 return (
27 <div className={styles.starRating}>
28 {[...Array(5)].map((_, index) => {
29 const starValue = index + 1;
30 return (
31 <FaStar
32 key={index}
33 className={`${styles.star} ${
34 (hoverRating || rating) >= starValue ? styles.active : ''
35 }`}
36 onMouseOver={!readOnly ? () => handleMouseOver(starValue) : null}
37 onMouseLeave={!readOnly ? handleMouseLeave : null}
38 onClick={!readOnly ? () => handleClick(starValue) : null}
39 style={{ cursor: readOnly ? 'not-allowed' : 'pointer' }}
40 />
41 );
42 })}
43 </div>
44 );
45};
46
47export default StarRating;
Note: See TracBrowser for help on using the repository browser.