All files / client/src/components Dropdown.tsx

83.33% Statements 10/12
88.88% Branches 8/9
100% Functions 6/6
83.33% Lines 10/12

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                                      356x               756x     756x                       1609x                       1x 756x 756x     756x     1x   1046x        
import { ReactElement, ChangeEvent } from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
 
export interface Option {
  disabled?: boolean;
  value: string;
  title: string;
}
 
interface Props {
  id?: string;
  onChange: (event: ChangeEvent<HTMLSelectElement>) => void;
  selectedValue?: string;
  options: Option[];
  loading?: boolean;
  className?: string;
}
 
export const Dropdown = ({
  id,
  onChange,
  selectedValue,
  options,
  loading = false,
  className,
}: Props): ReactElement => {
  const { t } = useTranslation();
 
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  Iif (!options) {
    return <div />;
  }
 
  return (
    <StyledSelect
      className={className}
      id={id}
      onChange={onChange}
      value={selectedValue}
    >
      {options.map((item) => (
        <StyledOption
          disabled={item.disabled ?? false}
          value={item.value}
          key={item.value}
        >
          {loading ? t("loading") : item.title}
        </StyledOption>
      ))}
    </StyledSelect>
  );
};
 
const StyledSelect = styled.select`
  border: 1px solid ${(props) => props.theme.borderInactive};
  font-size: ${(props) => props.theme.fontSizeNormal};
  padding: 6px;
  border-radius: 6px;
  background-color: ${(props) => props.theme.backgroundMain};
`;
 
const StyledOption = styled.option<{ disabled: boolean }>`
  background-color: ${(props) =>
    props.disabled
      ? props.theme.backgroundDisabled
      : props.theme.backgroundMain};
`;