All files / src/form/cells EnumCell.tsx

100% Statements 20/20
83.33% Branches 10/12
100% Functions 4/4
100% Lines 19/19

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          66x     66x                         66x 9x 9x   9x 7x 4x 8x 4x 4x   7x 7x 5x 3x 3x   2x     2x           9x            
import React, { ReactElement, useEffect, useState } from "react";
import { EnumCellProps, EnumMetaProps } from "@props/RecordProps";
import { Tag } from "antd";
import { getEnumLabelFromCache } from "@utils/EnumUtils";
 
const DefaultDisplayVal = '';
 
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const colors = [
  "#E6F1FF", //(light blue)
  "#F5F5F5", // (light grey)
  "#F4F4F4", // (light grey with a hint of beige)
  "#FFF3E0", // (light orange)
  "#F1F8E9", // (light green)
  "#F2F2F2", // (light off-white)
  "#FCF3CF", // (light yellow)
  "#EEEFF2", // (light grey-blue)
  "#F9EBEA", // (light pink)
  "#F2F2F2", //(light grey with a hint of blue)
];
 
const EnumCell = (props: EnumCellProps): ReactElement => {
  const { value, field, mappings, type } = props;
  const [displayVal, setDisplayVal] = useState<string>(DefaultDisplayVal);
 
  useEffect(() => {
    const setLabel = (mapping: Array<EnumMetaProps>): void => {
      const enumName = (typeof value === 'string') ? value : value?.name;
      const enumMetaProps = mapping?.filter(e => e.value === enumName);
      const label = (enumMetaProps != null && enumMetaProps.length === 1) ? enumMetaProps[0].label : "";
      setDisplayVal(label);
    };
    const mapping = mappings[field];
    if (mapping == null) {
      if (type == null) {
        console.warn(`Mapping for ${field}/${value} not found in mappings: `, mappings);
        setDisplayVal("");
      } else {
        getEnumLabelFromCache(type).then(setLabel);
      }
    } else {
      setLabel(mapping);
    }
  }, [value, type, field, mappings, displayVal]);
 
  //const color = (displayVal != null && displayVal.length > 0) ? colors[displayVal.length % colors.length] : undefined;
 
  return (displayVal === "") ? <></> : (<div className="enum-value">
    <Tag className="enum-tag">{displayVal}</Tag>
  </div>);
};
 
export default EnumCell;