Skip to content

Instantly share code, notes, and snippets.

@SomtoUgeh
Last active July 4, 2021 23:12
Show Gist options
  • Save SomtoUgeh/6b2f15e220b8a260776ef10ce2f952a2 to your computer and use it in GitHub Desktop.
Save SomtoUgeh/6b2f15e220b8a260776ef10ce2f952a2 to your computer and use it in GitHub Desktop.
function Star({ marked, starId }) {
return (
<span star-id={starId} style={{ color: "#ff9933" }} role="button">
{marked ? "\u2605" : "\u2606"}
</span>
);
}
function StarRating(props) {
const [rating, setRating] = React.useState(
typeof props.rating == "number" ? props.rating : 0
);
const [selection, setSelection] = React.useState(0);
const hoverOver = event => {
let val = 0;
if (event && event.target && event.target.getAttribute("star-id"))
val = event.target.getAttribute("star-id");
setSelection(val);
};
return (
<div
onMouseOut={() => hoverOver(null)}
onClick={() =>
setRating(event.target.getAttribute("star-id") || rating)
}
onMouseOver={hoverOver}
>
{Array.from({ length: 5 }, (v, i) => (
<Star
starId={i + 1}
key={`star_${i + 1} `}
marked={selection ? selection >= i + 1 : rating >= i + 1}
/>
))}
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment