Skip to content

Instantly share code, notes, and snippets.

@drahoslavzan
Last active August 2, 2020 17:05
Show Gist options
  • Select an option

  • Save drahoslavzan/bc044d05fac37a3985356fc95029278f to your computer and use it in GitHub Desktop.

Select an option

Save drahoslavzan/bc044d05fac37a3985356fc95029278f to your computer and use it in GitHub Desktop.
[REACT | TAILWIND] Rating Component
import * as React from "react";
import { times } from "lodash";
export type RatingProps = {
rating: number
onUpdate: (rating: number) => void
className?: string
};
export default React.forwardRef<HTMLButtonElement, RatingProps>((props, ref) => {
function handleUpdate(rating: number) {
props.onUpdate(rating);
}
return (
<div className={`${props.className} flex`}>
{times(5, i => (
<button
key={i}
ref={ref}
name="rating"
type="button"
className={`${i + 1 < 5 ? "mr-1" : ""} focus:outline-none`}
onClick={() => handleUpdate(i + 1)}
>
<svg
className={`${
i + 1 <= props.rating ? "text-blue-500" : "text-gray-700"
} block h-8 w-8`}
fill="currentColor"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
>
<path d="M10 15l-5.878 3.09 1.123-6.545L.489 6.91l6.572-.955L10 0l2.939 5.955 6.572.955-4.756 4.635 1.123 6.545z" />
</svg>
</button>
))}
</div>
);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment