All files / components Button.tsx

100% Statements 6/6
61.76% Branches 21/34
50% Functions 1/2
100% Lines 5/5

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  7x                                                   7x                           7x                               25x                                                                                                                                                               27x  
import { ReactNode, MouseEventHandler } from 'react'
import Image from 'next/image'
 
interface ButtonProps {
  id: string
  style?:
    | 'supertask'
    | 'primary'
    | 'secondary'
    | 'danger'
    | 'link'
    | 'none'
    | 'smallPrimary'
  text: string
  icon?: string
  iconAltText?: string
  iconEnd?: boolean
  href?: string
  type?: 'submit' | 'reset' | 'button'
  onClick?: MouseEventHandler<HTMLElement>
  disabled?: boolean
  className?: string
  attributes?: { [key: string]: string }
  children?: ReactNode | ReactNode[] | string
  refPageAA?: string
}
 
const buttonStyles: Record<string, string> = {
  primary:
    'text-white bg-blue-primary text-xl hover:bg-deep-blue-focus active:bg-blue-pressed rounded focus:ring focus:ring-offset-4 focus:ring-deep-blue-60f focus:ring-bg-deep-blue-focus',
  secondary:
    'text-blue-60b text-xl bg-gray-30a hover:bg-gray-50a active:bg-gray-60  focus:bg-gray-60  focus:ring-deep-blue-60f focus:ring-bg-gray-50a',
  supertask:
    'text-white bg-green-50 hover:bg-green-70 active:bg-green-90 focus:ring-deep-blue-60f focus:green-70',
  danger:
    'text-white bg-red-50 hover:bg-red-70 focus:bg-red-70 active:bg-red-dark focus:ring-deep-blue-60f focus:red-dark',
  link: 'text-blue-default hover:text-blue-hover hover:underline active:text-blue-hover active:underline focus:ring focus:ring-deep-blue-60f visited:text-purple-50a',
  smallPrimary:
    'text-white bg-blue-primary hover:bg-deep-blue-focus active:bg-blue-pressed rounded focus:ring focus:ring-offset-4 focus:ring-deep-blue-60f focus:ring-bg-deep-blue-focus',
}
 
const Button = ({
  id = 'mscaPlaceholder',
  style = 'supertask',
  text = 'mscaPlaceholder',
  icon,
  iconAltText = 'mscaPlaceholder',
  iconEnd,
  href = 'no ref',
  type,
  onClick = () => {},
  disabled,
  className,
  attributes,
  children,
  refPageAA = 'mscaPlaceholder',
}: ButtonProps) => {
  const buttonStyle = style in buttonStyles ? buttonStyles[style] : ''
 
  return href === 'no ref' ? (
    <button
      className={`flex flex-row ${buttonStyle} ${
        disabled ? 'cursor-not-allowed' : ''
      } rounded px-3.5 py-1.5 focus:ring focus:ring-offset-4 ${className} `}
      onClick={onClick}
      data-gc-analytics-customclick={`${refPageAA}:${text}`}
      type={type}
      id={id}
      disabled={disabled}
      {...attributes}
      data-testid={id}
    >
      {icon && !iconEnd ? (
        <span className="grid h-8 w-8 place-items-center">
          <Image
            width={8}
            height={8}
            className="rounded pr-2"
            src={icon}
            alt={iconAltText}
          />
        </span>
      ) : undefined}
      {text}
      {children}
      {icon && iconEnd ? (
        <span className="grid h-8 w-8 place-items-center">
          <Image
            width={8}
            height={8}
            className="rounded pl-2"
            src={icon}
            alt={iconAltText}
          />
        </span>
      ) : undefined}
    </button>
  ) : (
    <a
      data-testid={id}
      href={href}
      className={`flex flex-row ${disabled ? 'cursor-not-allowed' : ''} ${
        style === 'link'
          ? `rounded-sm font-body text-xl leading-[23px] text-deep-blue-dark visited:text-purple-50a hover:text-blue-hover focus:outline-1 focus:outline-blue-hover active:text-blue-hover`
          : style === 'none'
            ? ''
            : buttonStyle
      } px-3.5 py-1.5 ${className} `}
      onClick={onClick}
      id={id}
      data-gc-analytics-customclick={`${refPageAA}:${id}`}
    >
      {icon && !iconEnd ? (
        <Image
          className="rounded pr-2"
          width={8}
          height={8}
          src={icon}
          alt={iconAltText}
        />
      ) : undefined}
      {text}
      {children}
      {icon && iconEnd ? (
        <div className="grid place-items-center">
          <Image
            className="rounded pb-3 pl-5"
            width={8}
            height={8}
            src={icon}
            alt={iconAltText}
          />
        </div>
      ) : undefined}
    </a>
  )
}
export default Button