feat(example): add sidebar to provider example
This commit is contained in:
@@ -1,2 +1 @@
|
|||||||
example/src
|
|
||||||
*.md
|
*.md
|
||||||
25
example/src/Provider/Sidebar.js
Normal file
25
example/src/Provider/Sidebar.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { useStoreState } from 'react-flow-renderer';
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
const nodes = useStoreState((store) => store.nodes);
|
||||||
|
const transform = useStoreState((store) => store.transform);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<aside>
|
||||||
|
<div className="description">
|
||||||
|
This is an example of how you can access the internal state outside of the ReactFlow component.
|
||||||
|
</div>
|
||||||
|
<div className="title">Zoom & pan transform</div>
|
||||||
|
<div className="transform">
|
||||||
|
[{transform[0].toFixed(2)}, {transform[1].toFixed(2)}, {transform[2].toFixed(2)}]
|
||||||
|
</div>
|
||||||
|
<div className="title">Nodes</div>
|
||||||
|
{nodes.map((node) => (
|
||||||
|
<div key={node.id}>
|
||||||
|
Node {node.id} - x: {node.__rg.position.x.toFixed(2)}, y: {node.__rg.position.y.toFixed(2)}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</aside>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
|
import ReactFlow, { ReactFlowProvider, addEdge, removeElements } from 'react-flow-renderer';
|
||||||
|
|
||||||
import ReactFlow, { addEdge, ReactFlowProvider } from 'react-flow-renderer';
|
import Sidebar from './Sidebar';
|
||||||
|
|
||||||
const onElementClick = element => console.log('click', element);
|
import './provider.css';
|
||||||
|
|
||||||
|
const onElementClick = (element) => console.log('click', element);
|
||||||
|
|
||||||
const initialElements = [
|
const initialElements = [
|
||||||
{ id: '1', type: 'input', data: { label: 'Node 1' }, position: { x: 250, y: 5 } },
|
{ id: '1', type: 'input', data: { label: 'Node 1' }, position: { x: 250, y: 5 } },
|
||||||
@@ -10,22 +13,29 @@ const initialElements = [
|
|||||||
{ id: '3', data: { label: 'Node 3' }, position: { x: 400, y: 100 } },
|
{ id: '3', data: { label: 'Node 3' }, position: { x: 400, y: 100 } },
|
||||||
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 200 } },
|
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 200 } },
|
||||||
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
||||||
{ id: 'e1-3', source: '1', target: '3' }
|
{ id: 'e1-3', source: '1', target: '3' },
|
||||||
];
|
];
|
||||||
|
|
||||||
const ProviderFlow = () => {
|
const ProviderFlow = () => {
|
||||||
const [elements, setElements] = useState(initialElements);
|
const [elements, setElements] = useState(initialElements);
|
||||||
const onConnect = (params) => setElements(els => addEdge(params, els));
|
const onConnect = (params) => setElements((els) => addEdge(params, els));
|
||||||
|
const onElementsRemove = (elementsToRemove) => setElements((els) => removeElements(elementsToRemove, els));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ReactFlowProvider>
|
<div className="providerflow">
|
||||||
<ReactFlow
|
<ReactFlowProvider>
|
||||||
elements={elements}
|
<Sidebar />
|
||||||
onElementClick={onElementClick}
|
<div className="reactflow-wrapper">
|
||||||
onConnect={onConnect}
|
<ReactFlow
|
||||||
/>
|
elements={elements}
|
||||||
</ReactFlowProvider>
|
onElementClick={onElementClick}
|
||||||
|
onConnect={onConnect}
|
||||||
|
onElementsRemove={onElementsRemove}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</ReactFlowProvider>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export default ProviderFlow;
|
export default ProviderFlow;
|
||||||
|
|||||||
32
example/src/Provider/provider.css
Normal file
32
example/src/Provider/provider.css
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
.providerflow {
|
||||||
|
display: flex;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.providerflow aside {
|
||||||
|
width: 20%;
|
||||||
|
max-width: 250px;
|
||||||
|
flex-grow: 1;
|
||||||
|
border-right: 1px solid #eee;
|
||||||
|
padding: 10px;
|
||||||
|
font-size: 12px;
|
||||||
|
background: #fcfcfc;
|
||||||
|
}
|
||||||
|
|
||||||
|
.providerflow aside .description {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.providerflow aside .title {
|
||||||
|
font-weight: 700;
|
||||||
|
margin-bottom: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.providerflow aside .transform {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.providerflow .reactflow-wrapper {
|
||||||
|
flex-grow: 1;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
@@ -15,54 +15,65 @@ import Provider from './Provider';
|
|||||||
|
|
||||||
import './index.css';
|
import './index.css';
|
||||||
|
|
||||||
const routes = [{
|
const routes = [
|
||||||
path: '/',
|
{
|
||||||
component: Overview,
|
path: '/',
|
||||||
label: 'Overview'
|
component: Overview,
|
||||||
}, {
|
label: 'Overview',
|
||||||
path: '/provider',
|
},
|
||||||
component: Provider,
|
{
|
||||||
label: 'Provider'
|
path: '/edges',
|
||||||
}, {
|
component: Edges,
|
||||||
path: '/edges',
|
label: 'Edges',
|
||||||
component: Edges,
|
},
|
||||||
label: 'Edges'
|
{
|
||||||
}, {
|
path: '/custom-node',
|
||||||
path: '/custom-node',
|
component: CustomNode,
|
||||||
component: CustomNode,
|
label: 'CustomNode',
|
||||||
label: 'CustomNode'
|
},
|
||||||
}, {
|
{
|
||||||
path: '/horizontal',
|
path: '/horizontal',
|
||||||
component: Horizontal,
|
component: Horizontal,
|
||||||
label: 'Horizontal'
|
label: 'Horizontal',
|
||||||
}, {
|
},
|
||||||
path: '/validation',
|
{
|
||||||
component: Validation,
|
path: '/validation',
|
||||||
label: 'Validation'
|
component: Validation,
|
||||||
}, {
|
label: 'Validation',
|
||||||
path: '/stress',
|
},
|
||||||
component: Stress,
|
{
|
||||||
label: 'Stress'
|
path: '/provider',
|
||||||
}, {
|
component: Provider,
|
||||||
path: '/basic',
|
label: 'Provider',
|
||||||
component: Basic
|
},
|
||||||
}, {
|
{
|
||||||
path: '/empty',
|
path: '/stress',
|
||||||
component: Empty
|
component: Stress,
|
||||||
}, {
|
label: 'Stress',
|
||||||
path: '/inactive',
|
},
|
||||||
component: Inactive
|
{
|
||||||
}];
|
path: '/basic',
|
||||||
|
component: Basic,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/empty',
|
||||||
|
component: Empty,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/inactive',
|
||||||
|
component: Inactive,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
const navLinks = routes.filter(route => route.label);
|
const navLinks = routes.filter((route) => route.label);
|
||||||
|
|
||||||
const SourceDisplay = withRouter(({ location }) => {
|
const SourceDisplay = withRouter(({ location }) => {
|
||||||
const route = routes.find(route => route.path === location.pathname);
|
const route = routes.find((route) => route.path === location.pathname);
|
||||||
const sourceLink = `https://github.com/wbkd/react-flow/tree/master/example/src/${route.label}/index.js`;
|
const sourceLink = `https://github.com/wbkd/react-flow/tree/master/example/src/${route.label}/index.js`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<a className="sourcedisplay" href={sourceLink}>
|
<a className="sourcedisplay" href={sourceLink}>
|
||||||
{'<Source />'}
|
{'<Source />'}
|
||||||
</a>
|
</a>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -70,30 +81,34 @@ const SourceDisplay = withRouter(({ location }) => {
|
|||||||
const Header = () => {
|
const Header = () => {
|
||||||
const [menuOpen, setMenuOpen] = useState();
|
const [menuOpen, setMenuOpen] = useState();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<header>
|
<header>
|
||||||
<a className="logo" href="https://github.com/wbkd/react-flow">React Flow</a>
|
<a className="logo" href="https://github.com/wbkd/react-flow">
|
||||||
|
React Flow
|
||||||
|
</a>
|
||||||
<nav className={menuOpen ? 'is-open' : ''}>
|
<nav className={menuOpen ? 'is-open' : ''}>
|
||||||
{navLinks.map(route => (
|
{navLinks.map((route) => (
|
||||||
<NavLink to={route.path} key={route.label} exact>
|
<NavLink to={route.path} key={route.label} exact>
|
||||||
{route.label}
|
{route.label}
|
||||||
</NavLink>
|
</NavLink>
|
||||||
))}
|
))}
|
||||||
</nav>
|
</nav>
|
||||||
<button className="menu" onClick={() => setMenuOpen(!menuOpen)}>Menu</button>
|
<button className="menu" onClick={() => setMenuOpen(!menuOpen)}>
|
||||||
|
Menu
|
||||||
|
</button>
|
||||||
</header>
|
</header>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
ReactDOM.render((
|
ReactDOM.render(
|
||||||
<Router forceRefresh={true}>
|
<Router forceRefresh={true}>
|
||||||
<Header />
|
<Header />
|
||||||
<SourceDisplay />
|
<SourceDisplay />
|
||||||
<Switch>
|
<Switch>
|
||||||
{routes.map(route => (
|
{routes.map((route) => (
|
||||||
<Route exact path={route.path} render={() => <route.component />} key={route.path} />
|
<Route exact path={route.path} render={() => <route.component />} key={route.path} />
|
||||||
))}
|
))}
|
||||||
</Switch>
|
</Switch>
|
||||||
</Router>
|
</Router>,
|
||||||
), document.getElementById('root'));
|
document.getElementById('root')
|
||||||
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user