|
| 1 | +import { Icon, IconType } from "@/primitives/Icon"; |
| 2 | +import { cn } from "@/lib/utils"; |
| 3 | + |
| 4 | +export type CheckerReviewsLabelProps = React.SVGProps<SVGSVGElement> & { |
| 5 | + posReviews: number; |
| 6 | + negReviews: number; |
| 7 | +}; |
| 8 | + |
| 9 | +const ICON_CLASSES = { |
| 10 | + base: "size-7 bg-white", |
| 11 | + positive: " fill-green-600", |
| 12 | + negative: "fill-orange-200", |
| 13 | + border: "border border-gray-100 rounded-full", |
| 14 | +}; |
| 15 | + |
| 16 | +const renderIcons = (countPos: number, countNeg: number, props: React.SVGProps<SVGSVGElement>) => { |
| 17 | + const negProps = { ...props, type: IconType.X }; |
| 18 | + const posProps = { ...props, type: IconType.CHECK }; |
| 19 | + const totalReviews = countPos + countNeg; |
| 20 | + |
| 21 | + return Array.from({ length: totalReviews }).map((_, index) => ( |
| 22 | + <Icon |
| 23 | + key={index} |
| 24 | + {...(index < countPos ? posProps : negProps)} |
| 25 | + className={cn( |
| 26 | + ICON_CLASSES.base, |
| 27 | + ICON_CLASSES.border, |
| 28 | + index >= countPos ? ICON_CLASSES.negative : ICON_CLASSES.positive, |
| 29 | + index !== 0 ? "-ml-2" : "", |
| 30 | + )} |
| 31 | + /> |
| 32 | + )); |
| 33 | +}; |
| 34 | + |
| 35 | +export const CheckerReviewsLabelComponents: React.FC<CheckerReviewsLabelProps> = ({ |
| 36 | + className, |
| 37 | + posReviews, |
| 38 | + negReviews, |
| 39 | + ...props |
| 40 | +}) => { |
| 41 | + if (posReviews < 0) { |
| 42 | + posReviews = 0; |
| 43 | + } |
| 44 | + if (negReviews < 0) { |
| 45 | + negReviews = 0; |
| 46 | + } |
| 47 | + const totalReviews = posReviews + negReviews; |
| 48 | + return ( |
| 49 | + <div className={cn("flex items-center gap-2", className)}> |
| 50 | + <div className="flex items-center">{renderIcons(posReviews, negReviews, { ...props })}</div> |
| 51 | + <span className="text-[16px]/[24px]">{`${totalReviews} Reviews`}</span> |
| 52 | + </div> |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
| 56 | +export const CheckerReviewsLabel: React.FC<CheckerReviewsLabelProps> = ({ |
| 57 | + posReviews, |
| 58 | + negReviews, |
| 59 | + className, |
| 60 | + ...props |
| 61 | +}) => ( |
| 62 | + <CheckerReviewsLabelComponents |
| 63 | + className={className} |
| 64 | + posReviews={posReviews} |
| 65 | + negReviews={negReviews} |
| 66 | + {...props} |
| 67 | + /> |
| 68 | +); |
0 commit comments