import { Star } from 'lucide-react' import React, { useState } from 'react' import type { FieldProps } from './field-types' const RatingField: React.FC> = ({ value, config, onSave, readOnly }) => { const ratingConfig = config?.rating const max = Math.max(2, Math.min(6, ratingConfig?.maxValue || 5)) // Clamp 2-6 const [hoverValue, setHoverValue] = useState(null) const handleClick = (e: React.MouseEvent, rating: number) => { e.stopPropagation() if (readOnly) return let newValue if (value === rating) { newValue = 0 // Toggle off } else { newValue = rating } onSave(newValue) } const stars = Array.from({ length: max }, (_, i) => i + 1) return (
!readOnly && setHoverValue(null)} onDoubleClick={(e) => e.stopPropagation()} > {stars.map((star) => { const isFilled = hoverValue !== null ? star <= hoverValue : star <= (value || 0) return ( ) })}
) } export default RatingField