3.16.1 • Published 8 months ago

flowchart-react v3.16.1

Weekly downloads
6
License
MIT
Repository
-
Last release
8 months 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,
    },
    {
      type: "end",
      title: "End",
      x: 500,
      y: 190,
      id: 2,
    },
    {
      x: 330,
      y: 190,
      id: 3,
      title: "Joyce",
    },
    {
      x: 330,
      y: 300,
      id: 4,
      title: () => {
        return "No approver";
      },
    },
  ]);
  const [conns, setConns] = useState<ConnectionData[]>([
    {
      source: { id: 1, position: "right" },
      destination: { id: 3, position: "left" },
    },
    {
      source: { id: 3, position: "right" },
      destination: { id: 2, position: "left" },
    },
    {
      source: { id: 1, position: "bottom" },
      destination: { id: 4, position: "left" },
    },
    {
      source: { id: 4, position: "right" },
      destination: { id: 2, position: "bottom" },
    },
  ]);

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

export default App;

Demo

API

Flowchart use nodes and connections to describe a flowchart.

Props

nodes: NodeData[]

Array of nodes.

NodeData
PropsDescriptionTypeDefaultRequired
idIdentitynumbertrue
titleTitle of nodestring, (node: NodeData) => string, JSX.Elementtrue
typeType of nodestart, end, operation, decisionoperationfalse
xX axisnumbertrue
yY axisnumbertrue
payloadCustom data{[key: string]: unknown}false
widthNode widthnumber120false
heightNode heightnumber60false
connectionPositionConnection positiontop, bottomtopfalse
containerPropsSupportedSVGShapePropsfalse
textPropsSupportedSVGTextPropsfalse
SupportedSVGShapeProps

Node shape props, only fill and stroke are supported, for more information, please refer to MDN.

PropsDescriptionTypeDefaultRequired
fillstringfalse
strokestringfalse
SupportedSVGTextProps

Node text props, only fill is supported, for more information, please refer to MDN.

Works when title is a string.

PropsDescriptionTypeDefaultRequired
fillstringfalse

connections: ConnectionData[]

Connections between nodes.

ConnectionData

Use type to describe the type of connection, success will draw a green line, fail will draw a red line.

PropsDescriptionTypeDefaultRequired
typeType of connectionsuccess, failsuccessfalse
sourceSource info{id: number, position: 'left', 'right', 'top', 'bottom'}true
destinationDestination info{id: number, position: 'left', 'right', 'top', 'bottom'}true
titleTitle of connectionstringfalse

readonly: boolean | undefined

Prop to disabled drag, connect and delete nodes.

style: React.CSSProperties

Style of container.

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

Global node size, works when width or height of node is not set.

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

showToolbar: boolean | undefined | ("start-end" | "operation" | "decision")[]

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.

className: string | undefined

Custom class name of container.

License

MIT © Joyceworks

3.16.1

8 months ago

3.16.0

8 months ago

3.16.3

8 months ago

3.16.2

8 months ago

3.15.0

9 months ago

3.14.0

9 months ago

3.13.0

9 months ago

3.12.3

9 months ago

3.10.5

11 months ago

3.12.2

9 months ago

3.12.5

9 months ago

3.12.4

9 months ago

3.11.0

9 months ago

3.12.1

9 months ago

3.12.0

9 months ago

3.10.4

1 year ago

3.10.3

1 year ago

3.10.2

1 year ago

3.9.0

1 year ago

3.8.1

1 year ago

3.10.1

1 year ago

3.8.0

1 year ago

3.10.0

1 year ago

3.7.0

1 year ago

3.6.3

2 years ago

3.4.0

2 years ago

3.3.0

2 years ago

3.2.0

2 years ago

3.6.2

2 years ago

3.6.1

2 years ago

3.6.0

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

3.4.1

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.3.0

2 years ago

2.2.1

2 years ago

2.1.2

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.0.2

2 years ago

2.3.1

2 years ago

2.2.2

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago