'use client'
import { Text, View, XStack } from '@hanzo/gui';
import * as React from 'react';
// Inline star icons — fixed at the one size Rating ever draws them (20px, the
// star buttons' own width/height), so there is nothing left to parameterize.
const StarFilledIcon = () => (
);
const StarOutlineIcon = () => (
);
export interface RatingProps extends Omit, 'defaultValue' | 'onChange'> {
count?: number;
label?: string | Array;
defaultValue?: number;
onValueChange?: ({ value }: { value: number }) => void;
readOnly?: boolean;
}
export const Rating = React.forwardRef(
function Rating(props, ref) {
const { count = 5, label: labelProp, defaultValue = 0, onValueChange, readOnly, className, ...rest } = props;
const [ value, setValue ] = React.useState(defaultValue);
const [ hoveredIndex, setHoveredIndex ] = React.useState(-1);
const highlightedIndex = hoveredIndex >= 0 && !readOnly ? hoveredIndex + 1 : value;
const label = Array.isArray(labelProp) ? labelProp[highlightedIndex - 1] : labelProp;
const handleClick = React.useCallback((index: number) => () => {
if (readOnly) return;
setValue(index);
onValueChange?.({ value: index });
}, [ readOnly, onValueChange ]);
const handleMouseEnter = React.useCallback((index: number) => () => {
if (readOnly) return;
setHoveredIndex(index);
}, [ readOnly ]);
const handleMouseLeave = React.useCallback(() => {
setHoveredIndex(-1);
}, []);
return (
{ Array.from({ length: count }).map((_, index) => {
const filled = index < highlightedIndex;
const starIndex = index + 1;
return (
}
tabIndex={ readOnly ? -1 : 0 }
aria-label={ `Rate ${ starIndex } of ${ count }` }
display="inline-flex"
alignItems="center"
justifyContent="center"
width={ 20 }
height={ 20 }
cursor={ readOnly ? 'default' : 'pointer' }
onClick={ handleClick(starIndex) }
onMouseEnter={ handleMouseEnter(index) }
>
{ filled ? : }
);
}) }
{ label && { label } }
);
},
);