All files / app/components Select.tsx

100% Statements 10/10
100% Branches 1/1
100% Functions 3/3
100% Lines 9/9

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          5x 5x 5x 5x   20x                       5x                           159x   159x                                 711x                
import {
  FormControl,
  InputLabel,
  Select as MuiSelect,
  SelectProps,
} from "@material-ui/core";
import classnames from "classnames";
import React from "react";
import makeStyles from "utils/makeStyles";
 
const useStyles = makeStyles((theme) => ({
  formControl: {
    "& .MuiOutlinedInput-root": {
      borderRadius: theme.shape.borderRadius * 3,
    },
    "& .MuiInputBase-input": {
      height: "auto",
    },
    display: "block",
  },
}));
 
export default function Select<T extends Record<string | number, string>>({
  id,
  className,
  optionLabelMap,
  label,
  variant = "outlined",
  options,
  ...otherProps
}: Omit<SelectProps, "children"> & {
  id: string;
  options: Extract<keyof T, string | number>[];
  value?: T extends undefined ? string | number : keyof T;
  optionLabelMap: T;
}) {
  const classes = useStyles();
 
  return (
    <FormControl
      variant={variant}
      className={classnames(className, classes.formControl)}
      margin="normal"
    >
      <InputLabel htmlFor={id}>{label}</InputLabel>
      <MuiSelect
        native
        label={label}
        {...otherProps}
        inputProps={{
          name: id,
          id,
        }}
      >
        {options.map((option) => (
          <option value={option} key={option}>
            {optionLabelMap[option]}
          </option>
        ))}
      </MuiSelect>
    </FormControl>
  );
}