轻松连接
厌倦了小小的连接句柄吗?让你的整个节点作为一个整体工作!不过请记住,在这种情况下你仍然需要定义单独的拖动句柄,以便能够拖动节点。
🌐 Fed up with tiny little connection handles? Make your whole node act as one! Keep in mind though that you need to define separate drag handles in this case to still be able to drag the node.
import React, { useCallback } from 'react';
import {
Background,
ReactFlow,
addEdge,
useNodesState,
useEdgesState,
MarkerType,
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
import CustomNode from './CustomNode';
import FloatingEdge from './FloatingEdge';
import CustomConnectionLine from './CustomConnectionLine';
const initialNodes = [
{
id: '1',
type: 'custom',
position: { x: 0, y: 0 },
},
{
id: '2',
type: 'custom',
position: { x: 250, y: 320 },
},
{
id: '3',
type: 'custom',
position: { x: 40, y: 300 },
},
{
id: '4',
type: 'custom',
position: { x: 300, y: 0 },
},
];
const initialEdges = [];
const connectionLineStyle = {
stroke: '#b1b1b7',
};
const nodeTypes = {
custom: CustomNode,
};
const edgeTypes = {
floating: FloatingEdge,
};
const defaultEdgeOptions = {
type: 'floating',
markerEnd: {
type: MarkerType.ArrowClosed,
color: '#b1b1b7',
},
};
const EasyConnectExample = () => {
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = useCallback(
(params) => setEdges((eds) => addEdge(params, eds)),
[setEdges],
);
return (
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
fitView
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
defaultEdgeOptions={defaultEdgeOptions}
connectionLineComponent={CustomConnectionLine}
connectionLineStyle={connectionLineStyle}
>
<Background />
</ReactFlow>
);
};
export default EasyConnectExample;Last updated on