All files / app/components Select.tsx

100% Statements 8/8
80% Branches 4/5
100% Functions 2/2
100% Lines 7/7

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              5x 5x 5x   5x                                                   171x                                                             699x                 177x  
import {
  FormControl,
  InputLabel,
  MenuItem,
  Select as MuiSelect,
  SelectChangeEvent,
  SelectProps,
} from "@mui/material";
import React, { forwardRef } from "react";
import { theme } from "theme";
 
const Select = forwardRef(function Select<
  T extends Record<string | number, string>
>(
  {
    id,
    className,
    native = true,
    menuItems = false,
    optionLabelMap,
    label,
    variant = "outlined",
    options,
    onChange,
    ...otherProps
  }: Omit<SelectProps, "children"> & {
    id: string;
    options: Extract<keyof T, string | number>[];
    value?: T extends undefined
      ? string | number | number[]
      : keyof T | Array<keyof T>;
    menuItems?: boolean;
    optionLabelMap: T;
    onChange?: (event: SelectChangeEvent<T>) => void;
  },
  ref: React.Ref<HTMLSelectElement>
) {
  const OptionComponent: React.ElementType = menuItems ? MenuItem : "option";
 
  return (
    <FormControl
      variant={variant}
      className={className}
      margin="normal"
      sx={{
        "& .MuiOutlinedInput-root": {
          borderRadius: theme.shape.borderRadius * 3,
        },
        "& .MuiInputBase-input": {
          height: "auto",
        },
        display: "block",
      }}
    >
      <InputLabel htmlFor={id}>{label}</InputLabel>
      <MuiSelect
        inputRef={ref}
        variant="standard"
        native={native}
        label={label}
        onChange={onChange}
        {...otherProps}
        inputProps={{
          name: id,
          id,
        }}
      >
        {options.map((option) => (
          <OptionComponent value={option} key={option}>
            {optionLabelMap[option]}
          </OptionComponent>
        ))}
      </MuiSelect>
    </FormControl>
  );
});
 
export default Select;