From 88c9372bb93ecdbe64c853db7da40abee7347534 Mon Sep 17 00:00:00 2001 From: Felipe Emos Date: Sat, 13 Jan 2024 00:35:26 -0300 Subject: [PATCH] fix: typesafety of Node and Edge changes On some functions and type definitions related to Change Events, the generics for the types Node and Edge were being lost. This commit fixes those: - Functions - applyNodeChanges - applyEdgeChanges - Types - NodeChange - EdgeChange - OnNodesChange - OnEdgesChange --- packages/react/src/types/changes.ts | 13 +++++++++---- packages/react/src/types/general.ts | 4 ++-- packages/react/src/utils/changes.ts | 10 ++++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/react/src/types/changes.ts b/packages/react/src/types/changes.ts index 3dfdffa2..9a5e8fe5 100644 --- a/packages/react/src/types/changes.ts +++ b/packages/react/src/types/changes.ts @@ -44,13 +44,13 @@ export type NodeReplaceChange = { * Union type of all possible node changes. * @public */ -export type NodeChange = +export type NodeChange = | NodeDimensionChange | NodePositionChange | NodeSelectionChange | NodeRemoveChange - | NodeAddChange - | NodeReplaceChange; + | NodeAddChange + | NodeReplaceChange; export type EdgeSelectionChange = NodeSelectionChange; export type EdgeRemoveChange = NodeRemoveChange; @@ -64,4 +64,9 @@ export type EdgeReplaceChange = { item: EdgeType; type: 'replace'; }; -export type EdgeChange = EdgeSelectionChange | EdgeRemoveChange | EdgeAddChange | EdgeReplaceChange; + +export type EdgeChange = + | EdgeSelectionChange + | EdgeRemoveChange + | EdgeAddChange + | EdgeReplaceChange; diff --git a/packages/react/src/types/general.ts b/packages/react/src/types/general.ts index a1d76983..82cb23ce 100644 --- a/packages/react/src/types/general.ts +++ b/packages/react/src/types/general.ts @@ -16,8 +16,8 @@ import { import type { NodeChange, EdgeChange, Node, Edge, ReactFlowInstance, EdgeProps } from '.'; import { ComponentType } from 'react'; -export type OnNodesChange = (changes: NodeChange[]) => void; -export type OnEdgesChange = (changes: EdgeChange[]) => void; +export type OnNodesChange = (changes: NodeChange[]) => void; +export type OnEdgesChange = (changes: EdgeChange[]) => void; export type OnNodesDelete = (nodes: Node[]) => void; export type OnEdgesDelete = (edges: Edge[]) => void; diff --git a/packages/react/src/utils/changes.ts b/packages/react/src/utils/changes.ts index cd148a6e..91a7a004 100644 --- a/packages/react/src/utils/changes.ts +++ b/packages/react/src/utils/changes.ts @@ -188,7 +188,10 @@ function applyChange(change: any, element: any, elements: any[] = []): any { ); */ -export function applyNodeChanges(changes: NodeChange[], nodes: NodeType[]): NodeType[] { +export function applyNodeChanges( + changes: NodeChange[], + nodes: NodeType[] +): NodeType[] { return applyChanges(changes, nodes) as NodeType[]; } @@ -212,7 +215,10 @@ export function applyNodeChanges(changes: NodeChan ); */ -export function applyEdgeChanges(changes: EdgeChange[], edges: EdgeType[]): EdgeType[] { +export function applyEdgeChanges( + changes: EdgeChange[], + edges: EdgeType[] +): EdgeType[] { return applyChanges(changes, edges) as EdgeType[]; }