"use client"; import * as React from "react"; import { cn } from "./lib/utils"; export interface StarRatingInputProps extends Omit, "onChange"> { value?: number; defaultValue?: number; onValueChange?: (value: number) => void; max?: number; label?: string; } export function StarRatingInput({ value: controlledValue, defaultValue = 0, onValueChange, max = 5, label, className, ...props }: StarRatingInputProps) { const [inner, setInner] = React.useState(defaultValue); const [hover, setHover] = React.useState(0); const value = controlledValue ?? inner; const shown = hover || value; const pick = (v: number) => { setInner(v); onValueChange?.(v); }; return (
{label ? ( {label} ) : null}
setHover(0)} > {Array.from({ length: max }).map((_, i) => { const v = i + 1; const lit = v <= shown; return ( ); })} {shown > 0 ? `${shown}/${max}` : "Tap to rate"}
); }