3.6.5 • Published 3 years ago

@mhpsantos/colored-flowchart-react v3.6.5

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Flowchart React

Lightweight flowchart & flowchart designer for React.js

NPM JavaScript Style Guide

English | 中文

Install

npm install --save flowchart-react
# or
yarn add flowchart-react

Usage

import React, { useState } from "react";
import Flowchart from "flowchart-react";
import { ConnectionData, NodeData } from "flowchart-react/dist/schema";

const App = () => {
  const [nodes, setNodes] = useState<NodeData[]>([
    {
      type: "start",
      title: "Start",
      x: 150,
      y: 190,
      id: 1,
      bgColor: "lightgreen",
    },
    {
      type: "end",
      title: "End",
      x: 500,
      y: 190,
      id: 2,
      bgColor: "lightblue",
    },
    {
      x: 330,
      y: 190,
      id: 3,
      title: "Matheus",
      type: "operation",
      bgColor: "lightpink",
    },
  ]);
  const [conns, setConns] = useState<ConnectionData[]>([
    {
      source: { id: 1, position: "right" },
      destination: { id: 3, position: "left" },
      color: "purple",
    },
    {
      source: { id: 3, position: "right" },
      destination: { id: 2, position: "left" },
      color: "orange",
    },
  ]);

  return (
    <Flowchart
      onChange={(nodes, connections) => {
        setNodes(nodes);
        setConns(connections);
      }}
      style={{ width: 800, height: 600 }}
      nodes={nodes}
      connections={conns}
    />
  );
};

export default App;

Demo

API

Props

nodes: NodeData[]

Array of nodes.

NodeData
PropsDescriptionTypeDefaultRequired
idIdentitynumbertrue
titleTitle of nodestring, (node: NodeData) => stringtrue
typeType of nodestart, end, operation, decisiontrue
xX axisnumbertrue
yY axisnumbertrue
payloadCustom data{[key: string]: unknown}false
widthNode widthnumber120false
heightNode heightnumber60false
bgColorNode background colorstringwhitefalse

connections: ConnectionData[]

Connections between nodes.

ConnectionData
PropsDescriptionTypeDefaultRequired
typeType of connectionsuccess, failfalse
sourceSource info{id: number, position: 'left', 'right', 'top', 'bottom'}true
destinationDestination info{id: number, position: 'left', 'right', 'top', 'bottom'}true
colorConnection colorAny RGB/HEX colorblackfalse

readonly: boolean | undefined

Prop to disabled drag, connect and delete action.

style: React.CSSProperties

Style of background svg.

defaultNodeSize: {width: number, height: number} | undefined

Default: { width: 120, height: 60 }.

showToolbar: boolean | undefined

false to hide toolbar.

Events

onChange: (nodes: NodeData[], connections: ConnectionData[]) => void

Triggered when a node is deleted(click a node and press delete), moved, disconnected(click a connection and press delete) or connected.

onNodeDoubleClick: (node: NodeData) => void

Triggered when a node is double-clicked.

Tip: Double-click to edit.

onDoubleClick: (event: React.MouseEvent<SVGGElement, MouseEvent>, zoom: number) => void

Triggered when the background svg is double-clicked.

Tip: Double-click to create a node.

function handleDoubleClick(event: React.MouseEvent<SVGGElement, MouseEvent>, zoom: number): void {
  const point = {
    x: event.nativeEvent.offsetX / zoom,
    y: event.nativeEvent.offsetY / zoom,
    id: +new Date(),
  };
  let nodeData: NodeData;
  if (!nodes.find((item) => item.type === "start")) {
    nodeData = {
      type: "start",
      title: "Start",
      ...point,
    };
  } else if (!nodes.find((item) => item.type === "end")) {
    nodeData = {
      type: "end",
      title: "End",
      ...point,
    };
  } else {
    nodeData = {
      ...point,
      title: "New",
      type: "operation",
    };
  }
  setNodes((prevState) => [...prevState, nodeData]);
}

onConnectionDoubleClick: (connection: ConnectionData) => void

Triggered when a connection is double-clicked.

Tip: Double-click to edit connection.

onMouseUp: (event: React.MouseEvent<SVGSVGElement>, zoom: number) => void

Triggered when the mouse is up on the background svg.

Tip: Drop something to here to implement node creation.

License

MIT © Joyceworks

3.6.5

3 years ago

3.6.4

3 years ago