Introducing a component for viewing/editing simple flow-charts but with the ability to easily customise the contents of the "task" elements.
The layout is handled through CSS grid
which avoids error-prone pixel calculations.
The limited flexibility is accepted and even desired to concentrate on the more important content while still achieving a uniform design.
Topics covered in this document are:
- Demo
- Usage
npm i react-flow-modeler
Describing the whole data model being displayed, expecting an object with two fields:
flow.firstElementId
– string containing the key to theflow.elements
entry, that should follow the start nodeflow.elements
– object containing all "step" nodes and "diverging gateway" nodes as values- "step" nodes are represented by an object that may contain the following fields:
data
– object in which any kind of additional information may be stored (in order to consider it in therenderStep
functionnextElementId
– string containing the key to theflow.elements
entry, that should follow this step (if omitted or without matching entry inflow.elements
, it will point to the end node)
- "diverging gateway" nodes are represented by an object that may contain the following fields:
data
– object in which any kind of additional information may be stored (in order to consider it in therenderGatewayConditionType
functionnextElements
(required, otherwise it is treated as "step" node) – array of branches from this gateway, each branch being represented by an object with the following fields:conditionData
– object in which any kind of additional information may be stored (in order to consider it in therenderGatewayConditionValue
functionid
– string containing the key to theflow.elements
entry, that should follow this gateway branch (if omitted or without matching entry inflow.elements
, it will point to the end node)
- "step" nodes are represented by an object that may contain the following fields:
Currently only catering for one setting:
verticalAlign
– either"top"
,"middle"
or"bottom"
; with"middle"
being the default
Render function for a "step" element expecting a react element to be returned based on a single input object containing mainly the following fields:
type
– string of fixed value"step"
id
– string containing the key to the correspondingflow.elements
entrydata
– the provideddata
object, also available underflow.elements[id].data
precedingElement
– reference to the directly preceding element in the flowfollowingElement
– reference to the directly following element in the flow
Render function for the condition label following a diverging gateway expecting a react element to be returned based on a single input object containing mainly the following fields:
type
– string of fixed value"div-gw"
id
– string containing the key to the correspondingflow.elements
entrydata
– the provideddata
object, also available underflow.elements[id].data
precedingElement
– reference to the directly preceding element in the flowfollowingBranches
– reference to the associated following branches in the flow
Render function for the condition label on a branch from a diverging gateway expecting a react element to be returned based on a single input object containing mainly the following fields:
type
– string of fixed value"div-branch"
branchIndex
– string containing the key to the correspondingflow.elements
entrydata
– the provideddata
object, also available underflow.elements[precedingElement.id].nextElements[branchIndex].conditionData
precedingElement
– reference to the directly preceding diverging gateway in the flowfollowingElement
– reference to the directly following element in the flow
Callback function that when present enables structural editing,
- receiving a single input object containing the following field:
changedFlow
– updatedflow
that should be stored in some external state and provided again via theflow
prop to the<Modeler>
Object containing various customization options for the structural editing feature in the following fields:
addDivergingBranch
– object for customizing the adding of branches to a diverging gateway, expecting any of the following fields:className
– string overriding the default"menu-item add-branch"
css classes on the corresponding context menu itemtitle
– string defining the tool-tip to show for the corresponding context menu itemisActionAllowed
– function for preventing adding branches to certain gateway, expecting aboolean
to be returned based on the same single input object as onrenderGatewayConditionType
referring to the selected elementgetBranchConditionData
– function for providing the defaultconditionData
on a newly added diverging gateway branch based on the same single input object as onrenderGatewayConditionType
addFollowingStepElement
– object for customizing the adding of step nodes, expecting any of the following fields:className
– string overriding the default"menu-item add-step"
css classes on the corresponding context menu itemtitle
– string defining the tool-tip to show for the corresponding context menu itemisActionAllowed
– function for preventing adding step nodes after certain elements, expecting aboolean
to be returned based on the reference to the selected elementgetStepData
– function for providing the defaultdata
on a newly added step node based on the reference to the element where the corresponding context menu item was clicked
addFollowingDivergingGateway
– object for customizing the adding of diverging gateway nodes, expecting any of the following fields:className
– string overriding the default"menu-item add-gateway"
css classes on the corresponding context menu itemtitle
– string defining the tool-tip to show for the corresponding context menu itemisActionAllowed
– function for preventing adding diverging gateways after certain elements, expecting aboolean
to be returned based on the reference to the element where the corresponding context menu item was clickedgetGatewayData
– function for providing the defaultdata
on a newly added diverging gateway based on the reference to the selected elementgetBranchConditionData
– function for providing an array of the defaultconditionData
objects for each branch of a newly added diverging gateway based on the reference to the element where the corresponding context menu item was clicked; thereby also determining how many branches there are by default
changeNextElement
– object for customizing the links between elements in the flow, expecting any of the following fields:className
– string overriding the default"menu-item change-next"
css classes on the corresponding context menu itemtitle
– string defining the tool-tip to show for the corresponding context menu itemisActionAllowed
– function for preventing links from certain elements to be changed, expecting aboolean
to be returned based on the reference to the selected element
removeElement
– object for customizing the removal of elements in the flow, expecting any of the following fields:className
– string overriding the default"menu-item remove"
css classes on the corresponding context menu itemtitle
– string defining the tool-tip to show for the corresponding context menu itemisActionAllowed
– function for preventing certain elements to be removed, expecting aboolean
to be returned based on the reference to the selected element
- input: expecting a complete
flow
as single input parameter - output: returning a
boolean
response to indicate whether the givenflow
is deemed valid by the<Modeler>
- input: expecting a complete
flow
as single input parameter - output: not returning anything in case of a valid
flow
but throwing anError
otherwise
- inputs:
- expecting a complete
flow
as first input parameter - expecting the
id
of a step node (i.e. identifying the step node underflow.elements[id]
) as second input parameter - expecting a callback function as third input parameter
- input: the current
data
of the targeted step node will be provided as single input parameter - output: the new
data
object to set on the targeted step node is expected to be returned
- input: the current
- expecting a complete
- output: returning an object (that can be provided to your
onChange
function) containing the following field:changedFlow
– updatedflow
that should be stored in some external state and provided again via theflow
prop to the<Modeler>
- inputs:
- expecting a complete
flow
as first input parameter - expecting the
id
of a gateway node (i.e. identifying the gateway node underflow.elements[id]
) as second input parameter - expecting a callback function as third input parameter
- input: the current
data
of the targeted gateway node will be provided as single input parameter - output: the new
data
object to set on the targeted gateway node is expected to be returned
- input: the current
- optionally catering for another callback function to be provided as fourth input parameter
- inputs:
- the current
conditionData
of a branch of the targeted gateway node will be provided as first input parameter - the index of the respective branch of the targeted gateway node will be provided as second input parameter
- an array of the current
conditionData
of all branches of the targeted gateway node will be provided as third input parameter
- the current
- ooutput: the new
conditionData
object to set on the respective branch of the targeted gateway node is expected to be returned
- inputs:
- expecting a complete
- output: returning an object (that can be provided to your
onChange
function) containing the following field:changedFlow
– updatedflow
that should be stored in some external state and provided again via theflow
prop to the<Modeler>
- inputs:
- expecting a complete
flow
as first input parameter - expecting the
id
of a gateway node (i.e. identifying the gateway node underflow.elements[id]
) as second input parameter - expecting the index of the targeted branch of the gateway node (i.e. referring to
flow.elements[id].nextElements[index]
) as third input parameter - expecting a callback function as fourth input parameter
- input: the current
conditionData
of the targeted gateway branch will be provided as single input parameter - output: the new
conditionData
object to set on the targeted gateway branch is expected to be returned
- input: the current
- expecting a complete
- output: returning an object (that can be provided to your
onChange
function) containing the following field:changedFlow
– updatedflow
that should be stored in some external state and provided again via theflow
prop to the<Modeler>
import { FlowModeler } from "react-flow-modeler";
<FlowModeler
flow={{
firstElementId: "a",
elements: {
"a": {
nextElementId: "b",
data: { stepContent: "A" }
},
"b": {
nextElements: [
{ id: "c", conditionData: { conditionValue: "1" } },
{ conditionData: { conditionValue: "2" } }
],
data: { conditionType: "B?" }
},
"c": {
data: { stepContent: "C" }
}
}
}}
renderStep={({ data }) => <label>{data ? data.stepContent : null}</label>}
renderGatewayConditionType={({ data }) => <label>{data ? data.conditionType : null}</label>}
renderGatewayConditionValue={({ data }) => <label>{data ? data.conditionValue : null}</label>}
/>
import { FlowModeler } from "react-flow-modeler";
const externalState = {
flow: { firstElementId: null, elements: {} }
};
<FlowModeler
flow={externalState.flow}
renderStep={({ data }) => data ? <label>{data.value}</label> : null}
renderGatewayConditionType={({ data }) => data ? <label>{data.value}</label> : null}
renderGatewayConditionValue={({ data }) => data ? <label>{data.value}</label> : null}
onChange={({ changedFlow }) => { externalState.flow = changedFlow; }}
/>