import { Handle } from 'react-flow-renderer';
const targetHandleWithValidation = (
<Handle
type="target"
position="left"
isValidConnection={(connection) => connection.source === 'some-id'}
onConnect={(params) => console.log('handle onConnect', params)}
style={{ background: '#fff' }}
/>
);
The handle receives the additional class names
connecting
when the connection line is above the handle and
valid
if the connection is valid. You can find an example which uses these classes
here.
If you need multiple source or target handles you can achieve this by creating a custom node. Normally you just use the id of a node for the source
or target
of an edge. If you have multiple source or target handles you need to pass an id to these handles. These ids can be used by an edge with the sourceHandle
and targetHandle
options, so that you can connect a specific handle. If you have a node with an id = 1
and a handle with an id = a
you can connect this handle by using the node source=1
and the sourceHandle=a
.
The way multiple handles are treated has changed in v7.0.0.
const nodes = [
{ id: '1', type: 'input', data: { label: 'Node with one source handle' } },
{
id: '2',
type: 'multiHandleNode',
data: { label: 'This node contains 2 handles with ids a and b' },
},
];
const elementsV6 = [
...nodes,
{
id: 'e1-2a',
source: '1',
target: '2__a',
},
{
id: 'e1-2a',
source: '1',
target: '2__b',
},
];
const elementsV7 = [
{ id: '1', type: 'input', data: { label: 'Node with one source handle' } },
{
id: '2',
type: 'multiHandleNode',
data: { label: 'This node contains 2 handles with ids a and b' },
},
{
id: 'e1-2a',
source: '1',
target: '2',
targetHandle: 'a',
},
{
id: 'e1-2a',
source: '1',
target: '2',
targetHandle: 'b',
},
];