All files / molecules/Accordion/mobile/Components/Atoms Header.tsx

77.77% Statements 21/27
58.62% Branches 17/29
100% Functions 5/5
76.92% Lines 20/26

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 152 153 154 155 156 157 158 159 160 161 162 163                                  20x 20x 20x   20x 20x 13x   7x 7x     20x 20x                       20x       20x   20x             20x     20x 20x                       20x       20x                                                     2x   2x                                                                                                                    
// Header.tsx
import React, { ReactNode } from 'react';
import { Pressable, StyleSheet, Text, View, ViewStyle } from 'react-native';
import { useAccordion } from '../Context/AccordionContext';
import { ToggleIcon } from './ToggleIcon';
import { cn } from '@sb/libs';
import { AccordionNativeVariant } from '../../../Accordion.types';
import { extractTextClasses, getIndexFromId } from '../Utils';
 
type HeaderProps = {
  id: string;
  label: string | ReactNode;
  className?: string;
};
 
export function Header({ id, label, className }: HeaderProps) {
  const { toggleItem, isItemOpen, variant, icon, headerPosition, isFlush, isDisabled } =
    useAccordion();
  const { icon: IconDefault, activeIcon: IconActive, iconPosition } = icon;
  const isVertical = headerPosition === 'top' || headerPosition === 'bottom';
 
  const renderIcon = () => {
    if (!IconDefault && !IconActive) {
      return <ToggleIcon id={id} />;
    }
    const currentIcon = isItemOpen(id) ? IconActive : IconDefault;
    return <View style={styles.iconContainer}>{currentIcon}</View>;
  };
 
  const renderLabel = () => {
    Iif (typeof label === 'string' && label !== '') {
      return (
        <Text
          allowFontScaling={false}
          numberOfLines={1}
          className={cn('text-text-main font-Bold', extractTextClasses(className))}
          style={styles.label}
        >
          {label}
        </Text>
      );
    } else {
      return <View style={styles.label}>{label}</View>;
    }
  };
 
  const handlePress = () => toggleItem(id);
 
  const variantStrategy: Record<AccordionNativeVariant, string> = {
    primary: 'bg-primary-background',
    secondary: 'bg-primary',
    none: '',
    stripped: getIndexFromId(id) % 2 === 0 ? 'bg-tertiary-background' : 'bg-primary',
  };
 
  const variantClass = variantStrategy[variant];
 
  // Determine layout based on icon position
  const getLayoutStyle = (): ViewStyle => {
    switch (iconPosition) {
      case 'left':
        return styles.iconLeft;
      case 'right':
        return styles.iconRight;
      case 'top':
        return styles.iconTop;
      case 'bottom':
        return styles.iconBottom;
      case 'center':
        return styles.iconCenter;
      default:
        return styles.iconRight; // Default to right
    }
  };
 
  return (
    <Pressable
      disabled={isDisabled}
      style={[
        styles.container,
        getLayoutStyle(),
        (headerPosition === 'right' || headerPosition === 'left') && styles.fitContent,
        !isVertical && { width: 'auto' },
        isDisabled && { opacity: 0.7 },
      ]}
      className={cn(
        variantClass,
        'px-2 py-3',
        {
          'p-0': isFlush,
        },
        className
      )}
      onPress={handlePress}
    >
      {label && renderLabel()}
 
      {renderIcon()}
    </Pressable>
  );
}
 
Header.displayName = 'Header';
 
const styles = StyleSheet.create({
  container: {
    width: '100%',
    justifyContent: 'space-between',
  },
  // Icon positioning styles
  iconLeft: {
    flexDirection: 'row-reverse',
    alignItems: 'center',
  },
  iconRight: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  iconTop: {
    flexDirection: 'column-reverse',
    alignItems: 'center',
  },
  iconBottom: {
    flexDirection: 'column',
    alignItems: 'center',
  },
  iconCenter: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
  },
  iconContainer: {
    alignItems: 'center',
    justifyContent: 'center',
  },
  // Legacy styles (keeping for compatibility)
  left: { flexDirection: 'row-reverse' },
  right: { flexDirection: 'row' },
  horizontalTop: {
    flexDirection: 'column-reverse',
  },
  horizontalBottom: {
    flexDirection: 'column',
  },
  verticalTop: {
    flexDirection: 'column-reverse',
  },
  verticalBottom: {
    flexDirection: 'column',
  },
  center: {},
  label: {
    flex: 1,
  },
  labelRow: {
    flex: 1,
    width: '100%',
  },
  fitContent: {
    width: 'auto',
  },
});