chore(examples): add undirectional
This commit is contained in:
@@ -0,0 +1,19 @@
|
|||||||
|
import React, { memo, FC, CSSProperties } from 'react';
|
||||||
|
|
||||||
|
import { Handle, Position, NodeProps } from 'react-flow-renderer';
|
||||||
|
|
||||||
|
const nodeStyles: CSSProperties = { padding: '10px 15px', border: '1px solid #ddd' };
|
||||||
|
|
||||||
|
const CustomNode: FC<NodeProps> = () => {
|
||||||
|
return (
|
||||||
|
<div style={nodeStyles}>
|
||||||
|
<div>node</div>
|
||||||
|
<Handle type="source" id="left" position={Position.Left} />
|
||||||
|
<Handle type="source" id="right" position={Position.Right} />
|
||||||
|
<Handle type="source" id="top" position={Position.Top} />
|
||||||
|
<Handle type="source" id="bottom" position={Position.Bottom} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default memo(CustomNode);
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import React, { useState, useCallback } from 'react';
|
||||||
|
|
||||||
|
import ReactFlow, {
|
||||||
|
NodeTypesType,
|
||||||
|
addEdge,
|
||||||
|
useZoomPanHelper,
|
||||||
|
ReactFlowProvider,
|
||||||
|
Elements,
|
||||||
|
Connection,
|
||||||
|
Edge,
|
||||||
|
ElementId,
|
||||||
|
ConnectionLineType,
|
||||||
|
ConnectionMode,
|
||||||
|
} from 'react-flow-renderer';
|
||||||
|
import CustomNode from './CustomNode';
|
||||||
|
|
||||||
|
const initialElements: Elements = [
|
||||||
|
{
|
||||||
|
id: '1',
|
||||||
|
type: 'custom',
|
||||||
|
position: { x: 150, y: 100 },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2',
|
||||||
|
type: 'custom',
|
||||||
|
position: { x: 300, y: 100 },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '3',
|
||||||
|
type: 'custom',
|
||||||
|
position: { x: 450, y: 100 },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const nodeTypes: NodeTypesType = {
|
||||||
|
custom: CustomNode,
|
||||||
|
};
|
||||||
|
|
||||||
|
let id = 4;
|
||||||
|
const getId = (): ElementId => `${id++}`;
|
||||||
|
|
||||||
|
const UpdateNodeInternalsFlow = () => {
|
||||||
|
const [elements, setElements] = useState<Elements>(initialElements);
|
||||||
|
const onConnect = (params: Connection | Edge) =>
|
||||||
|
setElements((els) => addEdge({ ...params, type: 'smoothstep' }, els));
|
||||||
|
const { project } = useZoomPanHelper();
|
||||||
|
|
||||||
|
const onPaneClick = useCallback(
|
||||||
|
(evt) =>
|
||||||
|
setElements((els) =>
|
||||||
|
els.concat({
|
||||||
|
id: getId(),
|
||||||
|
position: project({ x: evt.clientX, y: evt.clientY - 40 }),
|
||||||
|
type: 'custom',
|
||||||
|
})
|
||||||
|
),
|
||||||
|
[project]
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ReactFlow
|
||||||
|
elements={elements}
|
||||||
|
nodeTypes={nodeTypes}
|
||||||
|
onConnect={onConnect}
|
||||||
|
onPaneClick={onPaneClick}
|
||||||
|
connectionLineType={ConnectionLineType.SmoothStep}
|
||||||
|
connectionMode={ConnectionMode.Loose}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const WrappedFlow = () => (
|
||||||
|
<ReactFlowProvider>
|
||||||
|
<UpdateNodeInternalsFlow />
|
||||||
|
</ReactFlowProvider>
|
||||||
|
);
|
||||||
|
|
||||||
|
export default WrappedFlow;
|
||||||
@@ -22,7 +22,6 @@ const initialElements: Elements = [
|
|||||||
type: 'custom',
|
type: 'custom',
|
||||||
data: { label: 'Node 1', handleCount: initialHandleCount },
|
data: { label: 'Node 1', handleCount: initialHandleCount },
|
||||||
position: { x: 250, y: 5 },
|
position: { x: 250, y: 5 },
|
||||||
className: 'light',
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import Layout from './Layouting';
|
|||||||
import SwitchFlows from './Switch';
|
import SwitchFlows from './Switch';
|
||||||
import UseZoomPanHelper from './UseZoomPanHelper';
|
import UseZoomPanHelper from './UseZoomPanHelper';
|
||||||
import UseUpdateNodeInternals from './UseUpdateNodeInternals';
|
import UseUpdateNodeInternals from './UseUpdateNodeInternals';
|
||||||
|
import Undirectional from './Undirectional';
|
||||||
|
|
||||||
import './index.css';
|
import './index.css';
|
||||||
|
|
||||||
@@ -111,6 +112,10 @@ const routes = [
|
|||||||
path: '/useupdatenodeinternals',
|
path: '/useupdatenodeinternals',
|
||||||
component: UseUpdateNodeInternals,
|
component: UseUpdateNodeInternals,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: '/undirectional',
|
||||||
|
component: Undirectional,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const Header = withRouter(({ history, location }) => {
|
const Header = withRouter(({ history, location }) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user