All files / src/components evidence-tag.tsx

100% Statements 7/7
100% Branches 7/7
100% Functions 2/2
100% Lines 7/7

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                          8x   8x                         8x               2x 2x 2x       1x                                                    
import {
  useState,
  type FC,
  type ReactElement,
  type HTMLAttributes,
  useId,
} from 'react';
import cn from 'classnames';
 
import EvidenceTagIcon from '../svg/evidence-tag.svg';
 
import '../styles/components/evidence-tag.scss';
 
const size = 12;
 
const defaultIcon = <EvidenceTagIcon width={size} height={size} />;
 
type EvidenceTagProps = {
  /**
   * Displayed on the tag
   */
  label: string;
  /**
   * Icon to display
   */
  iconComponent?: ReactElement;
};
 
const EvidenceTag: FC<EvidenceTagProps & HTMLAttributes<HTMLButtonElement>> = ({
  label,
  title,
  className,
  iconComponent = defaultIcon,
  children,
  ...props
}) => {
  const buttonId = useId();
  const [contentDisplay, setContentDisplay] = useState(false);
  return (
    <>
      <button
        className={cn(className, 'evidence-tag')}
        onClick={() => setContentDisplay(!contentDisplay)}
        type="button"
        data-testid="evidence-tag-trigger"
        aria-expanded={contentDisplay}
        aria-controls={buttonId}
        {...props}
      >
        {iconComponent}
        <span className="evidence-tag__label">{label}</span>
      </button>
      {children && (
        <div
          className={`evidence-tag-content ${
            contentDisplay ? 'evidence-tag-content--visible' : ''
          }`}
          data-testid="evidence-tag-content"
          id={buttonId}
        >
          {contentDisplay && children}
        </div>
      )}
    </>
  );
};
 
export default EvidenceTag;