当前位置:   article > 正文

React工作流_react-flow-renderer

react-flow-renderer

技术选型

领导提到要写个工作流,我们的技术栈是React,翻阅了文档之后在 jsPlumb.js 和 react-flow-renderer 中选择,因为 react-flow-renderer 相对简单,也能达到预期效果,所以选择react-flow-renderer

效果

在这里插入图片描述
在这里插入图片描述

数据格式

因为节点是后端返回的数据列表,可能会达到上千个,所以没有用拖拽的方式,选择用导入的方式。
数据格式为:

[
    {
      data: {id: "BPvugVEM1", label: "名字1", remark: "", edit: ""},
      id: "BPvugVEM1",
      type: "relation",
      position: {x: 0, y: 0},
    },
    {
      data: {id: "BPvugVEM2", label: "名字2", remark: "", edit: ""},
      id: "BPvugVEM2",
      type: "relation",
      position: {x: 0, y: 60},
    },
    {
      data: {id: "BPvugVEM3", label: "名字3", remark: "", edit: ""},
      id: "BPvugVEM3",
      type: "relation",
      position: {x: 0, y: 120},
    }
  ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

当然,这肯定是拿到后端的数据之后做的二次处理。

API

既然使用了新的东西,肯定还是要研究一下,文档又是英文的,所以针对常用的api做了简单整理。

  1. node节点

    <ReactFlow elements={rightSource} nodeTypes={nodeTypes} onNodeDoubleClick={HandleNode}>
                <Controls />    
          </ReactFlow>
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    (1) node节点的相关属性,主要是在nodes对象里面去设置

			id: nodeId,  // 节点id
                data: {
                    label: nodeName,  // 要传入节点的内容
                },
                position: {
                    x: num%2 == 0 ? 25 + 200 * (n - 1 - remender) : 25 + 200 * remender, y: 75 * num
                },  //节点在视图中的位置
                className: status_list[t.status],  // 节点样式,这个很常用
                sourcePosition: num%2 == 0 ? 'left' : 'right',  // 入口位置
                targetPosition: num%2 == 0 ? 'right' : 'left',  //出口位置
                type: 'customnode' //节点类型
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(2)那么如果原始节点不符合产品的要求要做拓展,比如在右上角加上节点id,怎么办?
这里就要用到自定义样式

const CustomNode = ({ id, data, sourcePosition, targetPosition }) => (
      <div className="handle-nodes">
        <Handle type="target" position={targetPosition}/>
        <div style={{position: 'absolute', top: '0', left: '2px', fontSize: '12px'}}>{id}</div>
        <div style={{textAlign: 'center', height: '40px', lineHeight: '38px'}}>{data.label}</div>
        <Handle type="source" position={sourcePosition}/>
      </div>
    );
    const nodeTypes = {
      customnode: CustomNode
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

这里的data就是nodes中定义的节点内容

  1. edges 连线
    除了节点之外最重要的就是连线了
				id: uuidv4(),
                source: t.from,
                target: t.to,
                type: 'smoothstep', //指定连线的类型,常用的 默认为实线,smoothstep为虚线
                arrowHeadType:"arrow", //连线带 箭头
                borderRadius: 10, // 连线的弧度
                label: 'updatable edge', //连线中的文字提示,类似 ——阿巴—— 
                animated: true, //开启动画,当为虚线是常开启动画
                style: {strokeWidth: 2} //这里可以调节连线的粗细
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  1. ReactFlow props API
onNodeDoubleClick双击节点
	onNodeClick单击节点
	onNodeDragStart
	onNodeDrag
	onNodeDragStop
	onNodeMouseEnter
	onNodeMouseMove
	onNodeMouseLeave
	onNodeContextMenu
	onNodesDelete
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
参考文档 https://juejin.cn/post/7058483266533195807#heading-10
参考文档 https://reactflow.dev/docs/api/node-types/
  • 1
  • 2

工作流顶级巨坑

要注意的事情是

{ id: '1', data: { label: '-' }, position: { x: 100, y: 100 } },
  { id: '2', data: { label: 'Node 2' }, position: { x: 100, y: 200 } },
  { id: 'e1-2', source: '1', target: '2' },
  • 1
  • 2
  • 3

这个id一定要用字符串,不然会出bug,血泪史啊 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

还要注意一点!
edges 里面的 source: String(t.from), target: String(t.to),
也要用字符串啊 不然更新了线就没了啊 啊啊啊啊啊

如果你要保存工作流,怎么获取工作流的所有信息呢,包括节点位置哦。

这一行代码就能获取所有信息啦

console.log(reactFlowInstance.toObject()) //工作流所有信息
  • 1

reactFlowInstance 是谁呢?
是画布实例啦
// 画布加载完毕,保存当前画布实例
const onLoad = (instance) => setReactFlowInstance(instance);
这样就把画布上所有的信息都保存到reactFlowInstance啦,当然setReactFlowInstance是reducer,reactFlowInstance是state

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/864481
推荐阅读
相关标签
  

闽ICP备14008679号