All files / packages/ui/src/components/ReasonSelector/ReasonItem index.tsx

100% Statements 25/25
100% Branches 22/22
100% Functions 7/7
100% Lines 24/24

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                                                      3x 14x 14x   14x   14x 14x     14x 42x               42x               14x 42x               42x               14x 42x           14x 2x     14x           14x   14x       14x   14x   14x                                                 3x   14x                                                                              
import React, { JSX, memo, useCallback, useEffect } from "react";
import Animated, {
  interpolateColor,
  useAnimatedStyle,
  useSharedValue,
  withTiming,
} from "react-native-reanimated";
import { Pressable, View } from "react-native";
 
import { makeStyles } from "@repo/ui/themes/makeStyles";
import { useTheme } from "@repo/ui/themes/ThemeContext";
import { Theme } from "@repo/ui/themes/types";
import { textStyles } from "@repo/ui/themes/typography";
 
import { TickCircleV2Icon } from "../../../icons";
 
export interface BriefReasonItem {
  label: string;
  icon: JSX.Element;
}
 
interface Props {
  item: BriefReasonItem;
  selected: boolean;
  onPress: (label: string) => void;
}
 
const ReasonItemBase = ({ item, selected, onPress }: Props) => {
  const { theme } = useTheme();
  const styles = useStyles();
 
  const progress = useSharedValue(selected ? 1 : 0);
 
  useEffect(() => {
    progress.value = withTiming(selected ? 1 : 0, { duration: 250 });
  }, [selected, progress]);
 
  const animatedLineStyle = useAnimatedStyle(() => {
    const backgroundColor = interpolateColor(
      progress.value,
      [0, 1],
      [
        theme.isNewTheme ? theme.colors.background.errorMuted : theme.colors.gray100,
        theme.colors.text.info,
      ],
    );
    return { backgroundColor };
  }, [
    theme.colors.background.errorMuted,
    theme.colors.gray100,
    theme.colors.text.info,
    theme.isNewTheme,
  ]);
 
  const animatedLabelStyle = useAnimatedStyle(() => {
    const color = interpolateColor(
      progress.value,
      [0, 1],
      [
        theme.isNewTheme ? theme.colors.text.emphasis : theme.colors.text.primary,
        theme.colors.text.info,
      ],
    );
    return { color };
  }, [
    theme.colors.text.emphasis,
    theme.colors.text.primary,
    theme.colors.text.info,
    theme.isNewTheme,
  ]);
 
  const animatedBadgeStyle = useAnimatedStyle(() => {
    return {
      opacity: progress.value,
      transform: [{ scale: progress.value }],
    };
  }, [progress]);
 
  const handlePress = useCallback(() => {
    onPress(item.label);
  }, [item.label, onPress]);
 
  const iconColor = theme.isNewTheme
    ? selected
      ? theme.colors.text.info
      : theme.colors.background.errorMuted
    : undefined;
 
  const detailColor = theme.isNewTheme && selected ? theme.colors.text.white : undefined;
 
  const renderedIcon = iconColor
    ? React.cloneElement(item.icon, { color: iconColor, detailColor })
    : item.icon;
 
  const labelStyle = [styles.label, theme.isNewTheme && textStyles.content.regular];
 
  const tickColor = theme.isNewTheme ? theme.colors.text.info : undefined;
 
  return (
    <Pressable
      style={styles.container}
      onPress={handlePress}
      accessibilityRole="button"
      accessibilityLabel={`Select reason: ${item.label}`}
      accessibilityState={{ selected }}
    >
      <View>{renderedIcon}</View>
 
      <View style={styles.lineWrapper}>
        <Animated.View style={[styles.line, animatedLineStyle]} />
 
        <Animated.View style={[styles.checkBadge, animatedBadgeStyle]}>
          <TickCircleV2Icon color={tickColor} />
        </Animated.View>
      </View>
 
      <Animated.Text style={[labelStyle, animatedLabelStyle]} numberOfLines={4}>
        {item.label}
      </Animated.Text>
    </Pressable>
  );
};
 
export const ReasonItem = memo(ReasonItemBase);
 
const useStyles = makeStyles((theme: Theme) => ({
  container: {
    alignItems: "center",
    marginRight: 60,
  },
 
  lineWrapper: {
    height: 110,
    justifyContent: "flex-end",
    alignItems: "center",
    position: "relative",
    marginVertical: 6,
  },
 
  line: {
    position: "absolute",
    top: 0,
    bottom: 0,
    width: 1,
    backgroundColor: theme.colors.gray100,
  },
 
  checkBadge: {
    backgroundColor: theme.colors.white,
    position: "absolute",
    bottom: -2,
    alignItems: "center",
    justifyContent: "center",
    borderRadius: 12,
  },
 
  label: {
    textAlign: "center",
    fontSize: 16,
    lineHeight: 22,
    width: 150,
    ...textStyles.content.regular,
  },
}));