docs(site): add baisc example

This commit is contained in:
moklick
2020-10-06 11:38:29 +02:00
parent 2fd2e2d2b7
commit c469957c58
4 changed files with 91 additions and 12 deletions
@@ -0,0 +1,41 @@
import React, { useState } from 'react';
import ReactFlow, {
removeElements,
addEdge,
ReactFlowProvider,
} from 'react-flow-renderer';
const initialElements = [
{
id: '1',
type: 'input',
data: { label: 'Input Node' },
position: { x: 250, y: 25 },
},
{
id: '2',
data: { label: 'Another Node' },
position: { x: 100, y: 125 },
},
];
const BasicFlow = () => {
const [elements, setElements] = useState(initialElements);
const onElementsRemove = (elementsToRemove) =>
setElements((els) => removeElements(elementsToRemove, els));
const onConnect = (params) => setElements((els) => addEdge(params, els));
return (
<div style={{ height: 300, border: '1px solid #EAEDF1', borderRadius: 5 }}>
<ReactFlowProvider>
<ReactFlow
elements={elements}
onElementsRemove={onElementsRemove}
onConnect={onConnect}
/>
</ReactFlowProvider>
</div>
);
};
export default BasicFlow;
@@ -25,10 +25,9 @@ import React from 'react';
import ReactFlow from 'react-flow-renderer';
const elements = [
// input node
{
id: '1',
type: 'input',
type: 'input', // input node
data: { label: 'Input Node' },
position: { x: 250, y: 25 },
},
@@ -39,11 +38,9 @@ const elements = [
data: { label: <div>Default Node</div> },
position: { x: 100, y: 125 },
},
// output node
{
id: '3',
type: 'output',
// you can also pass a React component as a label
type: 'output', // output node
data: { label: 'Output Node' },
position: { x: 250, y: 250 },
},
@@ -52,11 +49,56 @@ const elements = [
{ id: 'e2-3', source: '2', target: '3' },
];
const BasicFlow = () => <ReactFlow elements={elements} />;
export default () => <ReactFlow elements={elements} />;
```
import Flow from './index';
<Flow />
You can find more advanced examples in the [examples](reactflow.dev/examples/) section.
## Basic Functionality
We dont do any state updates besides the positions. This means that you need to pass the functions to remove an element or connect nodes by yourself. You can implement your own ones or use the [helper functions](/docs/api/helper-functions/) that come with the library. Here you see an example of how to use the heler functions `removeElements` and `addEdge`.
```jsx
import React, { useState } from 'react';
import ReactFlow, { removeElements, addEdge } from 'react-flow-renderer';
const initialElements = [
{
id: '1',
type: 'input',
data: { label: 'Input Node' },
position: { x: 250, y: 25 },
},
{
id: '2',
data: { label: 'Another Node' },
position: { x: 100, y: 125 },
},
];
export default () => {
const [elements, setElements] = useState(initialElements);
const onElementsRemove = (elementsToRemove) =>
setElements((els) => removeElements(elementsToRemove, els));
const onConnect = (params) => setElements((els) => addEdge(params, els));
return (
<ReactFlow
elements={elements}
onElementsRemove={onElementsRemove}
onConnect={onConnect}
/>
);
}
```
In this example you can connect nodes and remove selected nodes and edges with the delete key.
import Basic from './BasicFunctions';
<Basic />
You can find more advanced examples in the [examples](/examples/) section.
-2
View File
@@ -12,5 +12,3 @@ React Flow is a library for building node-based applications. These can be simpl
* **Utils:** Snap-to-grid and graph [helper functions](#helper-functions)
* **Components:** [Background, Minimap and Controls](#components)
* **Reliable**: Written in [Typescript](https://www.typescriptlang.org/) and tested with [cypress](https://www.cypress.io/)
In order to make this library as flexible as possible we dont do any state updates besides the positions. This means that you need to pass the functions to remove an element or connect nodes by yourself. You can implement your own ones or use the helper functions that come with the library.
+1 -3
View File
@@ -149,9 +149,7 @@ const Home = () => {
<Paragraph my={3} color={baseColors.textLight}>
A flow consists of nodes and edges (or just nodes). Together we call
them elements. You can pass a set of elements as a prop to the
ReactFlow component. Hereby all elements need unique ids. A node
needs a position and a label and an edge needs a source (node id)
and a target (node id). This is the most basic for a flow. A simple
ReactFlow component. This is the most basic for a flow. A simple
flow could look like this:
</Paragraph>
<CodeBlock