react流程組件React Flow使用


React Flow流程組件用起來還是挺簡單的,能滿足一些基本的流程配置.可以通過自定義配置節點實現不同需求

可以瀏覽一遍基本API在結合官網給的例子,快速開發.官網給了很多例子,很方便

效果圖:
使用:
安裝包 yarn add react-flow-renderer -D

index.tsx
import React from 'react';
import ReactFlow, {
  addEdge,
  MiniMap,
  Controls,
  Background,
  useNodesState,
  useEdgesState,
} from 'react-flow-renderer';

import { nodes as initialNodes, edges as initialEdges } from './initial-elements';

const OverviewFlow = () => {
  const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
  const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
  const onConnect = (params) => setEdges((eds) => addEdge(params, eds));

  return (
    <ReactFlow
      nodes={nodes} // 節點
      edges={edges} // 連接線
      onNodesChange={onNodesChange} // 節點拖拽等改變
      onEdgesChange={onEdgesChange} // 連接線拖拽等改變
      onConnect={onConnect} // 節點直接連接
      fitView // 渲染節點數據
      attributionPosition="top-right" // react-flow的位置,類似水印,可以通過css隱藏
    >
      // 背景圖 可以配置顏色 方格寬度
      <Background color="#aaa" gap={16} />
    </ReactFlow>
  );
};

export default OverviewFlow;
initial-elements.ts節點與連接線數據
import { MarkerType } from 'react-flow-renderer';

export const nodes = [
  {
    id: '1', // id必須
    type: 'input', // 類型: input開始  default默認  output結束 區別在於連接點不一樣
    data: { // 額外的數據
      label: ( // 節點名稱
        <>
          Welcome to <strong>React Flow!</strong>
        </>
      ),
      // value: 5, .... // 可以將其他數據放入
    },
    position: { x: 250, y: 0 }, // 節點位置
  },
  {
    id: '2',
    data: {
      label: (
        <>
          This is a <strong>default node</strong>
        </>
      ),
    },
    position: { x: 100, y: 100 },
  },
  {
    id: '3',
    data: {
      label: (
        <>
          This one has a <strong>custom style</strong>
        </>
      ),
    },
    position: { x: 400, y: 100 },
    style: {
      background: '#D6D5E6',
      color: '#333',
      border: '1px solid #222138',
      width: 180,
    },
  },
  {
    id: '4',
    position: { x: 250, y: 200 },
    data: {
      label: 'Another default node',
    },
  },
  {
    id: '5',
    data: {
      label: 'Node id: 5',
    },
    position: { x: 250, y: 325 },
  },
  {
    id: '6',
    type: 'output',
    data: {
      label: (
        <>
          An <strong>output node</strong>
        </>
      ),
    },
    position: { x: 100, y: 480 },
  },
  {
    id: '7',
    type: 'output',
    data: { label: 'Another output node' },
    position: { x: 400, y: 450 },
  },
];

export const edges = [
  { id: 'e1-2', source: '1', target: '2', label: 'this is an edge label' },
  { id: 'e1-3', source: '1', target: '3' },
  {
    id: 'e3-4', // id必須
    source: '3', // 連接線起始節點id
    target: '4', // 連接線結束節點id
    animated: true, // 連接線是否有動畫
    label: 'animated edge', // 連接線名稱
  },
  {
    id: 'e4-5',
    source: '4',
    target: '5',
    label: 'edge with arrow head',
    markerEnd: { // 連接線尾部的箭頭
      type: MarkerType.ArrowClosed,
    },
  },
  {
    id: 'e5-6',
    source: '5',
    target: '6',
    type: 'smoothstep', // 連接線類型 default straight step smoothstep
    label: 'smooth step edge',
  },
  {
    id: 'e5-7',
    source: '5',
    target: '7',
    type: 'step',
    style: { stroke: '#f6ab6c' }, // 連接線顏色
    label: 'a step edge',
    animated: true,
    labelStyle: { fill: '#f6ab6c', fontWeight: 700 }, // 連接線名稱樣式
  },
];
基本的api
ReactFlow:

 

這樣一個基本的流程圖就實現了
這里是數據的展示,節點的新增和節點的操作可以看這一篇 https://www.cnblogs.com/steamed-twisted-roll/p/16136149.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM