All files / src/components/forms UnitsSelectLabelButton.tsx

0% Statements 0/21
0% Branches 0/12
0% Functions 0/5
0% Lines 0/20

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                                                                                                                                                                                         
import l10n from "shared/lib/lang/l10n";
import React, { useMemo } from "react";
import { UnitType, unitTypes } from "shared/lib/entities/entitiesTypes";
import styled from "styled-components";
import { Button } from "ui/buttons/Button";
import { DropdownButton } from "ui/buttons/DropdownButton";
import { CheckIcon, BlankIcon } from "ui/icons/Icons";
import { MenuItem } from "ui/menu/Menu";
import { StyledButton } from "ui/buttons/style";
 
interface UnitSelectLabelButtonProps {
  value?: UnitType;
  allowedValues?: UnitType[];
  onChange?: (newValue: UnitType) => void;
}
 
const Units = styled.div`
  display: inline-flex;
  pointer-events: all;
  margin: -6px 3px;
 
  ${StyledButton} {
    opacity: 0.5;
    padding: 1px;
    min-width: 0;
    height: 18px;
 
    &:hover {
      opacity: 1;
    }
  }
`;
 
export const UnitSelectLabelButton = ({
  value,
  allowedValues,
  onChange,
}: UnitSelectLabelButtonProps) => {
  const unitTypeNames: Record<UnitType, string> = useMemo(
    () => ({
      tiles: l10n("FIELD_TILES"),
      pixels: l10n("FIELD_PIXELS"),
      time: l10n("FIELD_SECONDS"),
      frames: l10n("FIELD_FRAMES"),
      "8px": "8px",
      "16px": "16px",
    }),
    []
  );
 
  const unitTypeButtonNames: Record<UnitType, string> = useMemo(
    () => ({
      tiles: l10n("FIELD_TILES").toLocaleLowerCase(),
      pixels: l10n("FIELD_PIXELS_SHORT").toLocaleLowerCase(),
      time: l10n("FIELD_SECONDS").toLocaleLowerCase(),
      frames: l10n("FIELD_FRAMES").toLocaleLowerCase(),
      "8px": "8px",
      "16px": "16px",
    }),
    []
  );
 
  const allValues = allowedValues ? allowedValues : unitTypes;
  const currentValue = value && unitTypeButtonNames[value];
  return (
    <Units>
      {allowedValues ? (
        <DropdownButton
          label={currentValue}
          showArrow={false}
          size="small"
          variant="transparent"
          type="button"
        >
          {allValues.map((item) => (
            <MenuItem
              key={item}
              onClick={() => onChange?.(item)}
              icon={value === item ? <CheckIcon /> : <BlankIcon />}
            >
              {unitTypeNames[item]}
            </MenuItem>
          ))}
        </DropdownButton>
      ) : (
        <Button size="small" variant="transparent" type="button">
          {currentValue}
        </Button>
      )}
    </Units>
  );
};