All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/PropertyPanel/InputFields UnitsDropdown.tsx

100% Statements 8/8
100% Branches 1/1
100% Functions 1/1
100% Lines 8/8

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                                15462x 15462x 15462x       27x             17x       27x     15435x     136022x                        
import SelectInput from "@/ahuora-design-system/inputs/SelectInput";
import { PropertyInfoRead } from "@/api/apiStore.gen";
import {
  GetUnitDisplay,
  GetUnitSet,
} from "../../../../../../data/UnitsLibrary";
 
export function UnitsDropdown({
  property,
  onUpdate,
  disabled = false,
}: {
  property: PropertyInfoRead;
  onUpdate: (unit: string) => void;
  disabled?: boolean;
}) {
  const unitType = property.unitType;
  const unitSet = GetUnitSet(unitType);
  const unit = GetUnitDisplay(unitSet, property.unit!);
 
  if (unitSet.length === 0) {
    // add the current unit as the default unit
    unitSet.push({
      label: unit,
      value: unit,
    });
  }
 
  const handleUnitChange = (unit: string) => {
    onUpdate(unit);
  };
 
  if (unitType === "dimensionless") {
    return null;
  }
 
  return (
    <SelectInput
      data={unitSet.map((unit) => {
        return {
          label: unit.label,
          value: unit.value,
        };
      })}
      value={unit}
      handleChange={handleUnitChange}
      disabled={disabled}
      ariaLabel={`${property.displayName}-unit`}
    />
  );
}