main
Last change
on this file since 748b7f6 was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
857 bytes
|
Rev | Line | |
---|
[d24f17c] | 1 | import React from 'react';
|
---|
| 2 |
|
---|
| 3 | const 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">★</span>
|
---|
| 15 | ))}
|
---|
| 16 | {hasHalfStar && <span className="star">☆</span>}
|
---|
| 17 | {[...Array(5 - filledStars - (hasHalfStar ? 1 : 0))].map((_, index) => (
|
---|
| 18 | <span key={filledStars + index + 1} className="star">☆</span>
|
---|
| 19 | ))}
|
---|
| 20 | </div>
|
---|
| 21 | );
|
---|
| 22 | };
|
---|
| 23 |
|
---|
| 24 | export default StarRating;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.