新疆玩石 發表於 2022-4-12 16:57:00

react流程组件React Flow使用

<div>
<div>React Flow流程组件用起来还是挺简单的,能满足一些基本的流程配置.可以通过自定义配置节点实现不同需求</div>
<br>
<div>官网地址: https://reactflow.dev/docs/introduction/ </div>
<div>可以浏览一遍基本API在结合官网给的例子,快速开发.官网给了很多例子,很方便</div>


<br>
<div>效果图:</div>


</div>
<div><img src="https://img2022.cnblogs.com/blog/1349812/202204/1349812-20220412165406051-1482049813.png">
<div>
<div>使用:</div>
<div>安装包 yarn add react-flow-renderer -D</div>

<br>
<div>index.tsx</div>


</div>
<div>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactFlow, {
addEdge,
MiniMap,
Controls,
Background,
useNodesState,
useEdgesState,
} from </span>'react-flow-renderer'<span style="color: rgba(0, 0, 0, 1)">;

import { nodes as initialNodes, edges as initialEdges } from </span>'./initial-elements'<span style="color: rgba(0, 0, 0, 1)">;

const OverviewFlow </span>= () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
const </span>=<span style="color: rgba(0, 0, 0, 1)"> useNodesState(initialNodes);
const </span>=<span style="color: rgba(0, 0, 0, 1)"> useEdgesState(initialEdges);
const onConnect </span>= (params) =&gt; setEdges((eds) =&gt;<span style="color: rgba(0, 0, 0, 1)"> addEdge(params, eds));

</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
    </span>&lt;<span style="color: rgba(0, 0, 0, 1)">ReactFlow
      nodes</span>={nodes} <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 节点</span>
      edges={edges} <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接线</span>
      onNodesChange={onNodesChange} <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 节点拖拽等改变</span>
      onEdgesChange={onEdgesChange} <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接线拖拽等改变</span>
      onConnect={onConnect} <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 节点直接连接</span>
      fitView <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 渲染节点数据</span>
      attributionPosition="top-right" <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> react-flow的位置,类似水印,可以通过css隐藏</span>
    &gt;
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 背景图 可以配置颜色 方格宽度</span>
      &lt;Background color="#aaa" gap={16} /&gt;
    &lt;/ReactFlow&gt;
<span style="color: rgba(0, 0, 0, 1)">);
};

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> OverviewFlow;</pre>
</div>
<div>initial-elements.ts节点与连接线数据</div>
<div>
<div class="cnblogs_code">
<pre>import { MarkerType } from 'react-flow-renderer'<span style="color: rgba(0, 0, 0, 1)">;

export const nodes </span>=<span style="color: rgba(0, 0, 0, 1)"> [
{
    id: </span>'1', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> id必须</span>
    type: 'input', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 类型: input开始default默认output结束 区别在于连接点不一样</span>
    data: { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 额外的数据</span>
      label: ( <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 节点名称</span>
      &lt;&gt;<span style="color: rgba(0, 0, 0, 1)">
          Welcome to </span>&lt;strong&gt;React Flow!&lt;/strong&gt;
      &lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">      ),
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> value: 5, .... // 可以将其他数据放入</span>
<span style="color: rgba(0, 0, 0, 1)">    },
    position: { x: </span>250, y: 0 }, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 节点位置</span>
<span style="color: rgba(0, 0, 0, 1)">},
{
    id: </span>'2'<span style="color: rgba(0, 0, 0, 1)">,
    data: {
      label: (
      </span>&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">
          This is a </span>&lt;strong&gt;<span style="color: rgba(0, 0, 255, 1)">default</span> node&lt;/strong&gt;
      &lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">      ),
    },
    position: { x: </span>100, y: 100<span style="color: rgba(0, 0, 0, 1)"> },
},
{
    id: </span>'3'<span style="color: rgba(0, 0, 0, 1)">,
    data: {
      label: (
      </span>&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">
          This one has a </span>&lt;strong&gt;custom style&lt;/strong&gt;
      &lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">      ),
    },
    position: { x: </span>400, y: 100<span style="color: rgba(0, 0, 0, 1)"> },
    style: {
      background: </span>'#D6D5E6'<span style="color: rgba(0, 0, 0, 1)">,
      color: </span>'#333'<span style="color: rgba(0, 0, 0, 1)">,
      border: </span>'1px solid #222138'<span style="color: rgba(0, 0, 0, 1)">,
      width: </span>180<span style="color: rgba(0, 0, 0, 1)">,
    },
},
{
    id: </span>'4'<span style="color: rgba(0, 0, 0, 1)">,
    position: { x: </span>250, y: 200<span style="color: rgba(0, 0, 0, 1)"> },
    data: {
      label: </span>'Another default node'<span style="color: rgba(0, 0, 0, 1)">,
    },
},
{
    id: </span>'5'<span style="color: rgba(0, 0, 0, 1)">,
    data: {
      label: </span>'Node id: 5'<span style="color: rgba(0, 0, 0, 1)">,
    },
    position: { x: </span>250, y: 325<span style="color: rgba(0, 0, 0, 1)"> },
},
{
    id: </span>'6'<span style="color: rgba(0, 0, 0, 1)">,
    type: </span>'output'<span style="color: rgba(0, 0, 0, 1)">,
    data: {
      label: (
      </span>&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">
          An </span>&lt;strong&gt;output node&lt;/strong&gt;
      &lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">      ),
    },
    position: { x: </span>100, y: 480<span style="color: rgba(0, 0, 0, 1)"> },
},
{
    id: </span>'7'<span style="color: rgba(0, 0, 0, 1)">,
    type: </span>'output'<span style="color: rgba(0, 0, 0, 1)">,
    data: { label: </span>'Another output node'<span style="color: rgba(0, 0, 0, 1)"> },
    position: { x: </span>400, y: 450<span style="color: rgba(0, 0, 0, 1)"> },
},
];

export const edges </span>=<span style="color: rgba(0, 0, 0, 1)"> [
{ id: </span>'e1-2', source: '1', target: '2', label: 'this is an edge label'<span style="color: rgba(0, 0, 0, 1)"> },
{ id: </span>'e1-3', source: '1', target: '3'<span style="color: rgba(0, 0, 0, 1)"> },
{
    id: </span>'e3-4', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> id必须</span>
    source: '3', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接线起始节点id</span>
    target: '4', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接线结束节点id</span>
    animated: <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接线是否有动画</span>
    label: 'animated edge', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接线名称</span>
<span style="color: rgba(0, 0, 0, 1)">},
{
    id: </span>'e4-5'<span style="color: rgba(0, 0, 0, 1)">,
    source: </span>'4'<span style="color: rgba(0, 0, 0, 1)">,
    target: </span>'5'<span style="color: rgba(0, 0, 0, 1)">,
    label: </span>'edge with arrow head'<span style="color: rgba(0, 0, 0, 1)">,
    markerEnd: { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接线尾部的箭头</span>
<span style="color: rgba(0, 0, 0, 1)">      type: MarkerType.ArrowClosed,
    },
},
{
    id: </span>'e5-6'<span style="color: rgba(0, 0, 0, 1)">,
    source: </span>'5'<span style="color: rgba(0, 0, 0, 1)">,
    target: </span>'6'<span style="color: rgba(0, 0, 0, 1)">,
    type: </span>'smoothstep', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接线类型 default straight step smoothstep</span>
    label: 'smooth step edge'<span style="color: rgba(0, 0, 0, 1)">,
},
{
    id: </span>'e5-7'<span style="color: rgba(0, 0, 0, 1)">,
    source: </span>'5'<span style="color: rgba(0, 0, 0, 1)">,
    target: </span>'7'<span style="color: rgba(0, 0, 0, 1)">,
    type: </span>'step'<span style="color: rgba(0, 0, 0, 1)">,
    style: { stroke: </span>'#f6ab6c' }, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接线颜色</span>
    label: 'a step edge'<span style="color: rgba(0, 0, 0, 1)">,
    animated: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
    labelStyle: { fill: </span>'#f6ab6c', fontWeight: 700 }, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接线名称样式</span>
<span style="color: rgba(0, 0, 0, 1)">},
];</span></pre>
</div>
<div>
<div>基本的api</div>
<div>ReactFlow:</div>
<img src="https://img2022.cnblogs.com/blog/1349812/202204/1349812-20220412165609167-943340157.png">
<p>&nbsp;</p>
<div>这样一个基本的流程图就实现了</div>
<div><span style="font-size: 18px">这里是数据的展示,节点的新增和节点的操作可以看这一篇 https://www.cnblogs.com/steamed-twisted-roll/p/16136149.html</span></div>
</div>
</div>
</div>
</div><br><br>
来源:https://www.cnblogs.com/steamed-twisted-roll/p/16135953.html
頁: [1]
查看完整版本: react流程组件React Flow使用