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

100% Statements 11/11
100% Branches 2/2
100% Functions 3/3
100% Lines 11/11

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                          6285x 6285x 6285x   6285x   32x           6285x 10x     6285x 32x     6253x     52810x                      
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,
}: {
  property: PropertyInfoRead;
  onUpdate: (unit: string) => void;
}) {
  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}
      ariaLabel={`${property.displayName}-unit`}
    />
  );
}