|
1 | 1 | import React from 'react'; |
2 | 2 |
|
3 | | -export const StarComponent = ({ index, rating, onRatingChange, onHover }) => { |
4 | | - const handleLeftClick = () => onRatingChange(index, false); |
5 | | - const handleRightClick = () => onRatingChange(index, true); |
| 3 | +const StarComponent = ({ index, rating, onRatingChange, onHover, readOnly = false }) => { |
| 4 | + const handleLeftClick = () => { |
| 5 | + if (!readOnly) onRatingChange(index, true); |
| 6 | + }; |
| 7 | + |
| 8 | + const handleRightClick = () => { |
| 9 | + if (!readOnly) onRatingChange(index, false); |
| 10 | + }; |
6 | 11 |
|
7 | 12 | const isFullStar = rating >= index + 1; |
8 | 13 | const isHalfStar = rating >= index + 0.5 && rating < index + 1; |
9 | 14 | const starClass = isFullStar ? "bxs-star" : isHalfStar ? "bxs-star-half" : "bx-star"; |
10 | 15 |
|
11 | 16 | return ( |
12 | 17 | <div |
13 | | - className="relative group" |
14 | | - onMouseEnter={() => onHover(index + 1)} |
15 | | - onMouseLeave={() => onHover(0)} |
| 18 | + className={`relative group ${readOnly ? 'cursor-default' : 'cursor-pointer'}`} |
| 19 | + onMouseEnter={() => !readOnly && onHover && onHover(index + 1)} |
| 20 | + onMouseLeave={() => !readOnly && onHover && onHover(0)} |
16 | 21 | > |
17 | 22 | <i |
18 | | - className={`bx ${starClass} text-5xl text-violet-500 t-500 transition-transform duration-200 group-hover:scale-110`} |
| 23 | + className={`bx ${starClass} text-5xl text-violet-500 t-500 transition-transform duration-200 ${!readOnly && 'group-hover:scale-110'}`} |
19 | 24 | ></i> |
20 | | - <button |
21 | | - className="absolute top-0 left-1/2 w-1/2 h-full cursor-pointer" |
22 | | - onClick={handleLeftClick} |
23 | | - /> |
24 | | - <button |
25 | | - className="absolute top-0 right-1/2 w-1/2 h-full cursor-pointer" |
26 | | - onClick={handleRightClick} |
27 | | - /> |
| 25 | + {!readOnly && ( |
| 26 | + <> |
| 27 | + <button |
| 28 | + className="absolute top-0 left-1/2 w-1/2 h-full cursor-pointer" |
| 29 | + onClick={handleLeftClick} |
| 30 | + /> |
| 31 | + <button |
| 32 | + className="absolute top-0 right-1/2 w-1/2 h-full cursor-pointer" |
| 33 | + onClick={handleRightClick} |
| 34 | + /> |
| 35 | + </> |
| 36 | + )} |
28 | 37 | </div> |
29 | 38 | ); |
30 | 39 | }; |
|
0 commit comments