All files / src/components/forms MovementSpeedSelect.tsx

0% Statements 0/29
0% Branches 0/47
0% Functions 0/9
0% Lines 0/28

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 123 124 125 126 127 128                                                                                                                                                                                                                                                               
import React, { FC, useEffect, useMemo, useState } from "react";
import { SingleValue } from "react-select";
import l10n from "shared/lib/lang/l10n";
import { Input } from "ui/form/Input";
import { OptionLabelWithInfo, Select } from "ui/form/Select";
 
interface MovementSpeedSelectProps {
  name: string;
  value?: number;
  allowNone?: boolean;
  noneLabel?: string;
  onChange?: (newValue: number) => void;
}
 
interface MovementSpeedOption {
  value: number | undefined;
  label: string;
}
 
export const MovementSpeedSelect: FC<MovementSpeedSelectProps> = ({
  name,
  value = 1,
  allowNone,
  noneLabel,
  onChange,
}) => {
  const [currentValue, setCurrentValue] =
    useState<MovementSpeedOption | undefined>();
  const [{ isCustom, autoFocus }, setIsCustom] = useState({
    isCustom: false,
    autoFocus: false,
  });
 
  const options: MovementSpeedOption[] = useMemo(
    () => [
      { value: 0.25, label: `${l10n("FIELD_SPEED")} ¼` },
      { value: 0.5, label: `${l10n("FIELD_SPEED")} ½` },
      { value: 1, label: `${l10n("FIELD_SPEED")} 1` },
      { value: 2, label: `${l10n("FIELD_SPEED")} 2` },
      { value: 3, label: `${l10n("FIELD_SPEED")} 3` },
      { value: 4, label: `${l10n("FIELD_SPEED")} 4` },
      { value: undefined, label: `${l10n("FIELD_CUSTOM_SPEED")}` },
    ],
    []
  );
 
  const optionsWithNone: MovementSpeedOption[] = useMemo(
    () => [
      { value: 0, label: noneLabel ?? `${l10n("FIELD_NONE")}` },
      ...options,
    ],
    [noneLabel, options]
  );
 
  useEffect(() => {
    const current = (allowNone ? optionsWithNone : options).find(
      (o) => o.value === value
    );
    setCurrentValue(current);
    Iif (value === undefined || !current) {
      setIsCustom({ isCustom: true, autoFocus: false });
    }
  }, [allowNone, options, optionsWithNone, value]);
 
  Iif (isCustom) {
    return (
      <Input
        autoFocus={autoFocus}
        type="number"
        id={name}
        name={name}
        value={value || ""}
        min={0}
        max={10}
        step={1 / 16}
        placeholder={`${l10n("FIELD_PIXELS_PER_FRAME")}...`}
        onChange={(e) =>
          onChange?.(Math.min(10, Math.max(0, Number(e.currentTarget.value))))
        }
        onBlur={(e) => {
          Iif (!e.currentTarget.value) {
            onChange?.(1);
            setIsCustom({ isCustom: false, autoFocus: false });
          }
        }}
      />
    );
  }
  return (
    <Select
      name={name}
      value={currentValue}
      options={allowNone ? optionsWithNone : options}
      formatOptionLabel={(
        option: MovementSpeedOption,
        { context }: { context: "menu" | "value" }
      ) => {
        return (
          <OptionLabelWithInfo
            info={
              context === "menu" && option.value && option.value > 0
                ? `${String(Math.round(option.value * 100) / 100)} ${l10n(
                    "FIELD_PIXELS_PER_FRAME_SHORT"
                  )}`
                : ""
            }
          >
            {option.label}{" "}
            {option.value === 0.25 && context === "menu"
              ? `(${l10n("FIELD_SLOWER")})`
              : ""}
            {option.value === 4 && context === "menu"
              ? `(${l10n("FIELD_FASTER")})`
              : ""}
          </OptionLabelWithInfo>
        );
      }}
      onChange={(newValue: SingleValue<MovementSpeedOption>) => {
        if (newValue?.value !== undefined) {
          onChange?.(newValue.value);
        } else {
          setIsCustom({ isCustom: true, autoFocus: true });
        }
      }}
    />
  );
};