拖放时删除边缘
在此示例中,我们展示了如何使用 onReconnect、onReconnectStart 和 onReconnectEnd 处理程序 删除边。如果你将现有边缘拖放到窗格上,它将从 edges 数组中删除。
¥In this example we are showing how to delete an edge by using the onReconnect, onReconnectStart and onReconnectEnd handlers. If you drag an existing edge and drop it on the pane, it gets deleted from the edges array.
import React, { useCallback, useRef } from 'react';
import {
Background,
ReactFlow,
useNodesState,
useEdgesState,
Controls,
reconnectEdge,
addEdge,
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
const initialNodes = [
{
id: '1',
type: 'input',
data: { label: 'Node A' },
position: { x: 250, y: 0 },
},
{
id: '2',
data: { label: 'Node B' },
position: { x: 100, y: 200 },
},
{
id: '3',
data: { label: 'Node C' },
position: { x: 350, y: 200 },
},
];
const initialEdges = [
{ id: 'e1-2', source: '1', target: '2', label: 'reconnectable edge' },
];
const DeleteEdgeDrop = () => {
const edgeReconnectSuccessful = useRef(true);
const [nodes, , onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = useCallback(
(params) => setEdges((els) => addEdge(params, els)),
[],
);
const onReconnectStart = useCallback(() => {
edgeReconnectSuccessful.current = false;
}, []);
const onReconnect = useCallback((oldEdge, newConnection) => {
edgeReconnectSuccessful.current = true;
setEdges((els) => reconnectEdge(oldEdge, newConnection, els));
}, []);
const onReconnectEnd = useCallback((_, edge) => {
if (!edgeReconnectSuccessful.current) {
setEdges((eds) => eds.filter((e) => e.id !== edge.id));
}
edgeReconnectSuccessful.current = true;
}, []);
return (
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
snapToGrid
onReconnect={onReconnect}
onReconnectStart={onReconnectStart}
onReconnectEnd={onReconnectEnd}
onConnect={onConnect}
fitView
attributionPosition="top-right"
>
<Controls />
<Background />
</ReactFlow>
);
};
export default DeleteEdgeDrop;Last updated on