All files / atoms/Input/mobile/Components/Molecules InputTooltip.tsx

7.69% Statements 1/13
3.44% Branches 1/29
0% Functions 0/3
7.69% Lines 1/13

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                                                                                                                                                                                                                                        2x                                                                    
import React, { useEffect, useState } from 'react';
import { Platform, StyleProp, StyleSheet, Text, View, ViewStyle } from 'react-native';
import { TooltipPointerDirection } from '../../../../Tooltip/index.native';
import { cn } from '@sb/libs';
 
type InputTooltipProps = {
  id: string;
  children?: React.ReactNode;
  message: string | string[] | undefined;
  bgColor?: string;
  isVisible?: boolean;
  body?: (value: string | string[]) => React.ReactNode;
  pointerDirection?: TooltipPointerDirection;
  className?: string;
  style?: StyleProp<ViewStyle>;
};
 
function InputTooltip({
  children,
  message,
  body,
  bgColor,
  isVisible = false,
  pointerDirection = 'left',
  className,
  style,
}: InputTooltipProps) {
  const [visible, setVisible] = useState(isVisible);
 
  useEffect(() => {
    setVisible(isVisible);
  }, [isVisible]);
 
  if (!message || !visible) {
    return <>{children}</>;
  }
 
  const getArrowStyle = () => {
    const baseStyle = {
      position: 'absolute' as const,
      width: 0,
      height: 0,
      borderLeftWidth: 6,
      borderRightWidth: 6,
      borderTopWidth: 6,
      borderLeftColor: 'transparent' as const,
      borderRightColor: 'transparent' as const,
      borderTopColor: bgColor || '#333',
    };
 
    switch (pointerDirection) {
      case 'left':
        return {
          ...baseStyle,
          top: -6,
          left: 20,
          marginLeft: -6,
          transform: [{ rotate: '180deg' }],
        };
      case 'right':
        return {
          ...baseStyle,
          top: -6,
          right: 20,
          marginRight: -6,
          transform: [{ rotate: '180deg' }],
        };
      case 'center':
      default:
        return {
          ...baseStyle,
          top: -6,
          left: 50,
          marginLeft: -6,
          transform: [{ rotate: '180deg' }],
        };
    }
  };
 
  return (
    <View style={styles.container} pointerEvents="box-none">
      {children}
 
      <View
        style={[
          styles.tooltipContainer,
          { backgroundColor: bgColor || '#333' },
          !visible && !message && { display: 'none' },
          pointerDirection === 'right' && { right: 0 },
          pointerDirection === 'left' && { left: 0 },
          pointerDirection === 'center' && { left: '50%', transform: [{ translateX: -50 }] },
          style,
        ]}
        pointerEvents="none"
        accessible={false}
        accessibilityElementsHidden={true}
        importantForAccessibility="no-hide-descendants"
      >
        <View style={getArrowStyle()} pointerEvents="none" />
        <View style={styles.tooltipContent} pointerEvents="none">
          {body ? (
            body(message)
          ) : (
            <Text
              style={[styles.tooltipText]}
              className={cn('text-normal text-left text-white', className)}
            >
              {Array.isArray(message) ? message.join(', ') : message}
            </Text>
          )}
        </View>
      </View>
    </View>
  );
}
 
const styles = StyleSheet.create({
  container: {
    position: 'relative',
    overflow: 'visible',
  },
  tooltipContainer: {
    position: 'absolute',
    zIndex: Platform.OS === 'ios' ? 9999 : 1000,
    top: '100%',
    marginTop: 8,
    borderRadius: 1,
    paddingHorizontal: 10,
    paddingVertical: 4,
    shadowColor: '#000',
    shadowOffset: {
      width: 0,
      height: 2,
    },
    shadowOpacity: 0.25,
    shadowRadius: 3.84,
    elevation: 5,
  },
  tooltipContent: {
    alignItems: 'flex-start',
    justifyContent: 'flex-start',
  },
  tooltipText: {
    fontSize: 12,
    textAlign: 'left',
    lineHeight: 16,
  },
});
 
export default InputTooltip;