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

100% Statements 21/21
89.28% Branches 25/28
100% Functions 4/4
100% Lines 21/21

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                              4x                                                   93x                               93x   93x 63x 63x 31x     32x 32x 32x     93x 66x 66x 51x     15x 15x 15x     93x   1x           93x                                                                                                                                                                                                   4x                                    
import { forwardRef, useImperativeHandle, useMemo } from "react";
import DateTimePicker from "react-native-ui-datepicker";
import { Text, View } from "react-native";
 
import { useDatePickerController } from "./hooks/useDatePickerController";
import {
  DATE_PICKER_MODE,
  DATE_PICKER_MODE_TYPE,
  DATE_PICKER_TIME_SLOT,
  EMPTY_RANGE,
} from "./constants";
import { DatePickerTimeOffSection } from "./DatePickerTimeOffSection";
import type { DatePickerProps, DatePickerRef } from "./types";
import { normalizeDate } from "./utils";
 
export const DatePicker = forwardRef<DatePickerRef, DatePickerProps>(
  (
    {
      mode = DATE_PICKER_MODE.RANGE,
      modeType = DATE_PICKER_MODE_TYPE.DATE_ONLY,
      label = "",
      value = null,
      range = EMPTY_RANGE,
      rangeStartTimeSlot,
      rangeEndTimeSlot,
      activeRangeSelection,
      onConfirm,
      timeSlot = DATE_PICKER_TIME_SLOT.ALL_DAY,
      textForTimeSlot,
      disableStyleTimeSlot = false,
      onTimeSlotChange,
      disabled = false,
      errorMessage,
      disabledDates,
      minDate,
      maxDate,
      testID,
      datePickerHeight,
    },
    ref,
  ) => {
    const controller = useDatePickerController({
      mode,
      modeType,
      value,
      range,
      rangeStartTimeSlot,
      rangeEndTimeSlot,
      activeRangeSelection,
      timeSlot,
      textForTimeSlot,
      disableStyleTimeSlot,
      disabledDates,
      onConfirm,
      onTimeSlotChange,
    });
    const { actions, calendar, state, styles, summary, timeOff, ui } =
      controller;
 
    const calendarStartDate = useMemo(() => {
      const normalizedDate = normalizeDate(state.tempRange.startDate);
      if (!normalizedDate) {
        return undefined;
      }
 
      const nextDate = new Date(normalizedDate);
      nextDate.setHours(0, 0, 0, 0);
      return nextDate;
    }, [state.tempRange.startDate]);
 
    const calendarEndDate = useMemo(() => {
      const normalizedDate = normalizeDate(state.tempRange.endDate);
      if (!normalizedDate) {
        return undefined;
      }
 
      const nextDate = new Date(normalizedDate);
      nextDate.setHours(0, 0, 0, 0);
      return nextDate;
    }, [state.tempRange.endDate]);
 
    useImperativeHandle(
      ref,
      () => ({
        reset: actions.reset,
      }),
      [actions.reset],
    );
 
    return (
      <View style={styles.container}>
        {label ? <Text style={styles.label}>{label}</Text> : null}
 
        <View
          style={[styles.card, disabled && styles.cardDisabled]}
          testID={testID}
          pointerEvents={disabled ? "none" : "auto"}
        >
          {mode === DATE_PICKER_MODE.SINGLE ? (
            <DateTimePicker
              mode={DATE_PICKER_MODE.SINGLE}
              showOutsideDays
              date={state.tempDate || undefined}
              minDate={minDate}
              maxDate={maxDate}
              disabledDates={calendar.disabledDates.single}
              disableMonthPicker
              disableYearPicker
              weekdaysFormat="short"
              monthCaptionFormat="full"
              navigationPosition="right"
              components={calendar.components}
              styles={{
                ...calendar.baseStyles,
                today: styles.calendarToday,
                selected: styles.calendarSelected,
                selected_label: styles.calendarSelectedLabel,
              }}
              containerHeight={datePickerHeight}
              onChange={actions.handleSingleChange}
            />
          ) : (
            <DateTimePicker
              mode={DATE_PICKER_MODE.RANGE}
              showOutsideDays
              startDate={calendarStartDate}
              endDate={calendarEndDate}
              minDate={minDate}
              maxDate={maxDate}
              disabledDates={calendar.disabledDates.range}
              disableMonthPicker
              disableYearPicker
              weekdaysFormat="short"
              monthCaptionFormat="full"
              navigationPosition="right"
              components={calendar.components}
              styles={{
                ...calendar.baseStyles,
                selected: styles.calendarSelected,
                selected_label: styles.calendarSelectedLabel,
                range_fill: styles.calendarRangeFill,
                range_fill_weekstart: styles.calendarRangeFillWeekstart,
                range_fill_weekend: styles.calendarRangeFillWeekend,
                range_start:
                  state.selectedDays <= 1
                    ? styles.calendarSelected
                    : styles.calendarRangeStart,
                range_start_label: styles.calendarRangeSelectedLabel,
                range_middle: styles.calendarRangeMiddle,
                range_middle_label: styles.calendarRangeSelectedLabel,
                range_end: styles.calendarRangeEnd,
                range_end_label: styles.calendarRangeSelectedLabel,
                today: styles.calendarToday,
              }}
              containerHeight={datePickerHeight}
              onChange={actions.handleRangeChange}
            />
          )}
 
          {ui.showTimeOffSection ? (
            <DatePickerTimeOffSection
              styles={styles}
              slotState={{
                disabled,
                value: state.tempTimeSlot,
                textForTimeSlot,
                isOptionDisabled: timeOff.isOptionDisabled,
                onSelect: actions.handleSelectTimeSlot,
              }}
              summary={{
                dateText: summary.dateText,
                valueText: summary.valueText,
              }}
            />
          ) : null}
        </View>
 
        {errorMessage ? (
          <Text style={styles.errorMessage} accessibilityLiveRegion="polite">
            {errorMessage}
          </Text>
        ) : null}
      </View>
    );
  },
);
 
DatePicker.displayName = "DatePicker";
 
export {
  DATE_PICKER_MODE,
  DATE_PICKER_MODE_TYPE,
  DATE_PICKER_NO_DATE_SELECTED,
  DATE_PICKER_RANGE_SELECTION,
  DATE_PICKER_TIME_SLOT,
} from "./constants";
 
export type {
  DatePickerConfirmMeta,
  DatePickerModeType,
  DateRangeValue,
  DatePickerProps,
  DatePickerRef,
  DatePickerTimeSlot,
} from "./types";