Home

Awesome

Introduction

English | 简体中文

A highly customizable streaming flow builder. The registration ability can flexibly customize your nodes, different types of node display and form, etc.

demo1demo2

Try it out

https://bytedance.github.io/flow-builder

Github

https://github.com/bytedance/flow-builder

Installation

yarn add react-flow-builder

or

npm install react-flow-builder

Usage

// index.tsx
import React, { useState, useContext } from 'react';
import FlowBuilder, {
  NodeContext,
  INode,
  IRegisterNode,
} from 'react-flow-builder';

import './index.css';

const StartNodeDisplay: React.FC = () => {
  const node = useContext(NodeContext);
  return <div className="start-node">{node.name}</div>;
};

const EndNodeDisplay: React.FC = () => {
  const node = useContext(NodeContext);
  return <div className="end-node">{node.name}</div>;
};

const OtherNodeDisplay: React.FC = () => {
  const node = useContext(NodeContext);
  return <div className="other-node">{node.name}</div>;
};

const ConditionNodeDisplay: React.FC = () => {
  const node = useContext(NodeContext);
  return <div className="condition-node">{node.name}</div>;
};

const registerNodes: IRegisterNode[] = [
  {
    type: 'start',
    name: 'start node',
    displayComponent: StartNodeDisplay,
    isStart: true,
  },
  {
    type: 'end',
    name: 'end node',
    displayComponent: EndNodeDisplay,
    isEnd: true,
  },
  {
    type: 'node',
    name: 'other node',
    displayComponent: OtherNodeDisplay,
  },
  {
    type: 'condition',
    name: 'condition node',
    displayComponent: ConditionNodeDisplay,
  },
  {
    type: 'branch',
    name: 'branch node',
    conditionNodeType: 'condition',
  },
];

const Demo = () => {
  const [nodes, setNodes] = useState<INode[]>([]);

  const handleChange = (nodes: INode[]) => {
    console.log('nodes change', nodes);
    setNodes(nodes);
  };

  return (
    <FlowBuilder
      nodes={nodes}
      onChange={handleChange}
      registerNodes={registerNodes}
    />
  );
};

export default Demo;

// index.css
.start-node, .end-node {
  height: 64px;
  width: 64px;
  border-radius: 50%;
  line-height: 64px;
  color: #fff;
  text-align: center;
}

.start-node {
  background-color: #338aff;
}

.end-node {
  background-color: #666;
}

.other-node, .condition-node {
  width: 224px;
  border-radius: 4px;
  color: #666;
  background: #fff;
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.08);
}

.other-node {
  height: 118px;
  padding: 16px;
  display: flex;
  flex-direction: column;
}

.condition-node {
  height: 44px;
  padding: 12px 16px;
}

API

FlowBuilder

PropertyDescriptionTypeRequiredDefaultVersion
backgroundColorThe color of backgroundstring#F7F7F7
classNameThe class name of the containerstring-
draggabledrag and dropbooleanfalse1.0.0
DragComponentcustom drag componentReact.FC<DragComponent>-1.0.0
DropComponentcustom drop componentReact.FC<DropComponent>-1.0.0
createUuidcustom node uuid(type?: string) => string-2.0.0
DrawerComponentDrawer componentReact.FC<DrawerComponent>-2.0.0
PopoverComponentPopover componentReact.FC<PopoverComponent>-2.0.0
PopconfirmComponentPopconfirm componentReact.FC<PopconfirmComponent>-2.0.0
drawerPropsExtra props of DrawerComponentany-
drawerVisibleWhenAddNodeDrawer visible when add nodebooleanfalse
historyToolundo and redoboolean | HistoryToolConfigfalse
layoutUse vertical or horizontal layoutvertical | horizontalvertical
lineColorThe color of linestring#999999
nodesThe nodes of FlowBuilderNode[]-
readonlyReadonly mode, cannot add, remove, configure.booleanfalse
registerNodesThe registered nodesRegisterNode[]-
registerRemoteNodesThe registered remote nodesRegisterRemoteNode[]-1.3.0
showPracticalBranchNode-booleanfalse1.1.0
showPracticalBranchRemove-booleanfalse1.1.0
sortableCondition nodes can be dragged and sorted in branchbooleanfalse1.4.0
sortableAnchorAnchor for start dragging 序ReactNode-1.4.0
spaceXHorizontal spacing between nodesnumber16
spaceYVertical spacing between nodesnumber16
zoomToolzoomboolean | ZoomToolConfigfalse
onChangeCallback function for when the data change(nodes: Node[], changeEvent: string, nodeChanged?: INode) => void-
onHistoryChange(undoDisabled: boolean, redoDisabled: boolean) => void-
onZoomChange(outDisabled: boolean, value: number, inDisabled: boolean) => void-
showArrowShow arrowbooleanfalse1.4.5
arrowIconThe icon of the arrowReactNode-1.4.5
onAddNodeSuccessCalled when add node success(type: string, node: INode) => void-1.4.9
onDropNodeSuccessCalled when drop node success(type: string, node: INode) => void-1.4.9
onRemoveNodeSuccessCalled when remove node success(node: INode) => void-2.2.0
allowStartConfigAllow start node configboolean-2.1.0
allowEndConfigAllow end node configboolean-2.1.0
scrollByDragScroll by mouse draggingboolean-2.6.0

HistoryToolConfig

PropertyDescriptionTypeDefault
hiddenbooleanfalse
maxnumber10

ZoomToolConfig

PropertyDescriptionTypeDefault
hiddenbooleanfalse
initialValuenumber100
minnumber10
maxnumber200
stepnumber10

DragComponent

PropertyDescriptionTypeVersion
onDragStartThe dragStart event of the custom drag component needs to call this method to set the dragged type( dragType in BuilderContext )(nodeType: string) => void1.0.0
onDragEndThe dragEnd event of the custom drag component needs to call this method to clear the dragged type( dragType in BuilderContext )() => void1.0.0

DropComponent

PropertyDescriptionTypeVersion
onDropThe drop event of the custom drop component needs to call this method to add the new node type() => void1.0.0

DrawerComponent

PropertyDescriptionTypeVersion
visibleYou can judge the boolean value of selectedNode by yourself.any2.0.0
onCloseYou can also call closeDrawer by yourself.any2.0.0
childrenany2.0.0
titleany2.0.0
widthany2.0.0
destroyOnCloseany2.0.0
maskClosableany2.0.0
configComponentRefReact.MutableRefObject<any>2.5.0

PopoverComponent

PropertyDescriptionTypeVersion
visibleany2.0.0
onVisibleChangeany2.0.0
childrenany2.0.0
overlayClassNameany2.0.0
placementany2.0.0
triggerany2.0.0
contentany2.0.0
getPopupContainerany2.0.0

PopconfirmComponent

PropertyDescriptionTypeVersion
titleany2.0.0
onConfirmYou can also call removeNode yourself.any2.0.0
childrenany2.0.0
getPopupContainerany2.0.0

FlowBuilderInstance

NameDescriptionTypeVersion
addadd node(node: INode, newNodeType: string) => void | (newNodeType: string) => void
historyundo, redo(type: 'undo' | 'redo') => void
removeremove noded(nodes: INode | INode[] = useContext(NodeContext)) => void
zoomzoom(type: 'out' | 'in' | number) => void
closeDrawerclose drawer() => void
contextBuilderContextBuilderContext1.3.5

Formatter

NameDescriptionType
buildFlatNodesTranslate to flat nodes(params: {registerNodes: IRegisterNode[], nodes: INode[]}) => INode[]
buildTreeNodesTranslate to tree nodes(params: {nodes: INode[]}) => INode[]
createUuidCreate uuid(prefix?: string) => string

RegisterNode

PropertyDescriptionTypeRequiredDefaultVersion
addableComponentReact.FC<AddableComponent>-
addableNodeTypesThe list of nodes that can be added below the nodestring[]-
addIconThe icon in addable node list (There are already some default icons)ReactNode-
addConditionIconThe icon of the branch node when adding a condition (The default icon already exists)ReactNode-1.3.3
classNameThe class name of nodestring-1.3.4
conditionMaxNumThe max number of condition nodenumber-
conditionNodeTypeThe type of condition nodestring-
configComponentThe Component of configuring node formReact.FC<ConfigComponent> | React.ForwardRefExoticComponent<ConfigComponent & React.RefAttributes<any>>-
configTitleThe drawer title of configuring nodestring | ((node: INode, nodes: INode[]) => string)-
customRemoveCustom remove buttonbooleanfalse
displayComponentThe Component of displaying nodeReact.FC<DisplayComponent>-
initialNodeDatathe initial data when add new nodeRecord<string, any>-
isStartIs start nodebooleanfalse
isEndIs end nodebooleanfalse
isLoopIs loop nodebooleanfalse1.4.6
nameThe name of nodestring-
removeConfirmTitleThe confirmation information before deleting the node. The title of Popconfirmstring | ReactNodeAre you sure to remove this node?
showPracticalBranchNode-booleanfalse1.1.0
showPracticalBranchRemove-booleanfalse1.1.0
typeThe type of node, promise start is start node type and end is end node typestring-

RegisterRemoteNode

PropertyDescriptionTypeRequiredDefaultVersion
urlremote urlstring-1.3.0
cssUrlremote css urlstring-1.3.0

DisplayComponent

PropertyDescriptionType
nodeThe all information of node (NodeContext is recommended since V1)Node
nodes(BuilderContext is recommended since V1)Node[]
readonly(BuilderContext is recommended since V1)boolean
removeRemove node (useAction is recommended since V1)(nodes?: INode | INode[]) => void

ConfigComponent

PropertyDescriptionType
cancelCalled on cancel, used to close the drawer (useDrawer is recommended since V1)() => void
nodeThe all information of node (NodeContext is recommended since V1)Node
nodes(BuilderContext is recommended since V1)Node[]
saveCalled on save node data (automatically close the drawer, no need to call cancel). FlowBuilder will set the validateStatusError property according to the second param (useDrawer is recommended since V1)(values: any, validateStatusError?: boolean) => void

AddableComponent

PropertyDescriptionType
add(type: string) => void
nodeThe all information of node (NodeContext is recommended since V1)Node
nodes(BuilderContext is recommended since V1)Node[]

Node

PropertyDescriptionType
childrenThe condition nodes array of branch node, or the next flow of condition nodeNode[]
configuringWhether configuring of node. The display Component can highlight the node according to this propertyboolean
dataThe data of nodeany
idThe unique id of nodestring
nameThe name of node. Same as the name of the registered nodestring
pathThe full path in FlowBuilderstring[]
typeThe type of node. Same as the type of the registered nodestring
validateStatusErrorThe Component of configuring node form validate failed. The display Component can highlight the node according to this propertyboolean

Context

Added since V1

In the context of FlowBuilder the following contexts can be used

BuilderContext

Contains props and state. The following is the state:

PropertyDescriptionType
zoomValuecurrent zoom valuenumber
setZoomValueset zoomValue(zoomValue: number) => void
historyRecordshistory nodes recordsINode[][]
setHistoryRecordsset historyRecords(records: INode[][]) => void
activeHistoryRecordIndexcurrent index in history nodes recordsnumber
setActiveHistoryRecordIndexset activeHistoryRecordIndex(index: number) => void
selectedNodecurrent selecred nodeINode | undefined
setSelectedNodeset selectedNode(node: INode | undefined) => void
drawerTitlethe title of Drawerstring
setDrawerTitleset drawerTitle(title: string) => void
dragTypedragged node typestring
setDragTypeset dragType(type: string) => void

NodeContext

Get the data of the node where it is used. For details Node

Hooks

Added since V1

In the context of FlowBuilder the following hooks can be used

useAction

PropertyDescriptionTypeVersion
clickNodeclick node(node: INode = useContext(NodeContext)) => void
addNodeadd one node. (Get the current node through NodeContext when there is no node property)(node: INode, newNodeType: string) => void | (newNodeType: string) => void
addNodeInLoopadd one node in loop node.(newNodeType: string) => void1.4.6
removeNoderemove one node or more nodes.(targetNode: INode | INode[] = useContext(NodeContext)) => void

useDrawer

PropertyDescriptionType
closeDrawerclose Drawer and clear selectedNode() => void
saveDrawersave the content in Drawer (same as the save method in ConfigComponent)(values: any, validateStatusError?: boolean) => void

useZoom

PropertyDescriptionType
minZoomminimum zoom valuenumber
maxZoommaximum zoom valuenumber
zoomchange zoom value (same as the zoom method in FlowBuilderInstance)(type: 'out' | 'in' | number) => void

useHistory

PropertyDescriptionType
maxLengthMaximum length of history nodes recordsnumber
pushHistoryadd history nodes record(record?: INode[] = useContext(BuilderContext).nodes) => void
historyundo, redo (same as the history method in FlowBuilderInstance)(type: 'undo' | 'redo') => void

useSort

PropertyDescriptionTypeVersion
backwardsort to backward(node: INode = useContext(NodeContext)) => void1.4.3
forwardsort to forward(node: INode = useContext(NodeContext)) => void1.4.3
endsort to end(node: INode = useContext(NodeContext)) => void1.4.3
startsort to start(node: INode = useContext(NodeContext)) => void1.4.3