Rating.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import clsx from "clsx";
  2. import type { RatingTypes } from "./type";
  3. import StarIcon from "./icons/StarIcon";
  4. export const Rating = ({
  5. length = 5,
  6. star = 0,
  7. size = "size-4",
  8. className,
  9. reviewCount,
  10. onReviewClick,
  11. }: RatingTypes) => {
  12. const rating = star ?? 0;
  13. const reviewCountToShow = reviewCount ?? star > 0;
  14. return (
  15. <div className={clsx("flex items-center gap-x-2", className)}>
  16. {reviewCountToShow ? (
  17. <>
  18. <div className="flex gap-x-0.5">
  19. {Array.from({ length }).map((_, index) => (
  20. <StarIcon
  21. key={index}
  22. className={clsx(
  23. size,
  24. index < rating
  25. ? "fill-yellow-500 dark:fill-yellow-500"
  26. : "fill-black dark:fill-gray-900",
  27. "fill-black dark:stroke-white"
  28. )}
  29. />
  30. ))}
  31. </div>
  32. <span className="text-sm text-gray-600 dark:text-gray-400">
  33. ({reviewCountToShow} {reviewCountToShow === 1 ? 'Review' : 'Reviews'})
  34. </span>
  35. </>
  36. ) : (
  37. <span
  38. className="text-sm text-blue-600 underline cursor-pointer"
  39. onClick={onReviewClick}
  40. >
  41. Write a review
  42. </span>
  43. )}
  44. </div>
  45. );
  46. };