All files / src/sections/PayBuilder/Forms/DateAndLocation/components/Timers AutoCompleteTime.tsx

73.91% Statements 17/23
50% Branches 2/4
61.53% Functions 8/13
71.42% Lines 15/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                    12x 12x 12x 12x   12x     12x   12x       12x                                                                 1x     412x     1x   19x 19x                                                 2x 2x                                                                    
import { Autocomplete, Stack } from "@mui/material";
import { useRef, useState } from "react";
import { generateTimeOptions } from "./utils";
import { GiveInput } from "@shared/GiveInputs/GiveInput";
import { ClockIcon } from "@phosphor-icons/react";
import { useAppTheme } from "@theme/v2/Provider";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
 
export function AutoCompleteTime({ onChange, error, value, name }: any) {
  const theme = useAppTheme();
  const { isMobileView } = useCustomThemeV2();
  const [open, setOpen] = useState(false);
  const inputRef = useRef<HTMLInputElement | null>(null);
 
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
 
  // We do not filter based on input, always return the full list of options
  const filterOptions = (options: any) => options;
 
  const closeMenu = () => {
    setAnchorEl(null);
  };
 
  return (
    <>
      <Autocomplete
        data-testid="auto-complete-timer"
        disableClearable
        freeSolo
        open={open}
        value={value}
        sx={{ width: "100%" }}
        componentsProps={{
          popper: {
            sx: { display: isMobileView ? "none" : "auto" },
            modifiers: [
              {
                name: "offset",
                options: {
                  offset: [0, 10],
                },
              },
            ],
          },
          paper: {
            sx: {
              borderRadius: "16px",
              boxShadow: "none",
              padding: "8",
              overflow: "hidden",
              background: theme.palette.primitive?.neutral[25],
              border: `1px solid #CCCCC9`,
              backdropFilter: "blur(7.5px)",
            },
          },
        }}
        onClose={() => setOpen(false)}
        options={generateTimeOptions()}
        filterOptions={filterOptions}
        getOptionLabel={(option) => option}
        popupIcon={null}
        openOnFocus={false}
        onInputChange={(event, newValue) => onChange(newValue)}
        renderInput={(params) => {
          const { InputProps, ...rest } = params;
          return (
            <GiveInput
              {...rest}
              data-testid={name}
              ref={inputRef}
              error={Boolean(error)}
              helperText={error?.message}
              InputProps={{
                ...InputProps,
                startAdornment: (
                  <Stack sx={{ marginRight: "8px" }}>
                    <ClockIcon size={20} />
                  </Stack>
                ),
              }}
              variant="outlined"
              inputProps={{
                style: {
                  caretColor: "transparent",
                  cursor: "pointer",
                },
                onKeyDown: (e) => e.preventDefault(),
                ...rest.inputProps,
              }}
              onClick={(e: any) => {
                setOpen((prev) => !prev);
                setAnchorEl(e?.currentTarget as HTMLElement);
              }}
              sx={{
                "& .MuiInputBase-root": {
                  cursor: "pointer",
                },
              }}
            />
          );
        }}
      />
      {isMobileView ? (
        <ContextualMenu
          anchorEl={anchorEl as any}
          handleClose={closeMenu}
          options={generateTimeOptions().map((x) => ({
            text: x,
            onClick: () => {
              onChange(x);
              closeMenu();
            },
          }))}
          color="primary"
          texture="solid"
          horizontalOrigin="left"
          menuWidth={200}
          onChange={(v) => {
            console.log(v);
          }}
        />
      ) : null}
    </>
  );
}