Skip to content

Commit cecf9d4

Browse files
committed
feat(core,composables): add useNodesData composable
1 parent c443af6 commit cecf9d4

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

packages/core/src/composables/useHandleConnections.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ interface UseHandleConnectionsParams {
1616
}
1717

1818
/**
19-
* Hook to check if a <Handle /> is connected to another <Handle /> and get the connections.
19+
* Composable that returns existing connections of a handle
2020
*
2121
* @public
22-
* @param param.type - handle type 'source' or 'target'
23-
* @param param.nodeId - node id - if not provided, the node id from the NodeIdContext is used
24-
* @param param.id - the handle id (this is only needed if the node has multiple handles of the same type)
25-
* @param param.onConnect - gets called when a connection is established
26-
* @param param.onDisconnect - gets called when a connection is removed
27-
* @returns an array with connections
22+
* @param UseHandleConnectionsParams
23+
* @param UseHandleConnectionsParams.type - handle type `source` or `target`
24+
* @param UseHandleConnectionsParams.nodeId - node id - if not provided, the node id from the `useNodeId` (meaning, the context-based injection) is used
25+
* @param UseHandleConnectionsParams.id - the handle id (this is required if the node has multiple handles of the same type)
26+
* @param UseHandleConnectionsParams.onConnect - gets called when a connection is created
27+
* @param UseHandleConnectionsParams.onDisconnect - gets called when a connection is removed
28+
* @returns An array of connections
2829
*/
2930
export function useHandleConnections({
3031
type,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { computed } from 'vue'
2+
import type { GraphNode, Node } from '../types'
3+
import { useVueFlow } from './useVueFlow'
4+
5+
/**
6+
* Composable for receiving data of one or multiple nodes
7+
*
8+
* @public
9+
* @param nodeId - The id (or ids) of the node to get the data from
10+
* @param guard - Optional guard function to narrow down the node type
11+
* @returns An array of data objects
12+
*/
13+
export function useNodesData<NodeType extends Node = GraphNode>(nodeId: string): NodeType['data'] | null
14+
export function useNodesData<NodeType extends Node = GraphNode>(nodeIds: string[]): NodeType['data'][]
15+
export function useNodesData<NodeType extends Node = GraphNode>(
16+
nodeIds: string[],
17+
guard: (node: Node) => node is NodeType,
18+
): NodeType['data'][]
19+
export function useNodesData(nodeIds: any): any {
20+
const { findNode } = useVueFlow()
21+
22+
return computed({
23+
get() {
24+
if (!Array.isArray(nodeIds)) {
25+
return findNode(nodeIds)?.data || null
26+
}
27+
28+
const data = []
29+
30+
for (const nodeId of nodeIds) {
31+
const nodeData = findNode(nodeId)?.data
32+
33+
if (nodeData) {
34+
data.push(nodeData)
35+
}
36+
}
37+
38+
return data
39+
},
40+
set() {
41+
console.warn('You are trying to set node data via useNodesData. This is not supported.')
42+
},
43+
})
44+
}

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export { useGetPointerPosition } from './composables/useGetPointerPosition'
7171
export { useNodeId } from './composables/useNodeId'
7272
export { useConnection } from './composables/useConnection'
7373
export { useHandleConnections } from './composables/useHandleConnections'
74+
export { useNodesData } from './composables/useNodesData'
7475

7576
export { VueFlowError, ErrorCode } from './utils/errors'
7677

0 commit comments

Comments
 (0)