feat(react) added getConnectedEdges to useReactFlow

This commit is contained in:
Peter
2023-10-05 12:24:35 +02:00
parent 18a22e6f4c
commit 8e7ee88ca3
2 changed files with 18 additions and 0 deletions

View File

@@ -223,6 +223,21 @@ export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlo
[]
);
const getConnectedEdges = useCallback<Instance.getConnectedEdges>((node) => {
const edges = store.getState().edges;
const nodeIds = new Set();
if (typeof node === 'string') {
nodeIds.add(node);
} else if (node.length >= 1) {
node.forEach((n) => {
nodeIds.add(n.id);
});
}
return edges.filter((edge) => nodeIds.has(edge.source) || nodeIds.has(edge.target));
}, []);
return useMemo(() => {
return {
...viewportHelper,
@@ -238,6 +253,7 @@ export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlo
deleteElements,
getIntersectingNodes,
isNodeIntersecting,
getConnectedEdges,
};
}, [
viewportHelper,
@@ -253,5 +269,6 @@ export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlo
deleteElements,
getIntersectingNodes,
isNodeIntersecting,
getConnectedEdges,
]);
}

View File

@@ -39,6 +39,7 @@ export namespace Instance {
area: Rect,
partially?: boolean
) => boolean;
export type getConnectedEdges = (id: string | (Partial<Node> & { id: Node['id'] })[]) => Edge[];
}
export type ReactFlowInstance<NodeData = any, EdgeData = any> = {