All files / packages/ui/src/components/HeaderRequest index.tsx

100% Statements 10/10
93.75% Branches 30/32
66.66% Functions 2/3
100% Lines 9/9

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                                                                                  2x                                         12x 12x 12x 12x 12x 12x   12x                                                                                                                                                                     12x                                                                                                                  
import {
  type DimensionValue,
  Image,
  ImageSourcePropType,
  Pressable,
  type StyleProp,
  Text,
  type TextStyle,
  View,
  type ViewStyle,
} from "react-native";
 
import { makeStyles } from "@repo/ui/themes/makeStyles";
import { useTheme } from "@repo/ui/themes/ThemeContext";
import { textStyles } from "@repo/ui/themes/typography";
 
import { ArrowLeftIcon } from "../../icons/ArrowLeft";
import { CloseIcon } from "../../icons/Close";
 
export interface HeaderRequestProps {
  title: string;
  subTitle?: string;
  description?: string;
  image?: ImageSourcePropType;
  imageWidth?: DimensionValue;
  imageHeight?: DimensionValue;
  containerStyle?: StyleProp<ViewStyle>;
  titleGroupStyle?: StyleProp<ViewStyle>;
  titleStyle?: StyleProp<TextStyle>;
  subTitleStyle?: StyleProp<TextStyle>;
  imageWrapperStyle?: StyleProp<ViewStyle>;
  titleMinimumFontScale?: number;
  isShowBackButton?: boolean;
  isShowCloseButton?: boolean;
  isLoading?: boolean;
  onBackStep?: () => void;
  onClose?: () => void;
  onBack?: () => void;
  disabled?: boolean;
}
 
export const HeaderRequest = ({
  title,
  subTitle,
  description,
  image,
  imageWidth,
  imageHeight,
  containerStyle,
  titleGroupStyle,
  titleStyle,
  subTitleStyle,
  imageWrapperStyle,
  titleMinimumFontScale = 0.7,
  isShowBackButton = false,
  isShowCloseButton = true,
  isLoading = false,
  onBackStep,
  onClose = () => {},
  onBack,
  disabled = false,
}: HeaderRequestProps) => {
  const { theme } = useTheme();
  const styles = useStyles();
  const handleBack = onBackStep || onBack;
  const isBusy = isLoading || disabled;
  const canPressBack = isShowBackButton && !isBusy;
  const canPressClose = isShowCloseButton && !isBusy;
 
  return (
    <View>
      <View style={[styles.container, containerStyle]}>
        <View style={styles.sideSlot}>
          <Pressable
            testID="header-back-button"
            style={[
              styles.icon,
              !isShowBackButton && styles.iconHidden,
              isLoading && styles.iconDisabled,
            ]}
            onPress={handleBack}
            disabled={!canPressBack}
            hitSlop={8}
            accessibilityRole="button"
            accessibilityLabel="Go back"
          >
            <ArrowLeftIcon color={theme.colors.text.heading} />
          </Pressable>
        </View>
        <View style={[styles.titleGroup, titleGroupStyle]}>
          <Text
            style={[styles.title, titleStyle]}
            numberOfLines={1}
            adjustsFontSizeToFit
            minimumFontScale={titleMinimumFontScale}
            accessibilityRole="header"
          >
            {title}
          </Text>
          {subTitle && (
            <Text
              style={[
                styles.subTitle,
                !subTitle && styles.subTitleHidden,
                subTitleStyle,
              ]}
              numberOfLines={1}
            >
              {subTitle}
            </Text>
          )}
        </View>
        <View style={styles.sideSlot}>
          <Pressable
            testID="header-close-button"
            style={[
              styles.icon,
              styles.closeIcon,
              !isShowCloseButton && styles.iconHidden,
              isLoading && styles.iconDisabled,
            ]}
            onPress={onClose}
            disabled={!canPressClose}
            hitSlop={8}
            accessibilityRole="button"
            accessibilityLabel="Close"
          >
            <CloseIcon color={theme.colors.text.heading} />
          </Pressable>
        </View>
      </View>
      {image && (
        <View style={[styles.imageWrapper, imageWrapperStyle]}>
          <Image
            source={image}
            resizeMode="contain"
            style={[{ width: imageWidth, height: imageHeight }]}
            accessible={false}
          />
        </View>
      )}
      {description && (
        <Text
          style={[styles.description, !description && styles.subTitleHidden]}
        >
          {description}
        </Text>
      )}
    </View>
  );
};
 
const useStyles = makeStyles((theme) => ({
  container: {
    flexDirection: "row",
    alignItems: "flex-start",
  },
  sideSlot: {
    width: theme.metrics.spacing[10],
    alignItems: "center",
  },
  icon: {
    width: theme.metrics.spacing[10],
    height: theme.metrics.spacing[10],
    justifyContent: "center",
  },
  closeIcon: {
    alignItems: "flex-end",
  },
  iconHidden: {
    opacity: 0,
  },
  iconDisabled: {
    opacity: theme.metrics.opacity[60],
  },
  titleGroup: {
    flex: 1,
    alignItems: "center",
    minHeight: theme.metrics.spacing[10],
    gap: theme.metrics.spacing[0.5],
  },
  title: {
    color: theme.colors.text.heading,
    ...textStyles.title,
    fontSize: theme.metrics.spacing[8],
    textAlign: "center",
  },
  subTitle: {
    ...textStyles.content.regular,
    color: theme.colors.text.dark,
    fontSize: theme.metrics.textSize[14],
    lineHeight: theme.metrics.textSize[20],
    textAlign: "center",
  },
  description: {
    ...textStyles.content.light,
    color: theme.colors.text.dark,
    fontWeight: theme.metrics.fontWeight.light,
    fontSize: 22,
    lineHeight: 28,
    marginVertical: theme.metrics.spacing[5],
  },
  subTitleHidden: {
    opacity: 0,
  },
  imageWrapper: {
    alignItems: "center",
  },
}));