All files / atoms/Badge/mobile Badge.native.tsx

97.5% Statements 39/40
98.07% Branches 51/52
100% Functions 8/8
97.5% Lines 39/40

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              2x                               17x   17x 17x   7x   1x   1x   1x   6x   1x       17x   17x 17x   14x   1x   2x       17x 17x   14x   1x   2x       17x 17x 2x     15x 6x     9x       9x     17x 16x   12x   2x   2x       17x                             17x 17x                             17x 1x                             17x    
import { Pressable, StyleProp, Text, View, ViewStyle } from 'react-native';
import { BadgeNativeProps } from './Badge.native.types';
import { cn } from '@sb/libs';
import { Tooltip } from '@sb/ui/components/atoms/Tooltip/mobile/Tooltip.native';
import { Host } from 'react-native-portalize';
import { useThemeColors } from '@sb/hooks/Utilities/useThemeColors';
 
export const Badge = ({
  id,
  icon,
  text,
  circular,
  iconValue,
  onPress,
  pill,
  size = 'small',
  tooltipContent,
  color = 'primary',
  variant = 'solid',
  classNames,
  textClassNames,
  accessibilityLabel,
}: BadgeNativeProps) => {
  const { themedColors } = useThemeColors();
 
  const getColorCode = () => {
    switch (color) {
      case 'primary':
        return themedColors.colorTertiary;
      case 'secondary':
        return themedColors.colorSecondary;
      case 'danger':
        return themedColors.colorStatus_Error;
      case 'success':
        return themedColors.colorStatus_Success;
      case 'warning':
        return themedColors.colorAccent;
      default:
        return color;
    }
  };
 
  const colorCode = getColorCode();
 
  const getVerticalSize = () => {
    switch (size) {
      case 'small':
        return 4;
      case 'medium':
        return 8;
      case 'large':
        return 12;
    }
  };
 
  const getHorizontalSize = () => {
    switch (size) {
      case 'small':
        return 8;
      case 'medium':
        return 16;
      case 'large':
        return 24;
    }
  };
 
  const getTextColor = () => {
    if (variant === 'ghost' || variant === 'outline') {
      return colorCode;
    }
 
    if (variant === 'solid' && color === 'warning') {
      return themedColors.colorText_Tertiary;
    }
 
    Iif (color === 'warning') {
      return themedColors.colorText_Main;
    }
 
    return themedColors.colorText_Main;
  };
 
  const getCircularWidthHeight = () => {
    switch (size) {
      case 'small':
        return 28;
      case 'medium':
        return 50;
      case 'large':
        return 70;
    }
  };
 
  const badgeStyle: StyleProp<ViewStyle> = {
    paddingHorizontal: getHorizontalSize(),
    paddingVertical: getVerticalSize(),
    borderRadius: circular ? 9999 : pill ? 12 : 0,
    width: circular ? getCircularWidthHeight() : 'auto',
    height: circular ? getCircularWidthHeight() : 'auto',
    alignItems: 'center',
    justifyContent: 'center',
    marginBottom: tooltipContent ? 4 : 0,
    flexDirection: icon && iconValue ? 'row' : undefined,
    backgroundColor: variant === 'solid' ? colorCode : 'transparent',
    borderColor: variant === 'outline' ? colorCode : undefined,
    borderWidth: variant === 'outline' ? 1 : undefined,
  };
 
  const renderBadge = () => (
    <Pressable onPress={onPress} accessibilityLabel={accessibilityLabel}>
      <View id={id} testID={id} style={badgeStyle} className={classNames}>
        {icon && iconValue}
        <Text
          style={{
            color: getTextColor(),
          }}
          className={cn('font-Bold', textClassNames)}
        >
          {text}
        </Text>
      </View>
    </Pressable>
  );
 
  const renderBadgeWithTooltip = () => (
    <View>
      <Host>
        <Tooltip
          id="badge-tooltip"
          content={tooltipContent}
          className={"mt-8"}
          trigger="click"
          maxWidth={40}
        >
          {renderBadge()}
        </Tooltip>
      </Host>
    </View>
  )
 
  return tooltipContent === undefined ? renderBadge() : renderBadgeWithTooltip();
};