Hug*_*hes 5 drag-and-drop nodes reactjs react-tsx
我正在使用反应流创建节点图。每个节点的上方和下方都会出现一些小点来创建新的边。这些边缘的选择和放置区域的像素精度非常高,以至于用户很难链接项目。有什么办法可以增加连接区域吗?我希望用户能够将边缘拖动到节点上的任何位置,它将两者链接在一起。
import ReactFlow, { removeElements, addEdge, isNode, Background, Elements, BackgroundVariant, FlowElement, Node, Edge, Connection, OnLoadParams } from 'react-flow-renderer';
const onNodeDragStop = (_: MouseEvent, node: Node) => console.log('drag stop', node);
const onElementClick = (_: MouseEvent, element: FlowElement) => console.log('click', element);
const initialElements: Elements = [
{ id: '1', type: 'input', data: { label: 'Node 1' }, position: { x: 250, y: 5 }, className: 'light' },
{ id: '2', data: { label: 'Node 2' }, position: { x: 100, y: 100 }, className: 'light' },
{ id: '3', data: { label: 'Node 3' }, position: { x: 400, y: 100 }, className: 'light' },
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 200 }, className: 'light' },
{ id: 'e1-2', source: '1', target: '2', animated: true },
];
const BasicFlow = () =>
{
const [rfInstance, setRfInstance] = useState<OnLoadParams | null>(null);
const [elements, setElements] = useState<Elements>(initialElements);
const onElementsRemove = (elementsToRemove: Elements) => setElements((els) => removeElements(elementsToRemove, els));
const onConnect = (params: Edge | Connection) => setElements((els) => addEdge(params, els));
const onLoad = (reactFlowInstance: OnLoadParams) => setRfInstance(reactFlowInstance);
return (
<ReactFlow
elements={elements}
onLoad={onLoad}
onElementClick={onElementClick}
onElementsRemove={onElementsRemove}
onConnect={onConnect}
onNodeDragStop={onNodeDragStop}
>
<Background variant={BackgroundVariant.Lines} />
</ReactFlow>
);
};
export default BasicFlow;```
Run Code Online (Sandbox Code Playgroud)
小智 3
我这样做是传递一个带有自己的句柄的自定义节点:
const NODE_TYPES = {
yourType: CustomNode,
};
...
<ReactFlow
nodeTypes={NODE_TYPES}
...
/>
Run Code Online (Sandbox Code Playgroud)
然后,在 处CustomNode,我使用了Handle具有自定义高度和宽度的组件:
import { Handle, Position } from 'react-flow-renderer';
const CustomNode = (...) => {
...
return <Box>
...
<Handle
type="target"
position={Position.Left}
style={{ // Make the handle invisible and increase the touch area
background: 'transparent',
zIndex: 999,
border: 'none',
width: '20px',
height: '20px',
}}
/>
<CircleIcon
style={{}} // Fix the position of the icon over here
/>
</Box>;
}
Run Code Online (Sandbox Code Playgroud)
我认为这有点老套,但这就是我找到的实现它的方法。
| 归档时间: |
|
| 查看次数: |
3908 次 |
| 最近记录: |