elmeg-demo/frontend/components/ui/star-rating.tsx

44 lines
1.5 KiB
TypeScript

import { useState } from "react"
import { Star } from "lucide-react"
import { cn } from "@/lib/utils"
interface RatingProps {
value: number
onChange?: (value: number) => void
readonly?: boolean
className?: string
}
export function StarRating({ value, onChange, readonly = false, className }: RatingProps) {
const [hoverValue, setHoverValue] = useState<number | null>(null)
const stars = Array.from({ length: 10 }, (_, i) => i + 1)
return (
<div className={cn("flex gap-0.5", className)}>
{stars.map((star) => (
<button
key={star}
type="button"
disabled={readonly}
className={cn(
"p-0.5 transition-colors",
readonly ? "cursor-default" : "cursor-pointer hover:scale-110"
)}
onMouseEnter={() => !readonly && setHoverValue(star)}
onMouseLeave={() => !readonly && setHoverValue(null)}
onClick={() => !readonly && onChange?.(star)}
>
<Star
className={cn(
"h-4 w-4",
(hoverValue !== null ? star <= hoverValue : star <= value)
? "fill-primary text-primary"
: "fill-muted text-muted-foreground"
)}
/>
</button>
))}
</div>
)
}