All files / src/web/components GroupBadge.tsx

100% Statements 89/89
97.43% Branches 38/39
100% Functions 4/4
100% Lines 89/89

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 1271x                               1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x   31x   31x 31x     31x 31x 31x 7x 2x 5x 31x   2x 31x 22x 31x 31x   31x 3x 3x 2x 2x 3x   31x 2x 2x 2x 2x 2x   31x 31x   31x 5x   26x 2x   24x 31x 31x   31x 7x 24x 31x 31x   31x 31x 31x 31x 31x 31x 31x   31x 31x 31x 31x 31x 31x   31x   31x 7x 7x 7x 7x 7x 7x   7x 7x     31x 2x 2x 2x   31x   31x   1x  
import { X } from 'lucide-react';
import React from 'react';
 
interface GroupBadgeProps {
  id: string;
  name: string;
  color?: string;
  isActive?: boolean;
  isClickable?: boolean;
  onRemove?: (id: string) => void;
  onClick?: (id: string) => void;
  size?: 'sm' | 'md' | 'lg';
  variant?: 'default' | 'filter' | 'preview';
  className?: string;
}
 
const GroupBadge: React.FC<GroupBadgeProps> = ({
  id,
  name,
  color = '#3B82F6',
  isActive = false,
  isClickable = true,
  onRemove,
  onClick,
  size = 'sm',
  variant = 'default',
  className = '',
}) => {
  const sizeClasses = {
    sm: 'px-2 py-0.5 text-xs',
    md: 'px-3 py-1 text-sm',
    lg: 'px-4 py-1.5 text-base',
  };
 
  const baseClasses = `
    inline-flex items-center gap-1 rounded-full font-medium transition-all duration-200
    ${sizeClasses[size]}
    ${className}
  `;
 
  const getVariantClasses = () => {
    switch (variant) {
      case 'filter':
        return isActive
          ? `bg-opacity-100 border-2 ring-2 ring-offset-1`
          : `bg-opacity-60 border border-opacity-50 hover:bg-opacity-80`;
      case 'preview':
        // Use subtle background similar to inactive filter chips for readability
        return `bg-opacity-60 border border-opacity-40`;
      default:
        return `bg-opacity-100`;
    }
  };
 
  const handleClick = (e: React.MouseEvent) => {
    e.stopPropagation();
    if (isClickable && onClick) {
      onClick(id);
    }
  };
 
  const handleRemove = (e: React.MouseEvent) => {
    e.stopPropagation();
    if (onRemove) {
      onRemove(id);
    }
  };
 
  const badgeStyle = {
    backgroundColor:
      // Inactive filter chips are light tinted
      (variant === 'filter' && !isActive)
        ? `${color}30`
        // Preview chips in rows should also be light tinted for contrast
        : (variant === 'preview')
          ? `${color}30`
          // Default: solid color, typically for active state
          : `${color}${isActive ? 'FF' : 'CC'}`,
    borderColor: color,
    color:
      // For light tinted backgrounds, keep colored text
      (variant === 'filter' && !isActive) || variant === 'preview'
        ? color
        : (isActive ? '#FFFFFF' : color),
    ringColor: isActive ? color : undefined,
  };
 
  return (
    <span
      className={`
        ${baseClasses}
        ${getVariantClasses()}
        ${isClickable ? 'cursor-pointer hover:scale-105' : 'cursor-default'}
        ${isActive ? 'shadow-sm' : ''}
      `}
      style={badgeStyle}
      onClick={handleClick}
      title={isClickable ? `Click to ${isActive ? 'remove' : 'apply'} ${name} filter` : name}
      data-testid={`group-badge-${id}`}
      data-active={isActive}
      data-variant={variant}
    >
      <span className="truncate max-w-24">{name}</span>
 
      {variant === 'filter' && onRemove && (
        <button
          type="button"
          onClick={handleRemove}
          className="ml-1 rounded-full hover:bg-black hover:bg-opacity-10 p-0.5 transition-colors"
          title={`Remove ${name} filter`}
          data-testid={`group-badge-remove-${id}`}
        >
          <X size={size === 'sm' ? 10 : size === 'md' ? 12 : 14} />
        </button>
      )}
 
      {variant === 'filter' && isActive && (
        <span className="sr-only">
          {name} filter is active
        </span>
      )}
    </span>
  );
};
 
export default GroupBadge;