Advertisement
fr3gu

Untitled

Mar 2nd, 2025
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const [conditions, setConditions] = useState<ConditionDefinition[]>([]);
  2.   const [expressionTree, setExpressionTree] = useState<ExpressionNode>({
  3.     id: uuidv4(),
  4.     type: "GROUP",
  5.     children: [],
  6.   });
  7.  
  8.   // Uppdaterar condition i listan
  9.   const handleUpdateCondition = (id: number, updatedCondition: ConditionDefinition) => {
  10.     setConditions((prev) =>
  11.       prev.map((cond, index) => (index === id ? updatedCondition : cond))
  12.     );
  13.   };
  14.  
  15.   // Lägger till en AND/OR-grupp i trädet
  16.   const handleAddGroup = (parentId: string, type: "AND_GROUP" | "OR_GROUP") => {
  17.     setExpressionTree((prevTree) => addNode(prevTree, parentId, { id: uuidv4(), type, children: [] }));
  18.   };
  19.  
  20.   // Lägger till en ny CONDITION i trädet
  21.   const handleAddCondition = (parentId: string) => {
  22.     const newCondition: ConditionDefinition = {
  23.       typ: "MATH",
  24.       signalNames: "",
  25.       function: "ALL",
  26.       comparisonValue: "",
  27.       mathOperator: "EQUAL",
  28.     };
  29.     const newConditionId = conditions.length;
  30.     setConditions((prev) => [...prev, newCondition]);
  31.  
  32.     setExpressionTree((prevTree) =>
  33.       addNode(prevTree, parentId, { id: uuidv4(), type: "CONDITION", children: [], conditionId: newConditionId })
  34.     );
  35.   };
  36.  
  37.   return (
  38.     <div className="min-h-screen flex flex-col items-center bg-gray-100 p-4">
  39.       <h1 className="text-2xl font-bold mb-4">Expression Tree Editor</h1>
  40.       <ExpressionTree
  41.         node={expressionTree}
  42.         conditions={conditions}
  43.         onUpdateCondition={handleUpdateCondition}
  44.         onAddGroup={handleAddGroup}
  45.         onAddCondition={handleAddCondition}
  46.       />
  47.     </div>
  48.   );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement