source: my-react-app/src/components/StarRating.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 857 bytes
Line 
1import React from 'react';
2
3const StarRating = ({ rating }) => {
4 // Convert the rating to a number between 0 and 5
5 const normalizedRating = Math.min(Math.max(0, rating), 5);
6 // Calculate the number of filled stars
7 const filledStars = Math.floor(normalizedRating);
8 // Calculate the number of half stars
9 const hasHalfStar = normalizedRating - filledStars >= 0.5;
10
11 return (
12 <div>
13 {[...Array(filledStars)].map((_, index) => (
14 <span key={index} className="star">&#9733;</span>
15 ))}
16 {hasHalfStar && <span className="star">&#9734;</span>}
17 {[...Array(5 - filledStars - (hasHalfStar ? 1 : 0))].map((_, index) => (
18 <span key={filledStars + index + 1} className="star">&#9734;</span>
19 ))}
20 </div>
21 );
22};
23
24export default StarRating;
Note: See TracBrowser for help on using the repository browser.