All files / src/components/forms EngineFieldSelect.tsx

0% Statements 0/25
0% Branches 0/5
0% Functions 0/8
0% Lines 0/23

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                                                                                                                                     
import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import { useAppSelector } from "store/hooks";
import { Select, Option, OptGroup } from "ui/form/Select";
import l10n, { L10NKey } from "shared/lib/lang/l10n";
import { useGroupedEngineFields } from "components/settings/useGroupedEngineFields";
import { EngineFieldSchema } from "store/features/engine/engineState";
import { SingleValue } from "react-select";
 
interface EngineFieldSelectProps {
  name: string;
  value?: string;
  onChange?: (newValue: string) => void;
}
 
const notDefine = (engineField: EngineFieldSchema) =>
  engineField.cType !== "define";
 
const EngineFieldSelect: React.FC<EngineFieldSelectProps> = ({
  name,
  value,
  onChange,
}) => {
  const groupedFields = useGroupedEngineFields();
  const engineFields = useAppSelector((state) => state.engine.fields);
  const [options, setOptions] = useState<OptGroup[]>([]);
 
  useEffect(() => {
    setOptions(
      groupedFields.map((g) => ({
        label: l10n(g.name as L10NKey),
        options: g.fields.filter(notDefine).map((f) => ({
          value: f.key,
          label: l10n(f.label as L10NKey),
        })),
      }))
    );
  }, [groupedFields]);
 
  const currentField = engineFields.find((f) => f.key === value);
 
  const currentValue = currentField && {
    value: currentField.key,
    label: l10n(currentField.label as L10NKey),
  };
 
  return (
    <Select
      name={name}
      value={currentValue}
      options={options}
      onChange={(e: SingleValue<Option>) => {
        Iif (e && onChange) {
          onChange(e.value);
        }
      }}
    />
  );
};
 
EngineFieldSelect.propTypes = {
  value: PropTypes.string,
  onChange: PropTypes.func,
};
 
export default EngineFieldSelect;