All files / src/components/forms AnimationTypeSelect.tsx

0% Statements 0/22
0% Branches 0/9
0% Functions 0/4
0% Lines 0/21

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                                                                                                                                                                                             
import React, { FC, useEffect, useMemo, useState } from "react";
import { SpriteAnimationType } from "shared/lib/entities/entitiesTypes";
import { OptGroup, Option, Select } from "ui/form/Select";
import l10n from "shared/lib/lang/l10n";
import { SingleValue } from "react-select";
 
interface AnimationTypeSelectProps {
  name: string;
  value?: SpriteAnimationType;
  onChange?: (newValue: SpriteAnimationType) => void;
}
 
interface AnimationTypeOption extends Option {
  value: SpriteAnimationType;
}
 
interface AnimationTypeOptGroup extends OptGroup {
  options: AnimationTypeOption[];
}
 
export const AnimationTypeSelect: FC<AnimationTypeSelectProps> = ({
  name,
  value = "fixed",
  onChange,
}) => {
  const [currentValue, setCurrentValue] = useState<AnimationTypeOption>();
 
  const options: AnimationTypeOptGroup[] = useMemo(
    () => [
      {
        label: l10n("FIELD_FIXED_DIRECTION"),
        options: [
          { label: l10n("FIELD_FIXED_DIRECTION"), value: "fixed" },
          {
            label: l10n("FIELD_FIXED_DIRECTION_MOVEMENT"),
            value: "fixed_movement",
          },
        ],
      },
      {
        label: l10n("FIELD_MULTI_DIRECTION"),
        options: [
          { label: l10n("FIELD_MULTI_DIRECTION"), value: "multi" },
          {
            label: l10n("FIELD_MULTI_DIRECTION_MOVEMENT"),
            value: "multi_movement",
          },
        ],
      },
      {
        label: l10n("GAMETYPE_PLATFORMER"),
        options: [
          { label: l10n("FIELD_PLATFORMER_PLAYER"), value: "platform_player" },
        ],
      },
      {
        label: l10n("GAMETYPE_POINT_N_CLICK"),
        options: [{ label: l10n("FIELD_CURSOR"), value: "cursor" }],
      },
    ],
    []
  );
 
  useEffect(() => {
    Iif (value) {
      let found = false;
      for (const group of options) {
        for (const option of group.options) {
          Iif (option.value === value) {
            setCurrentValue(option);
            found = true;
            break;
          }
        }
        Iif (found) {
          break;
        }
      }
    }
  }, [options, value]);
 
  return (
    <Select
      name={name}
      value={currentValue}
      options={options}
      onChange={(newValue: SingleValue<AnimationTypeOption>) => {
        Iif (newValue) {
          onChange?.(newValue.value);
        }
      }}
    />
  );
};