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

100% Statements 24/24
100% Branches 24/24
100% Functions 9/9
100% Lines 23/23

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                                2x   2x 107x 87x     20x                     2x             109x 109x   109x 109x     109x 327x         327x     109x 327x         327x     109x               10x                                                                 29x   29x       109x         109x                               29x                                                                                                                      
import { useEffect } from "react";
import Animated, {
  interpolateColor,
  useAnimatedStyle,
  useSharedValue,
  withTiming,
} from "react-native-reanimated";
import { Pressable, Text, View } 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 type { QuickSelectProps, QuickSelectValue } from "./types";
import { isMatchingQuickSelectOption } from "./utils";
 
const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
 
const isDateQuickSelectValue = (value: unknown): value is QuickSelectValue => {
  if (!value || typeof value !== "object") {
    return false;
  }
 
  return "startDate" in value && "endDate" in value && "timeSlot" in value;
};
 
interface QuickSelectOptionItemProps {
  option: { label: string; testID?: string; value: any };
  isActive: boolean;
  disabled: boolean;
  onChange: (value: any) => void;
  styles: any;
}
 
const QuickSelectOptionItem = ({
  option,
  isActive,
  disabled,
  onChange,
  styles,
}: QuickSelectOptionItemProps) => {
  const { theme } = useTheme();
  const progress = useSharedValue(isActive ? 1 : 0);
 
  useEffect(() => {
    progress.value = withTiming(isActive ? 1 : 0, { duration: 200 });
  }, [isActive, progress]);
 
  const animatedButtonStyle = useAnimatedStyle(() => {
    const backgroundColor = interpolateColor(
      progress.value,
      [0, 1],
      [theme.colors.background.tertiary, theme.colors.background.selectedStrong],
    );
    return { backgroundColor };
  }, [theme.colors.background.tertiary, theme.colors.background.selectedStrong, progress]);
 
  const animatedTextStyle = useAnimatedStyle(() => {
    const color = interpolateColor(
      progress.value,
      [0, 1],
      [theme.colors.text.chip, theme.colors.text.white],
    );
    return { color };
  }, [theme.colors.text.chip, theme.colors.text.white, progress]);
 
  return (
    <AnimatedPressable
      testID={option.testID}
      disabled={disabled}
      style={[styles.optionButton, animatedButtonStyle, disabled && styles.optionButtonDisabled]}
      accessibilityRole="button"
      accessibilityLabel={option.label}
      accessibilityState={{ selected: isActive, disabled }}
      onPress={() => onChange(option.value)}
    >
      <Text
        style={styles.optionTextGhost}
        accessibilityElementsHidden
        importantForAccessibility="no-hide-descendants"
      >
        {option.label}
      </Text>
      <View style={styles.optionTextOverlay}>
        <Animated.Text
          style={[
            styles.optionText,
            animatedTextStyle,
            disabled && styles.optionTextDisabled,
            isActive ? { fontWeight: theme.metrics.fontWeight.semiBold } : null,
          ]}
        >
          {option.label}
        </Animated.Text>
      </View>
    </AnimatedPressable>
  );
};
 
export function QuickSelect<TValue = QuickSelectValue>({
  options,
  value,
  onChange,
  disabled = false,
  testID,
  isOptionActive,
}: QuickSelectProps<TValue>) {
  const styles = useStyles();
 
  return (
    <View testID={testID}>
      <View style={styles.optionsContainer}>
        {options.map((option) => {
          const isActive = isOptionActive
            ? isOptionActive(value, option.value)
            : isDateQuickSelectValue(value) && isDateQuickSelectValue(option.value)
              ? isMatchingQuickSelectOption(value, option.value)
              : value === option.value;
          return (
            <QuickSelectOptionItem
              key={option.testID}
              option={option}
              isActive={isActive}
              disabled={disabled}
              onChange={onChange}
              styles={styles}
            />
          );
        })}
      </View>
    </View>
  );
}
 
const useStyles = makeStyles((theme) => ({
  optionsContainer: {
    flexDirection: "row",
    flexWrap: "wrap",
    gap: theme.metrics.spacing[2],
  },
  optionButton: {
    borderRadius: theme.metrics.borderRadius[4],
    minHeight: theme.metrics.spacing[11],
    paddingVertical: theme.metrics.spacing[3.5],
    paddingHorizontal: theme.metrics.spacing[5],
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: theme.colors.background.tertiary,
  },
  optionButtonActive: {
    backgroundColor: theme.colors.background.selectedStrong,
  },
  optionButtonDisabled: {
    backgroundColor: theme.colors.background.disabled,
    opacity: theme.metrics.opacity[60],
  },
  optionText: {
    color: theme.colors.text.chip,
    fontSize: theme.metrics.textSize[16],
    lineHeight: theme.metrics.textSize[24],
    fontWeight: theme.metrics.fontWeight.medium,
    ...textStyles.content.medium,
  },
  optionTextActive: {
    color: theme.colors.text.white,
    fontWeight: theme.metrics.fontWeight.semiBold,
    ...textStyles.content.semiBold,
  },
  optionTextDisabled: {
    color: theme.colors.text.disabled,
  },
  optionTextGhost: {
    fontSize: theme.metrics.textSize[16],
    lineHeight: theme.metrics.textSize[24],
    fontWeight: theme.metrics.fontWeight.semiBold,
    ...textStyles.content.semiBold,
    opacity: 0,
  },
  optionTextOverlay: {
    position: "absolute",
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    justifyContent: "center",
    alignItems: "flex-start",
    // Mirror the button's paddingHorizontal so the text area matches the
    // ghost text content area width (both = button_width - 2 * spacing[5]).
    paddingHorizontal: theme.metrics.spacing[4],
  },
}));
 
export type { QuickSelectOption, QuickSelectProps, QuickSelectValue } from "./types";