All files / src/components/forms UnitsSelectButtonInputOverlay.tsx

53.85% Statements 14/26
0% Branches 0/13
0% Functions 0/8
52% Lines 13/25

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 1231x 1x 1x 1x 1x 1x 1x 1x 1x                   1x                             1x                     1x                             1x                                                                                                                              
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 UnitsSelectButtonInputOverlayProps {
  parentValue?: string;
  parentValueOffset?: number;
  value?: UnitType;
  allowedValues?: UnitType[];
  onChange?: (newValue: UnitType) => void;
}
 
const UnitsWrapper = styled.div`
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 1px;
  display: flex;
  align-items: center;
  pointer-events: none;
`;
 
interface HiddenValueProps {
  $offset: number;
}
 
const HiddenValue = styled.div<HiddenValueProps>`
  padding: 2px;
  opacity: 0;
  font-size: ${(props) => props.theme.typography.fontSize};
  padding: 5px;
  padding-right: 5px;
  box-sizing: border-box;
  pointer-events: none;
  margin-right: ${(props) => props.$offset}px;
`;
 
const Units = styled.div`
  background: ${(props) => props.theme.colors.input.background};
  pointer-events: all;
 
  ${StyledButton} {
    opacity: 0.5;
    padding: 1px;
    min-width: 0;
 
    &:hover {
      opacity: 1;
    }
  }
`;
 
export const UnitsSelectButtonInputOverlay = ({
  parentValue,
  parentValueOffset = 0,
  value,
  allowedValues,
  onChange,
}: UnitsSelectButtonInputOverlayProps) => {
  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 (
    <UnitsWrapper>
      <HiddenValue $offset={parentValueOffset}>{parentValue}</HiddenValue>
      <Units>
        {allowedValues ? (
          <DropdownButton
            label={currentValue}
            showArrow={false}
            size="small"
            variant="transparent"
          >
            {allValues.map((item) => (
              <MenuItem
                key={item}
                onClick={() => onChange?.(item)}
                icon={value === item ? <CheckIcon /> : <BlankIcon />}
              >
                {unitTypeNames[item]}
              </MenuItem>
            ))}
          </DropdownButton>
        ) : (
          <Button size="small" variant="transparent">
            {currentValue}
          </Button>
        )}
      </Units>
    </UnitsWrapper>
  );
};