All files / src/components/ui/PriceDisplay index.tsx

100% Statements 172/172
100% Branches 19/19
100% Functions 5/5
100% Lines 172/172

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 1731x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 163x 163x 163x 163x 163x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 163x 163x 163x 182x 182x 182x 182x 5x 5x 5x 5x 5x 5x 5x 5x 182x 182x 182x 182x 7x 7x 7x 7x 7x 7x 7x 7x 182x 182x 182x 182x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x  
'use client';
 
import { cn } from '@/lib/core';
import { formatCurrency, formatNumber } from '@/lib/core';
import { PriceDisplayProps } from '@/types/ui';
 
const sizeConfig = {
  sm: {
    price: 'text-sm',
    original: 'text-xs',
    badge: 'text-xs px-1.5 py-0.5'},
  md: {
    price: 'text-lg',
    original: 'text-sm',
    badge: 'text-xs px-2 py-1'},
  lg: {
    price: 'text-xl',
    original: 'text-base',
    badge: 'text-sm px-2 py-1'},
  xl: {
    price: 'text-2xl',
    original: 'text-lg',
    badge: 'text-base px-3 py-1'}};
 
/**
 * Calculate savings amount and percentage
 */
function calculateSavings(price: number, discountedPrice: number) {
  const amount = price - discountedPrice;
  const percentage = Math.round((amount / price) * 100);
  return { amount, percentage };
}
 
/**
 * PriceDisplay - Consistent price formatting component
 *
 * Features:
 * - Supports regular and discounted prices
 * - Multiple sizes and layouts
 * - Optional savings badge
 * - Optional discount percentage
 * - Currency formatting
 *
 * @example
 * ```tsx
 * // Simple price
 * <PriceDisplay price={199.99} />
 *
 * // With discount
 * <PriceDisplay
 *   price={199.99}
 *   discountedPrice={149.99}
 *   showSavings={true}
 * />
 *
 * // Large size with percentage
 * <PriceDisplay
 *   price={299.99}
 *   discountedPrice={199.99}
 *   size="lg"
 *   showPercentage={true}
 * />
 *
 * // Vertical layout
 * <PriceDisplay
 *   price={99.99}
 *   discountedPrice={79.99}
 *   layout="vertical"
 * />
 * ```
 */
export function PriceDisplay({
  price,
  discountedPrice,
  size = 'md',
  layout = 'horizontal',
  showSavings = false,
  showPercentage = false,
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  currency: _currency = '$', // Reserved for future i18n support
  className}: PriceDisplayProps) {
  const config = sizeConfig[size];
  const hasDiscount = discountedPrice !== undefined && discountedPrice < price;
  const savings = hasDiscount ? calculateSavings(price, discountedPrice) : null;
 
  const containerClassName = cn(
    'flex items-center',
    layout === 'horizontal' ? 'gap-2' : 'flex-col items-start gap-1',
    className
  );
 
  return (
    <div className={containerClassName}>
      {/* Current/Discounted Price */}
      <span className={cn('font-medium text-dark dark:text-gray-100', config.price)}>
        {hasDiscount ? formatCurrency(discountedPrice) : formatCurrency(price)}
      </span>
 
      {/* Original Price (if discounted) */}
      {hasDiscount && (
        <span className={cn('text-dark-4 dark:text-gray-500 line-through', config.original)}>
          {formatCurrency(price)}
        </span>
      )}
 
      {/* Savings Badge */}
      {hasDiscount && showSavings && savings && (
        <span
          className={cn(
            'bg-green-100 text-green-700 font-medium rounded',
            config.badge
          )}
        >
          Save {formatCurrency(savings.amount)}
        </span>
      )}
 
      {/* Discount Percentage Badge */}
      {hasDiscount && showPercentage && savings && (
        <span
          className={cn(
            'bg-red-100 text-red-700 font-medium rounded',
            config.badge
          )}
        >
          {formatNumber(savings.percentage)}% OFF
        </span>
      )}
    </div>
  );
}
 
/**
 * Compact price display (small size, no extras)
 */
export function PriceDisplayCompact({ price, discountedPrice }: Pick<PriceDisplayProps, 'price' | 'discountedPrice'>) {
  return <PriceDisplay price={price} discountedPrice={discountedPrice} size="sm" />;
}
 
/**
 * Price display with savings badge
 */
export function PriceDisplayWithSavings({
  price,
  discountedPrice,
  size = 'md'}: Pick<PriceDisplayProps, 'price' | 'discountedPrice' | 'size'>) {
  return (
    <PriceDisplay
      price={price}
      discountedPrice={discountedPrice}
      size={size}
      showSavings={true}
    />
  );
}
 
/**
 * Price display with percentage badge
 */
export function PriceDisplayWithPercentage({
  price,
  discountedPrice,
  size = 'md'}: Pick<PriceDisplayProps, 'price' | 'discountedPrice' | 'size'>) {
  return (
    <PriceDisplay
      price={price}
      discountedPrice={discountedPrice}
      size={size}
      showPercentage={true}
    />
  );
}