All files / src/components/forms ObjPaletteSelect.tsx

0% Statements 0/14
0% Branches 0/16
0% Functions 0/5
0% Lines 0/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 70 71 72 73 74 75 76 77 78 79 80 81 82                                                                                                                                                                   
import PaletteBlock from "components/forms/PaletteBlock";
import React, { FC } from "react";
import { ObjPalette } from "shared/lib/entities/entitiesTypes";
import {
  OptionLabelWithPreview,
  Select,
  SingleValueWithPreview,
} from "ui/form/Select";
import l10n from "shared/lib/lang/l10n";
import { SingleValue } from "react-select";
 
interface ObjPaletteSelectProps {
  name: string;
  value?: ObjPalette;
  onChange?: (newValue: ObjPalette) => void;
}
 
interface ObjPaletteOption {
  value: ObjPalette;
  label: string;
  colors: string[];
}
 
const options: ObjPaletteOption[] = [
  {
    value: "OBP0",
    label: "0: OBP0",
    colors: ["E8F8E0", "B0F088", "", "202850"],
  },
  {
    value: "OBP1",
    label: "1: OBP1",
    colors: ["E8F8E0", "509878", "", "202850"],
  },
];
 
export const ObjPaletteSelect: FC<ObjPaletteSelectProps> = ({
  name,
  value = "OBP0",
  onChange,
}) => {
  const currentValue = options.find((o) => o.value === value);
  return (
    <Select
      name={name}
      value={currentValue}
      options={options}
      onChange={(newValue: SingleValue<ObjPaletteOption>) => {
        Iif (newValue) {
          onChange?.(newValue.value);
        }
      }}
      formatOptionLabel={(option: ObjPaletteOption) => {
        return (
          <OptionLabelWithPreview
            preview={
              <PaletteBlock type="sprite" colors={option.colors} size={20} />
            }
          >
            {l10n("FIELD_PALETTE")} {option.label}
          </OptionLabelWithPreview>
        );
      }}
      components={{
        SingleValue: () => (
          <SingleValueWithPreview
            preview={
              <PaletteBlock
                type="sprite"
                colors={currentValue?.colors || []}
                size={20}
              />
            }
          >
            {l10n("FIELD_PALETTE")} {currentValue?.label}
          </SingleValueWithPreview>
        ),
      }}
    />
  );
};