All files / app/components Timepicker.tsx

100% Statements 18/18
66.66% Branches 2/3
100% Functions 4/4
100% Lines 16/16

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 1005x 5x 5x 5x 5x 5x                                     68x       68x     5x                         646x 646x 646x 646x                             31x 31x                                                                     646x  
import { TimePicker } from "@mui/x-date-pickers";
import { useTranslation } from "i18n";
import { GLOBAL } from "i18n/namespaces";
import React, { useMemo } from "react";
import { Control, Controller, UseControllerProps } from "react-hook-form";
import { theme } from "theme";
import { Dayjs } from "utils/dayjs";
 
interface TimepickerProps {
  className?: string;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  control: Control<any>;
  defaultValue?: Dayjs | null;
  error: boolean;
  helperText: React.ReactNode;
  id: string;
  rules?: UseControllerProps["rules"];
  label?: string;
  name: string;
  onPostChange?(time: Dayjs | null): void;
  testId?: string;
}
 
function uses24HourClock(locale: string = navigator.language): boolean {
  const formatted = new Intl.DateTimeFormat(locale, {
    hour: "numeric",
    hour12: undefined,
  }).format(new Date(2020, 0, 1, 23, 0));
  return formatted.includes("23");
}
 
const Timepicker = ({
  className,
  control,
  defaultValue,
  error,
  helperText,
  id,
  rules,
  label,
  name,
  onPostChange,
  testId,
}: TimepickerProps) => {
  const { t } = useTranslation([GLOBAL]);
  const locale = navigator.language;
  const is24HourClock = useMemo(() => uses24HourClock(locale), [locale]);
  const format = is24HourClock ? "HH:mm" : "h:mm a";
 
  return (
    <Controller
      control={control}
      defaultValue={defaultValue}
      name={name}
      rules={rules}
      render={({ field }) => (
        <TimePicker
          data-testid={testId}
          {...field}
          label={label}
          value={field.value}
          onChange={(time: Dayjs | null) => {
            field.onChange(time);
            onPostChange?.(time);
          }}
          format={format}
          slotProps={{
            textField: {
              fullWidth: true,
              id,
              error,
              helperText: (
                <span data-testid={`${name}-helper-text`}>{helperText}</span>
              ),
              variant: "standard",
              InputProps: {
                className,
                "aria-label": t("global:change_time"),
              },
              InputLabelProps: { shrink: true },
              sx: {
                "& .MuiOutlinedInput-root": {
                  backgroundColor: theme.palette.primary.main,
                  color: theme.palette.text.primary,
                },
                "& .MuiPaper-root": {
                  backgroundColor: theme.palette.primary.main,
                  color: theme.palette.text.primary,
                },
              },
            },
          }}
        />
      )}
    />
  );
};
 
export default Timepicker;