import { useId } from 'react'; interface StarRatingProps { value: number; max?: number; size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'; onChange?: (value: number) => void; label?: string; } const SIZE_CLASS: Record, string> = { xs: 'rating-xs', sm: 'rating-sm', md: 'rating-md', lg: 'rating-lg', xl: 'rating-xl', }; /** Read-only when `onChange` is omitted, interactive otherwise. Clicking the * currently-selected star clears the rating (daisyUI radios can't do this natively). */ const StarRating = ({ value, max = 5, size = 'sm', onChange, label }: StarRatingProps) => { const name = useId(); const stars = Array.from({ length: max }, (_, i) => i + 1); if (!onChange) { return (
{stars.map((n) => (
))}
); } return (
onChange(0)} aria-label="Clear rating" /> {stars.map((n) => ( onChange(n)} onClick={() => { if (value === n) onChange(0); }} aria-label={`Rate ${n} of ${max}`} /> ))}
); }; export default StarRating;