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

100% Statements 11/11
100% Branches 8/8
100% Functions 5/5
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 49 50 51 52 53 54 55 56 57 58 59 60 61                                                7771x 7771x     7771x 7771x 7771x 834x   7771x       8130x           184x             2x 2x                  
import { PropertyInfoRead, PropertyValueRead } from "../../../../../../api/apiStore.gen";
import { VariableWithUnit, VariableWithUnitProps } from "./VariableWithUnit";
 
 
 
interface PropertyIndex {
  [key: string]: {
    displayName?: string;
    value?: string | number;
  };
}
 
 
export interface IndexedVariableWithUnitProps {
  property: PropertyInfoRead;
  onUpdateUnit: (unit: string) => void;
  withMSSConnection?: boolean; // should be applied if multi steady state applies
  onUpdateValue: (value: string, id: number) => void;
  deletePropertyFunction?: (property_val_key: string) => void;
}
 
 
 
export function IndexedVariableWithUnit(props: IndexedVariableWithUnitProps) {
  const values = props.property.values; // if values is passed, just process the subset of all values
  if (values.id === undefined) {
    // console.error("No id found for property", props.property,"with values", values , "and indexes", props.indexes,"which means there probably is not the same depth of indexes as values, or some other logic issue.")
  }
  const individualNames = props.property.values?.[0]?.indexedSets.length != 0
  const name = individualNames ? props.property.displayName : null
  const sortedValues = [...values].sort(
    (v1, v2) => v1?.indexedSetNames?.join("").localeCompare(v2?.indexedSetNames?.join(""))
  )
  return (
    <div>
      {name}
      {sortedValues.map((value) => (
        <VariableWithUnit
          key={value.id}
          property={props.property}
          displayName={individualNames ? value.indexedSetNames.join(" ") : props.property.displayName + " " + value.indexedSetNames.join(" ")}
          value={value}
          onUpdateValue={(val) => {
            props.onUpdateValue(val, value.id);
          }}
          onUpdateUnit={props.onUpdateUnit}
          withMSSConnection={props.withMSSConnection}
          // Prevent the delete button from showing up if the deletePropertyFunction is not passed
          deletePropertyFunction={props.deletePropertyFunction ?
            () => {
                if (props.deletePropertyFunction) {
                    props.deletePropertyFunction(value.indexedSets.join(" "));
                }
            }: undefined
          }
 
        />
      ))}
    </div>
  )
}