All files / src/sections/PayBuilder/Forms/DateAndLocation/components ControlledDatePicker.tsx

53.57% Statements 15/28
68.75% Branches 33/48
57.14% Functions 4/7
58.33% Lines 14/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                                                        114x                             800x 800x   800x     800x                     921x                                                                                   87x 87x 46x   46x 46x 46x 46x                           1040x                                                    
import DatePicker, { validatedDate } from "@common/DatePickers/DatePicker";
import { Grid } from "@mui/material";
import { LocalizationProvider } from "@mui/x-date-pickers";
import { AdapterDateFns } from "@mui/x-date-pickers/AdapterDateFns";
import { AdapterMoment } from "@mui/x-date-pickers/AdapterMoment";
import { GiveTextField } from "@shared/GiveInputs/GiveInput";
import { Controller, useFormContext } from "react-hook-form";
import { DatePickerToolbar } from "@mui/x-date-pickers/DatePicker/DatePickerToolbar";
import { Picker } from "@shared/DatePicker/Picker";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import moment, { Moment } from "moment";
 
type Props = {
  name: string;
  label?: string;
  onChangeCustom?: any;
  disabled?: boolean;
  disablePast?: boolean;
  disableFuture?: boolean;
  minDate?: Date;
  maxDate?: Date;
  defaultPickerValue?: Date;
  hidePlaceholder?: boolean;
  useUTCMoment?: boolean;
  placeholder?: string;
  isValidateForm?: boolean;
};
 
export const ControlledDatePicker = ({
  name,
  label,
  onChangeCustom,
  disableFuture,
  disablePast,
  disabled,
  minDate,
  maxDate,
  defaultPickerValue,
  hidePlaceholder = true,
  useUTCMoment = false,
  placeholder,
  isValidateForm,
}: Props) => {
  const { isMobileView } = useCustomThemeV2();
  const { control, trigger, setValue } = useFormContext();
  const _minDate =
    minDate ||
    (disablePast ? new Date().setDate(new Date().getDate() + 1) : null);
 
  return (
    <LocalizationProvider
      dateAdapter={useUTCMoment ? AdapterMoment : AdapterDateFns}
    >
      <Controller
        name={name}
        control={control}
        render={({
          field: { ref, onChange, ...rest },
          fieldState: { error, invalid },
        }) =>
          isMobileView ? (
            <Picker
              label={label}
              minDate={useUTCMoment && _minDate ? moment(_minDate) : _minDate}
              maxDate={useUTCMoment && maxDate ? moment(maxDate) : maxDate}
              field={{
                ref,
                onChange: (value: Moment, keyboardInputValue?: string) => {
                  const isOnMountTrigger = keyboardInputValue === "initial";
                  if (isOnMountTrigger) return;
                  if (!useUTCMoment) onChange(validatedDate({ value }));
                  const formValue =
                    useUTCMoment && value?.toDate ? value.toDate() : value; //we are still expecting the dates accross our app to be returned as JS Date, not moment. to be able to use as moment, we need to change all the place
                  setValue(name, formValue, { shouldDirty: true });
                  isValidateForm ? trigger() : trigger(name);
                  if (onChangeCustom) {
                    onChangeCustom(formValue);
                  }
                },
                ...rest,
              }}
              defaultValue={defaultPickerValue}
              error={error}
              disablePast={disablePast}
              disableFuture={disableFuture}
              disabled={disabled}
              name={name}
              handleClickAccept={(data: any) => {
                //we need to pass down a callback otherwise mobile picker dosent work
              }}
              hidePlaceholder={hidePlaceholder && !placeholder}
              ToolbarComponent={(props: any) => (
                <DatePickerToolbar {...props} toolbarTitle="" />
              )}
              placeholder={placeholder}
              useUTCMoment={useUTCMoment}
            />
          ) : (
            <DatePicker
              {...rest}
              defaultValue={defaultPickerValue}
              onChange={(value: Moment, keyboardInputValue?: string) => {
                const isOnMountTrigger = keyboardInputValue === "initial";
                if (isOnMountTrigger) return;
                Iif (!useUTCMoment) onChange(validatedDate({ value }));
                const formValue =
                  useUTCMoment && value ? value?.toDate() : value; //we are still expecting the dates accross our app to be returned as JS Date, not moment. to be able to use as moment, we need to change all the place
                setValue(name, formValue, { shouldDirty: true });
                isValidateForm ? trigger() : trigger(name);
                Iif (onChangeCustom) {
                  onChangeCustom(formValue);
                }
              }}
              minDate={_minDate}
              maxDate={maxDate}
              disabled={disabled}
              disablePast={disablePast}
              disableFuture={disableFuture}
              inputRef={ref}
              inputFormat={useUTCMoment ? "MM/DD/YYYY" : "MM/dd/yyyy"}
              adornmentPosition="start"
              popperPlacement="bottom-start"
              renderInput={({ inputProps, ...params }) => (
                <Grid container>
                  <GiveTextField
                    {...params}
                    inputRef={ref}
                    fullWidth
                    name={name}
                    error={Boolean(error)}
                    helperText={error?.message}
                    inputProps={{
                      ...inputProps,
                      placeholder: placeholder,
                    }}
                    label={label}
                    hidePlaceholder={hidePlaceholder && !placeholder}
                  />
                </Grid>
              )}
              useGMT={false}
              useUTCMoment={useUTCMoment}
            />
          )
        }
      />
    </LocalizationProvider>
  );
};