Changeset e48199a for my-react-app/src/components/StarRating.js
- Timestamp:
- 05/07/25 18:34:01 (10 days ago)
- Branches:
- main
- Parents:
- b67dfd3
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
my-react-app/src/components/StarRating.js
rb67dfd3 re48199a 2 2 3 3 const StarRating = ({ rating }) => { 4 // Convert the rating to a number between 0 and 55 4 const normalizedRating = Math.min(Math.max(0, rating), 5); 6 // Calculate the number of filled stars7 5 const filledStars = Math.floor(normalizedRating); 8 // Calculate the number of half stars9 6 const hasHalfStar = normalizedRating - filledStars >= 0.5; 10 7 … … 12 9 <div> 13 10 {[...Array(filledStars)].map((_, index) => ( 14 <span key={ index} className="star">★</span>11 <span key={`filled-${index}`} className="star">★</span> 15 12 ))} 16 {hasHalfStar && <span className="star">☆</span>} 13 14 {hasHalfStar && <span className="star half">★</span>} 15 17 16 {[...Array(5 - filledStars - (hasHalfStar ? 1 : 0))].map((_, index) => ( 18 <span key={ filledStars + index + 1} className="star">☆</span>17 <span key={`empty-${index}`} className="star">☆</span> 19 18 ))} 20 19 </div> … … 23 22 24 23 export default StarRating; 24
Note:
See TracChangeset
for help on using the changeset viewer.