All files / packages/ui/src/components/DatePicker DatePickerTimeOffSection.tsx

100% Statements 9/9
95.23% Branches 20/21
100% Functions 3/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                                                        4x             79x 79x 79x   79x             158x   158x 158x               24x                                                                  
import { Pressable, Text, View } from "react-native";
 
import { TIME_SLOT_OPTIONS } from "./constants";
import { useStyles } from "./styles";
import type { DatePickerTimeSlot } from "./types";
 
type DatePickerStyles = ReturnType<typeof useStyles>;
 
interface DatePickerTimeOffSlotState {
  disabled: boolean;
  value: DatePickerTimeSlot;
  textForTimeSlot?: string;
  isOptionDisabled: (slot: DatePickerTimeSlot) => boolean;
  onSelect: (slot: DatePickerTimeSlot) => void;
}
 
interface DatePickerTimeOffSummary {
  dateText: string;
  valueText: string;
}
 
interface DatePickerTimeOffSectionProps {
  slotState: DatePickerTimeOffSlotState;
  summary: DatePickerTimeOffSummary;
  styles: DatePickerStyles;
  isShowHalfDay?: boolean;
}
 
export const DatePickerTimeOffSection = ({
  slotState,
  summary,
  styles,
  isShowHalfDay = true,
}: DatePickerTimeOffSectionProps) => {
  const { disabled, value, textForTimeSlot, isOptionDisabled, onSelect } =
    slotState;
  const { dateText, valueText } = summary;
  const normalizedTimeSlotText = textForTimeSlot?.trim();
 
  return (
    <>
      <View style={styles.divider} />
 
      <View style={styles.slotContainer}>
        {isShowHalfDay &&
          TIME_SLOT_OPTIONS.map((slotOption) => {
            const isActive = value === slotOption.value;
            const isDisabledSlot =
              disabled || isOptionDisabled(slotOption.value);
            return (
              <Pressable
                key={slotOption.value}
                style={[
                  styles.slotButton,
                  isActive && !isDisabledSlot && styles.slotButtonActive,
                  isDisabledSlot && styles.slotButtonDisabled,
                ]}
                onPress={() => onSelect(slotOption.value)}
                disabled={isDisabledSlot}
                testID={`time-slot-${slotOption.value}`}
              >
                <Text
                  style={[
                    styles.slotText,
                    isActive && !isDisabledSlot && styles.slotTextActive,
                    isDisabledSlot && styles.slotTextDisabled,
                  ]}
                >
                  {normalizedTimeSlotText
                    ? `${slotOption.label} ${normalizedTimeSlotText}`
                    : slotOption.label}
                </Text>
              </Pressable>
            );
          })}
      </View>
 
      {isShowHalfDay && <View style={styles.divider} />}
 
      <View style={styles.summaryContainer}>
        <Text style={styles.summaryDate} numberOfLines={1}>
          {dateText}
        </Text>
        {isShowHalfDay && (
          <Text style={styles.summaryDaysOff}>{valueText}</Text>
        )}
      </View>
    </>
  );
};