Merge pull request #58 from wbkd/refactor/examples
refactor(examples): update custom node example, create rich example
This commit is contained in:
@@ -107,7 +107,7 @@ nodeTypes={{
|
|||||||
|
|
||||||
You can now use type `special` for a node.
|
You can now use type `special` for a node.
|
||||||
The `default`, `input` and `output` types will be still available except you overwrite one of them.
|
The `default`, `input` and `output` types will be still available except you overwrite one of them.
|
||||||
You can find an example of how to implement a custom node in [custom nodes example](example/src/CustomNodes).
|
You can find an example of how to implement a custom node in the [custom node example](example/src/CustomNode).
|
||||||
|
|
||||||
|
|
||||||
## Edges
|
## Edges
|
||||||
@@ -200,7 +200,8 @@ const GraphWithControls = () => (
|
|||||||
|
|
||||||
You can find all examples in the [example](example) folder. They are also deployt:
|
You can find all examples in the [example](example) folder. They are also deployt:
|
||||||
|
|
||||||
|
- [rich](https://react-flow.netlify.com/rich)
|
||||||
- [basic](https://react-flow.netlify.com/basic)
|
- [basic](https://react-flow.netlify.com/basic)
|
||||||
- [empty](https://react-flow.netlify.com/empty)
|
- [empty](https://react-flow.netlify.com/empty)
|
||||||
- [basic](https://react-flow.netlify.com/basic)
|
- [inactive](https://react-flow.netlify.com/inactive)
|
||||||
- [custom nodes](https://react-flow.netlify.com/)
|
- [custom node](https://react-flow.netlify.com/custom-node)
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
describe('Custom Node Graph Rendering', () => {
|
||||||
|
it('renders a graph', () => {
|
||||||
|
cy.visit('/custom-node');
|
||||||
|
|
||||||
|
cy.get('.react-flow__renderer');
|
||||||
|
|
||||||
|
cy.get('.react-flow__node').should('have.length', 4);
|
||||||
|
cy.get('.react-flow__edge').should('have.length', 3);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
describe('Custom Nodes Graph Rendering', () => {
|
|
||||||
it('renders a flow with sone nodes', () => {
|
|
||||||
cy.visit('/');
|
|
||||||
|
|
||||||
cy.get('.react-flow__renderer');
|
|
||||||
|
|
||||||
cy.get('.react-flow__node').should('have.length', 8);
|
|
||||||
cy.get('.react-flow__edge').should('have.length', 8);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('renders a grid', () => {
|
|
||||||
cy.get('.react-flow__grid');
|
|
||||||
|
|
||||||
const gridStroke = Cypress.$('.react-flow__grid path').attr('fill');
|
|
||||||
expect(gridStroke).to.equal('#888');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('connects nodes', () => {
|
|
||||||
cy.get('.react-flow__node')
|
|
||||||
.contains('1 Tests')
|
|
||||||
.find('.react-flow__handle.source')
|
|
||||||
.trigger('mousedown', { which: 1 });
|
|
||||||
|
|
||||||
cy.get('.react-flow__node')
|
|
||||||
.contains('7 output')
|
|
||||||
.find('.react-flow__handle.target')
|
|
||||||
.trigger('mousemove')
|
|
||||||
.should('have.class', 'connecting')
|
|
||||||
.should('have.class', 'valid')
|
|
||||||
.trigger('mouseup', { force: true })
|
|
||||||
.should('not.have.class', 'valid')
|
|
||||||
.should('not.have.class', 'connecting');
|
|
||||||
|
|
||||||
cy.get('.react-flow__edge').should('have.length', 9);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('tries to make invalid connection', () => {
|
|
||||||
cy.get('.react-flow__node')
|
|
||||||
.contains('write something')
|
|
||||||
.parents('.react-flow__node')
|
|
||||||
.find('.react-flow__handle.source')
|
|
||||||
.trigger('mousedown', { which: 1 });
|
|
||||||
|
|
||||||
cy.get('.react-flow__node')
|
|
||||||
.contains('5 Another node')
|
|
||||||
.find('.react-flow__handle.target')
|
|
||||||
.trigger('mousemove')
|
|
||||||
.should('have.class', 'connecting')
|
|
||||||
.should('not.have.class', 'valid')
|
|
||||||
.trigger('mouseup', { force: true })
|
|
||||||
.should('not.have.class', 'connecting');
|
|
||||||
|
|
||||||
cy.get('.react-flow__edge').should('have.length', 9);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
describe('Rich Graph Rendering', () => {
|
||||||
|
it('renders a graph', () => {
|
||||||
|
cy.visit('/');
|
||||||
|
|
||||||
|
cy.get('.react-flow__renderer');
|
||||||
|
|
||||||
|
cy.get('.react-flow__node').should('have.length', 6);
|
||||||
|
cy.get('.react-flow__edge').should('have.length', 5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders a grid', () => {
|
||||||
|
cy.get('.react-flow__grid');
|
||||||
|
|
||||||
|
const gridStroke = Cypress.$('.react-flow__grid path').attr('fill');
|
||||||
|
expect(gridStroke).to.equal('#888');
|
||||||
|
});
|
||||||
|
});
|
||||||
Vendored
+142
-1160
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+142
-1160
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+2
-1
@@ -7,8 +7,9 @@ interface ConnectionLineProps {
|
|||||||
connectionLineType?: string | null;
|
connectionLineType?: string | null;
|
||||||
nodes: Node[];
|
nodes: Node[];
|
||||||
transform: Transform;
|
transform: Transform;
|
||||||
|
isInteractive: boolean;
|
||||||
connectionLineStyle?: SVGAttributes<{}>;
|
connectionLineStyle?: SVGAttributes<{}>;
|
||||||
className?: string;
|
className?: string;
|
||||||
}
|
}
|
||||||
declare const _default: ({ connectionSourceId, connectionLineStyle, connectionPositionX, connectionPositionY, connectionLineType, nodes, className, transform, }: ConnectionLineProps) => JSX.Element | null;
|
declare const _default: ({ connectionSourceId, connectionLineStyle, connectionPositionX, connectionPositionY, connectionLineType, nodes, className, transform, isInteractive, }: ConnectionLineProps) => JSX.Element | null;
|
||||||
export default _default;
|
export default _default;
|
||||||
|
|||||||
Vendored
+2
-1
@@ -8,6 +8,7 @@ interface EdgeWrapperProps {
|
|||||||
onClick: (edge: Edge) => void;
|
onClick: (edge: Edge) => void;
|
||||||
animated: boolean;
|
animated: boolean;
|
||||||
selected: boolean;
|
selected: boolean;
|
||||||
|
isInteractive: boolean;
|
||||||
}
|
}
|
||||||
declare const _default: (EdgeComponent: React.ComponentType<EdgeCompProps>) => React.MemoExoticComponent<({ id, source, target, type, animated, selected, onClick, ...rest }: EdgeWrapperProps) => JSX.Element>;
|
declare const _default: (EdgeComponent: React.ComponentType<EdgeCompProps>) => React.MemoExoticComponent<({ id, source, target, type, animated, selected, onClick, isInteractive, ...rest }: EdgeWrapperProps) => JSX.Element>;
|
||||||
export default _default;
|
export default _default;
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
/// <reference types="react" />
|
/// <reference types="react" />
|
||||||
import { NodeProps } from '../../types';
|
import { NodeProps } from '../../types';
|
||||||
declare const _default: ({ data, style }: NodeProps) => JSX.Element;
|
declare const _default: ({ data, targetPosition, sourcePosition, style }: NodeProps) => JSX.Element;
|
||||||
export default _default;
|
export default _default;
|
||||||
|
|||||||
Vendored
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
/// <reference types="react" />
|
/// <reference types="react" />
|
||||||
import { NodeProps } from '../../types';
|
import { NodeProps } from '../../types';
|
||||||
declare const _default: ({ data, style }: NodeProps) => JSX.Element;
|
declare const _default: ({ data, style, sourcePosition }: NodeProps) => JSX.Element;
|
||||||
export default _default;
|
export default _default;
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
/// <reference types="react" />
|
/// <reference types="react" />
|
||||||
import { NodeProps } from '../../types';
|
import { NodeProps } from '../../types';
|
||||||
declare const _default: ({ data, style }: NodeProps) => JSX.Element;
|
declare const _default: ({ data, style, targetPosition }: NodeProps) => JSX.Element;
|
||||||
export default _default;
|
export default _default;
|
||||||
|
|||||||
Vendored
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { NodeComponentProps, WrapNodeProps } from '../../types';
|
import { NodeComponentProps, WrapNodeProps } from '../../types';
|
||||||
declare const _default: (NodeComponent: React.ComponentType<NodeComponentProps>) => React.MemoExoticComponent<({ id, type, data, transform, xPos, yPos, selected, onClick, onNodeDragStop, style, isInteractive, }: WrapNodeProps) => JSX.Element>;
|
declare const _default: (NodeComponent: React.ComponentType<NodeComponentProps>) => React.MemoExoticComponent<({ id, type, data, transform, xPos, yPos, selected, onClick, onNodeDragStop, style, isInteractive, sourcePosition, targetPosition, }: WrapNodeProps) => JSX.Element>;
|
||||||
export default _default;
|
export default _default;
|
||||||
|
|||||||
+4
-1
@@ -1,3 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
declare const _default: React.MemoExoticComponent<() => JSX.Element | null>;
|
declare type UserSelectionProps = {
|
||||||
|
isInteractive: boolean;
|
||||||
|
};
|
||||||
|
declare const _default: React.MemoExoticComponent<({ isInteractive }: UserSelectionProps) => JSX.Element | null>;
|
||||||
export default _default;
|
export default _default;
|
||||||
|
|||||||
Vendored
+3
-3
@@ -35,9 +35,9 @@ declare const ReactFlow: {
|
|||||||
onLoad: () => void;
|
onLoad: () => void;
|
||||||
onMove: () => void;
|
onMove: () => void;
|
||||||
nodeTypes: {
|
nodeTypes: {
|
||||||
input: ({ data, style }: import("../../types").NodeProps) => JSX.Element;
|
input: ({ data, style, sourcePosition }: import("../../types").NodeProps) => JSX.Element;
|
||||||
default: ({ data, style }: import("../../types").NodeProps) => JSX.Element;
|
default: ({ data, targetPosition, sourcePosition, style }: import("../../types").NodeProps) => JSX.Element;
|
||||||
output: ({ data, style }: import("../../types").NodeProps) => JSX.Element;
|
output: ({ data, style, targetPosition }: import("../../types").NodeProps) => JSX.Element;
|
||||||
};
|
};
|
||||||
edgeTypes: {
|
edgeTypes: {
|
||||||
default: React.MemoExoticComponent<({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, style, }: import("../../types").EdgeBezierProps) => JSX.Element>;
|
default: React.MemoExoticComponent<({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, style, }: import("../../types").EdgeBezierProps) => JSX.Element>;
|
||||||
|
|||||||
Vendored
+8
@@ -43,6 +43,8 @@ export interface Node {
|
|||||||
__rg?: any;
|
__rg?: any;
|
||||||
data?: any;
|
data?: any;
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
|
targetPosition?: Position;
|
||||||
|
sourcePosition?: Position;
|
||||||
}
|
}
|
||||||
export interface Edge {
|
export interface Edge {
|
||||||
id: ElementId;
|
id: ElementId;
|
||||||
@@ -68,6 +70,8 @@ export interface NodeProps {
|
|||||||
type: string;
|
type: string;
|
||||||
data: any;
|
data: any;
|
||||||
selected: boolean;
|
selected: boolean;
|
||||||
|
targetPosition?: Position;
|
||||||
|
sourcePosition?: Position;
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
}
|
}
|
||||||
export interface NodeComponentProps {
|
export interface NodeComponentProps {
|
||||||
@@ -78,6 +82,8 @@ export interface NodeComponentProps {
|
|||||||
transform?: Transform;
|
transform?: Transform;
|
||||||
xPos?: number;
|
xPos?: number;
|
||||||
yPos?: number;
|
yPos?: number;
|
||||||
|
targetPosition?: Position;
|
||||||
|
sourcePosition?: Position;
|
||||||
onClick?: (node: Node) => void | undefined;
|
onClick?: (node: Node) => void | undefined;
|
||||||
onNodeDragStop?: () => any;
|
onNodeDragStop?: () => any;
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
@@ -94,6 +100,8 @@ export interface WrapNodeProps {
|
|||||||
onClick: (node: Node) => void | undefined;
|
onClick: (node: Node) => void | undefined;
|
||||||
onNodeDragStop: (node: Node) => void;
|
onNodeDragStop: (node: Node) => void;
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
|
sourcePosition?: Position;
|
||||||
|
targetPosition?: Position;
|
||||||
}
|
}
|
||||||
export declare type FitViewParams = {
|
export declare type FitViewParams = {
|
||||||
padding: number;
|
padding: number;
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import { Handle } from 'react-flow';
|
||||||
|
|
||||||
|
export default ({ data, styles }) => {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{ background: '#eee', border: '1px solid #ddd', padding: 10, borderRadius: 5, ...styles }}
|
||||||
|
>
|
||||||
|
<Handle
|
||||||
|
type="target"
|
||||||
|
position="left"
|
||||||
|
style={{ background: '#fff' }}
|
||||||
|
onConnect={params => console.log('handle onConnect', params)}
|
||||||
|
/>
|
||||||
|
<div>Custom Color Picker Node: <strong>{data.color}</strong></div>
|
||||||
|
<input type="color" onChange={data.onChange} defaultValue={data.color}/>
|
||||||
|
<Handle
|
||||||
|
type="source"
|
||||||
|
position="right"
|
||||||
|
id="a"
|
||||||
|
style={{ top: 10, background: '#fff' }}
|
||||||
|
/>
|
||||||
|
<Handle
|
||||||
|
type="source"
|
||||||
|
position="right"
|
||||||
|
id="b"
|
||||||
|
style={{ bottom: 10, top: 'auto', background: '#fff' }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
import React, { useState, useEffect } from 'react';
|
||||||
|
|
||||||
|
import Graph, { isEdge, removeElements, addEdge, MiniMap, Controls } from 'react-flow';
|
||||||
|
|
||||||
|
import ColorSelectorNode from './ColorSelectorNode';
|
||||||
|
|
||||||
|
const onNodeDragStop = node => console.log('drag stop', node);
|
||||||
|
const onElementClick = element => console.log('click', element);
|
||||||
|
const onLoad = (graph) => {
|
||||||
|
console.log('graph loaded:', graph);
|
||||||
|
};
|
||||||
|
|
||||||
|
const initBgColor = '#8888e8';
|
||||||
|
|
||||||
|
const CustomNodeGraph = () => {
|
||||||
|
const [elements, setElements] = useState([]);
|
||||||
|
const [bgColor, setBgColor] = useState(initBgColor);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onChange = (evt) => {
|
||||||
|
setElements(els => els.map(e => {
|
||||||
|
if (isEdge(e) || e.id !== '2') {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
const color = evt.target.value;
|
||||||
|
|
||||||
|
setBgColor(color);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...e,
|
||||||
|
data: {
|
||||||
|
...e.data,
|
||||||
|
color
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
setElements([
|
||||||
|
{ id: '1', type: 'input', data: { label: 'An input node' }, position: { x: 0, y: 50 }, sourcePosition: 'right' },
|
||||||
|
{ id: '2', type: 'selectorNode', data: { onChange: onChange, color: initBgColor }, position: { x: 250, y: 50 } },
|
||||||
|
{ id: '3', type: 'output', data: { label: 'Output A' }, position: { x: 550, y: 25 }, targetPosition: 'left' },
|
||||||
|
{ id: '4', type: 'output', data: { label: 'Output B' }, position: { x: 550, y: 100 }, targetPosition: 'left' },
|
||||||
|
|
||||||
|
{ id: 'e1-2', source: '1', target: '2', animated: true, style: { stroke: '#fff' } },
|
||||||
|
{ id: 'e2a-3', source: '2__a', target: '3', animated: true, style: { stroke: '#fff' } },
|
||||||
|
{ id: 'e2b-4', source: '2__b', target: '4', animated: true, style: { stroke: '#fff' } },
|
||||||
|
])
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const onElementsRemove = (elementsToRemove) => setElements(els => removeElements(elementsToRemove, els));
|
||||||
|
const onConnect = (params) => setElements(els => addEdge(params, els));
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Graph
|
||||||
|
elements={elements}
|
||||||
|
onElementClick={onElementClick}
|
||||||
|
onElementsRemove={onElementsRemove}
|
||||||
|
onConnect={onConnect}
|
||||||
|
onNodeDragStop={onNodeDragStop}
|
||||||
|
style={{ width: '100%', height: '100%', background: bgColor }}
|
||||||
|
onLoad={onLoad}
|
||||||
|
nodeTypes={{
|
||||||
|
selectorNode: ColorSelectorNode,
|
||||||
|
}}
|
||||||
|
connectionLineStyle={{ stroke: '#ddd', strokeWidth: 2 }}
|
||||||
|
connectionLineType="bezier"
|
||||||
|
backgroundColor="#888"
|
||||||
|
backgroundGap={16}
|
||||||
|
snapToGrid={true}
|
||||||
|
snapGrid={[16, 16]}
|
||||||
|
>
|
||||||
|
<MiniMap
|
||||||
|
nodeColor={n => {
|
||||||
|
if (n.type === 'input') return 'blue';
|
||||||
|
if (n.type === 'selectorNode') return bgColor;
|
||||||
|
if (n.type === 'output') return 'green';
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<Controls />
|
||||||
|
</Graph>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default CustomNodeGraph;
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
|
|
||||||
import { Handle } from 'react-flow';
|
|
||||||
|
|
||||||
export default({ data, styles }) => (
|
|
||||||
<div
|
|
||||||
style={{ background: '#FFCC00', padding: 10, borderRadius: 2, ...styles }}
|
|
||||||
>
|
|
||||||
<Handle type="target" position="left" style={{ background: '#999' }} />
|
|
||||||
<div>{data.input}</div>
|
|
||||||
<input onChange={(e) => data.onChange(e.target.value, data)} />
|
|
||||||
<Handle
|
|
||||||
type="source"
|
|
||||||
position="right"
|
|
||||||
style={{ background: '#999' }}
|
|
||||||
isValidConnection={connection => +connection.target % 2 === 0}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
|
|
||||||
import { Handle } from 'react-flow';
|
|
||||||
|
|
||||||
export default ({ data, styles }) => {
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
style={{ background: '#FFCC00', padding: 10, borderRadius: 2, ...styles }}
|
|
||||||
>
|
|
||||||
<Handle
|
|
||||||
type="target"
|
|
||||||
position="top"
|
|
||||||
id="a"
|
|
||||||
style={{ left: 10, background: '#999' }}
|
|
||||||
onConnect={params => console.log('handle onConnect', params)}
|
|
||||||
/>
|
|
||||||
<Handle
|
|
||||||
type="target"
|
|
||||||
position="top"
|
|
||||||
id="b"
|
|
||||||
style={{ left: 30, background: '#999' }}
|
|
||||||
/>
|
|
||||||
<div>I am <strong>special</strong>!<br />{data.label}</div>
|
|
||||||
<select onChange={(e) => data.onChange(e.target.value, data)}>
|
|
||||||
<option value="1">1</option>
|
|
||||||
<option value="2">2</option>
|
|
||||||
<option value="3">3</option>
|
|
||||||
</select>
|
|
||||||
<Handle
|
|
||||||
type="source"
|
|
||||||
position="bottom"
|
|
||||||
style={{ left: 10, background: '#999' }}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -1,119 +0,0 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
|
||||||
|
|
||||||
import Graph, { isEdge, removeElements, addEdge, MiniMap, Controls } from 'react-flow';
|
|
||||||
|
|
||||||
import SpecialNode from './SpecialNode';
|
|
||||||
import InputNode from './InputNode';
|
|
||||||
|
|
||||||
const onNodeDragStop = node => console.log('drag stop', node);
|
|
||||||
const onElementClick = element => console.log('click', element);
|
|
||||||
const onLoad = (graph) => console.log('graph loaded:', graph);
|
|
||||||
|
|
||||||
const CustomNodesGraph = () => {
|
|
||||||
const [elements, setElements] = useState([]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const onChange = (option, d) => {
|
|
||||||
setElements(els => els.map(e => {
|
|
||||||
if (isEdge(e) || e.id !== '6') {
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...e,
|
|
||||||
data: {
|
|
||||||
...e.data,
|
|
||||||
label: `Option ${option} selected.`
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
|
|
||||||
const onChangeInput = (input, d) => {
|
|
||||||
setElements(els => els.map(e => {
|
|
||||||
if (isEdge(e) || e.id !== '8') {
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...e,
|
|
||||||
data: {
|
|
||||||
...e.data,
|
|
||||||
input: input || 'write something'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
|
|
||||||
setElements([
|
|
||||||
{ id: '1', type: 'input', data: { label: '1 Tests' }, position: { x: 250, y: 5 } },
|
|
||||||
{ id: '2', data: { label: '2 This is a node This is a node This is a node This is a node' }, position: { x: 100, y: 100 } },
|
|
||||||
{ id: '3', data: { label: '3 I bring my own style' }, position: { x: 100, y: 200 }, style: { background: '#eee', color: '#222', border: '1px solid #bbb' } },
|
|
||||||
{ id: '4', type: 'output', data: { label: '4 nody nodes' }, position: { x: 50, y: 300 } },
|
|
||||||
{ id: '5', type: 'default', data: { label: '5 Another node'}, position: { x: 400, y: 300 } },
|
|
||||||
{ id: '6', type: 'special', data: { onChange: onChange, label: '6 no option selected' }, position: { x: 425, y: 375 } },
|
|
||||||
{ id: '7', type: 'output', data: { label: '7 output' }, position: { x: 250, y: 500 } },
|
|
||||||
{ id: '8', type: 'text', data: { onChange: onChangeInput, input: 'write something' }, position: { x: 350, y: 100 } },
|
|
||||||
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
|
||||||
{ id: 'e1-8', source: '1', target: '8', animated: true },
|
|
||||||
{ id: 'e2-3', source: '2', target: '3', animated: true },
|
|
||||||
{ id: 'e3-4', source: '3', target: '4', animated: true },
|
|
||||||
{ id: 'e3-5', source: '3', target: '5', animated: true },
|
|
||||||
{ id: 'e5-6b', source: '5', target: '6__b', animated: true },
|
|
||||||
{ id: 'e5-6a', source: '5', target: '6__a', animated: true },
|
|
||||||
{ id: 'e6-7', source: '6', target: '7', animated: true },
|
|
||||||
])
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const addRandomNode = () => {
|
|
||||||
setElements(els => els.concat({
|
|
||||||
id: (els.length + 1).toString(),
|
|
||||||
data: { label: 'Added node' },
|
|
||||||
position: { x: Math.random() * window.innerWidth, y: Math.random() * window.innerHeight }
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
const onElementsRemove = (elementsToRemove) => setElements(els => removeElements(elementsToRemove, els));
|
|
||||||
const onConnect = (params) => setElements(els => addEdge(params, els));
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Graph
|
|
||||||
elements={elements}
|
|
||||||
onElementClick={onElementClick}
|
|
||||||
onElementsRemove={onElementsRemove}
|
|
||||||
onConnect={onConnect}
|
|
||||||
onNodeDragStop={onNodeDragStop}
|
|
||||||
style={{ width: '100%', height: '100%' }}
|
|
||||||
onLoad={onLoad}
|
|
||||||
nodeTypes={{
|
|
||||||
special: SpecialNode,
|
|
||||||
text: InputNode
|
|
||||||
}}
|
|
||||||
connectionLineStyle={{ stroke: '#ddd', strokeWidth: 2 }}
|
|
||||||
connectionLineType="bezier"
|
|
||||||
backgroundColor="#888"
|
|
||||||
backgroundGap={16}
|
|
||||||
snapToGrid={true}
|
|
||||||
snapGrid={[16, 16]}
|
|
||||||
>
|
|
||||||
<MiniMap
|
|
||||||
nodeColor={n => {
|
|
||||||
if (n.type === 'input') return 'blue';
|
|
||||||
if (n.type === 'output') return 'green';
|
|
||||||
if (n.type === 'default') return 'red';
|
|
||||||
|
|
||||||
return '#FFCC00';
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<Controls />
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={addRandomNode}
|
|
||||||
style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}
|
|
||||||
>
|
|
||||||
add node
|
|
||||||
</button>
|
|
||||||
</Graph>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default CustomNodesGraph;
|
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
import React, { useState, useEffect } from 'react';
|
||||||
|
|
||||||
|
import Graph, { removeElements, addEdge, MiniMap, Controls } from 'react-flow';
|
||||||
|
|
||||||
|
const onNodeDragStop = node => console.log('drag stop', node);
|
||||||
|
const onElementClick = element => console.log('click', element);
|
||||||
|
const onLoad = (graph) => console.log('graph loaded:', graph);
|
||||||
|
|
||||||
|
const initialElements = [
|
||||||
|
{ id: '1', type: 'input', data: { label: 'Input Node 1' }, position: { x: 250, y: 5 } },
|
||||||
|
{ id: '2', data: { label: 'Node 2' }, position: { x: 150, y: 75 } },
|
||||||
|
{
|
||||||
|
id: '3', data: { label: 'Node 3 with custom style' }, position: { x: 250, y: 150 },
|
||||||
|
style: { background: '#eee', color: '#222', border: '1px solid #bbb' },
|
||||||
|
sourcePosition: 'right'
|
||||||
|
},
|
||||||
|
{ id: '4', data: { label: 'Node 4' }, position: { x: 500, y: 200 }, targetPosition: 'left' },
|
||||||
|
{ id: '5', type: 'output', data: { label: 'Output Node 5'}, position: { x: 300, y: 300 } },
|
||||||
|
{ id: '6', type: 'output', data: { label: 'Output Node 6' }, position: { x: 600, y: 400 } },
|
||||||
|
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
||||||
|
{ id: 'e2-3', source: '2', target: '3', animated: true },
|
||||||
|
{ id: 'e3-4', source: '3', target: '4', animated: true },
|
||||||
|
{ id: 'e3-5', source: '4', target: '5', animated: true, type: 'step' },
|
||||||
|
{ id: 'e5-6b', source: '4', target: '6', type: 'step' },
|
||||||
|
]
|
||||||
|
|
||||||
|
const RichGraph = () => {
|
||||||
|
const [elements, setElements] = useState(initialElements);
|
||||||
|
|
||||||
|
const addRandomNode = () => {
|
||||||
|
setElements(els => els.concat({
|
||||||
|
id: (els.length + 1).toString(),
|
||||||
|
data: { label: 'Added node' },
|
||||||
|
position: { x: Math.random() * window.innerWidth, y: Math.random() * window.innerHeight }
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
const onElementsRemove = (elementsToRemove) => setElements(els => removeElements(elementsToRemove, els));
|
||||||
|
const onConnect = (params) => setElements(els => addEdge(params, els));
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Graph
|
||||||
|
elements={elements}
|
||||||
|
onElementClick={onElementClick}
|
||||||
|
onElementsRemove={onElementsRemove}
|
||||||
|
onConnect={onConnect}
|
||||||
|
onNodeDragStop={onNodeDragStop}
|
||||||
|
style={{ width: '100%', height: '100%' }}
|
||||||
|
onLoad={onLoad}
|
||||||
|
connectionLineStyle={{ stroke: '#ddd', strokeWidth: 2 }}
|
||||||
|
connectionLineType="bezier"
|
||||||
|
backgroundColor="#888"
|
||||||
|
backgroundGap={16}
|
||||||
|
snapToGrid={true}
|
||||||
|
snapGrid={[16, 16]}
|
||||||
|
>
|
||||||
|
<MiniMap
|
||||||
|
nodeColor={n => {
|
||||||
|
if (n.type === 'input') return 'blue';
|
||||||
|
if (n.type === 'output') return 'green';
|
||||||
|
if (n.type === 'default') return 'red';
|
||||||
|
|
||||||
|
return '#FFCC00';
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<Controls />
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={addRandomNode}
|
||||||
|
style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}
|
||||||
|
>
|
||||||
|
add node
|
||||||
|
</button>
|
||||||
|
</Graph>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default RichGraph;
|
||||||
@@ -2,7 +2,8 @@ import React from 'react';
|
|||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
|
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
|
||||||
|
|
||||||
import CustomNodes from './CustomNodes';
|
import CustomNode from './CustomNode';
|
||||||
|
import Rich from './Rich';
|
||||||
import Basic from './Basic';
|
import Basic from './Basic';
|
||||||
import Empty from './Empty';
|
import Empty from './Empty';
|
||||||
import Inactive from './Inactive';
|
import Inactive from './Inactive';
|
||||||
@@ -21,8 +22,11 @@ ReactDOM.render((
|
|||||||
<Route path="/inactive">
|
<Route path="/inactive">
|
||||||
<Inactive />
|
<Inactive />
|
||||||
</Route>
|
</Route>
|
||||||
|
<Route path="/custom-node">
|
||||||
|
<CustomNode />
|
||||||
|
</Route>
|
||||||
<Route path="/">
|
<Route path="/">
|
||||||
<CustomNodes />
|
<Rich />
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
</Router>
|
</Router>
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ const nodeStyles: CSSProperties = {
|
|||||||
width: 150,
|
width: 150,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ({ data, style }: NodeProps) => (
|
export default ({ data, targetPosition = Position.Top, sourcePosition = Position.Bottom, style }: NodeProps) => (
|
||||||
<div style={{ ...nodeStyles, ...style }}>
|
<div style={{ ...nodeStyles, ...style }}>
|
||||||
<Handle type="target" position={Position.Top} />
|
<Handle type="target" position={targetPosition} />
|
||||||
{data.label}
|
{data.label}
|
||||||
<Handle type="source" position={Position.Bottom} />
|
<Handle type="source" position={sourcePosition} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ const nodeStyles: CSSProperties = {
|
|||||||
width: 150,
|
width: 150,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ({ data, style }: NodeProps) => (
|
export default ({ data, style, sourcePosition = Position.Bottom }: NodeProps) => (
|
||||||
<div style={{ ...nodeStyles, ...style }}>
|
<div style={{ ...nodeStyles, ...style }}>
|
||||||
{data.label}
|
{data.label}
|
||||||
<Handle type="source" position={Position.Bottom} />
|
<Handle type="source" position={sourcePosition} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ const nodeStyles: CSSProperties = {
|
|||||||
width: 150,
|
width: 150,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ({ data, style }: NodeProps) => (
|
export default ({ data, style, targetPosition = Position.Top }: NodeProps) => (
|
||||||
<div style={{ ...nodeStyles, ...style }}>
|
<div style={{ ...nodeStyles, ...style }}>
|
||||||
<Handle type="target" position={Position.Top} />
|
<Handle type="target" position={targetPosition} />
|
||||||
{data.label}
|
{data.label}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -149,6 +149,8 @@ export default (NodeComponent: ComponentType<NodeComponentProps>) => {
|
|||||||
onNodeDragStop,
|
onNodeDragStop,
|
||||||
style,
|
style,
|
||||||
isInteractive,
|
isInteractive,
|
||||||
|
sourcePosition,
|
||||||
|
targetPosition,
|
||||||
}: WrapNodeProps) => {
|
}: WrapNodeProps) => {
|
||||||
const nodeElement = useRef<HTMLDivElement>(null);
|
const nodeElement = useRef<HTMLDivElement>(null);
|
||||||
const [offset, setOffset] = useState({ x: 0, y: 0 });
|
const [offset, setOffset] = useState({ x: 0, y: 0 });
|
||||||
@@ -208,7 +210,15 @@ export default (NodeComponent: ComponentType<NodeComponentProps>) => {
|
|||||||
>
|
>
|
||||||
<div className={nodeClasses} ref={nodeElement} style={nodeStyle}>
|
<div className={nodeClasses} ref={nodeElement} style={nodeStyle}>
|
||||||
<Provider value={id}>
|
<Provider value={id}>
|
||||||
<NodeComponent id={id} data={data} type={type} style={style} selected={selected} />
|
<NodeComponent
|
||||||
|
id={id}
|
||||||
|
data={data}
|
||||||
|
type={type}
|
||||||
|
style={style}
|
||||||
|
selected={selected}
|
||||||
|
sourcePosition={sourcePosition}
|
||||||
|
targetPosition={targetPosition}
|
||||||
|
/>
|
||||||
</Provider>
|
</Provider>
|
||||||
</div>
|
</div>
|
||||||
</DraggableCore>
|
</DraggableCore>
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ function renderNode(
|
|||||||
selected={isSelected}
|
selected={isSelected}
|
||||||
style={node.style}
|
style={node.style}
|
||||||
isInteractive={isInteractive}
|
isInteractive={isInteractive}
|
||||||
|
sourcePosition={node.sourcePosition}
|
||||||
|
targetPosition={node.targetPosition}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ export interface Node {
|
|||||||
__rg?: any;
|
__rg?: any;
|
||||||
data?: any;
|
data?: any;
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
|
targetPosition?: Position;
|
||||||
|
sourcePosition?: Position;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Edge {
|
export interface Edge {
|
||||||
@@ -83,6 +85,8 @@ export interface NodeProps {
|
|||||||
type: string;
|
type: string;
|
||||||
data: any;
|
data: any;
|
||||||
selected: boolean;
|
selected: boolean;
|
||||||
|
targetPosition?: Position;
|
||||||
|
sourcePosition?: Position;
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,6 +98,8 @@ export interface NodeComponentProps {
|
|||||||
transform?: Transform;
|
transform?: Transform;
|
||||||
xPos?: number;
|
xPos?: number;
|
||||||
yPos?: number;
|
yPos?: number;
|
||||||
|
targetPosition?: Position;
|
||||||
|
sourcePosition?: Position;
|
||||||
onClick?: (node: Node) => void | undefined;
|
onClick?: (node: Node) => void | undefined;
|
||||||
onNodeDragStop?: () => any;
|
onNodeDragStop?: () => any;
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
@@ -111,6 +117,8 @@ export interface WrapNodeProps {
|
|||||||
onClick: (node: Node) => void | undefined;
|
onClick: (node: Node) => void | undefined;
|
||||||
onNodeDragStop: (node: Node) => void;
|
onNodeDragStop: (node: Node) => void;
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
|
sourcePosition?: Position;
|
||||||
|
targetPosition?: Position;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type FitViewParams = {
|
export type FitViewParams = {
|
||||||
|
|||||||
Reference in New Issue
Block a user