refactor(example): dont use inline functions for handle props #282

This commit is contained in:
moklick
2020-06-05 13:01:07 +02:00
parent 6b8e7bfbc3
commit f18e8f147f
+10 -20
View File
@@ -8,53 +8,43 @@ const initialElements = [
{ id: '0', type: 'custominput', position: { x: 0, y: 150 } }, { id: '0', type: 'custominput', position: { x: 0, y: 150 } },
{ id: 'A', type: 'customnode', position: { x: 250, y: 0 } }, { id: 'A', type: 'customnode', position: { x: 250, y: 0 } },
{ id: 'B', type: 'customnode', position: { x: 250, y: 150 } }, { id: 'B', type: 'customnode', position: { x: 250, y: 150 } },
{ id: 'C', type: 'customnode', position: { x: 250, y:300 } }, { id: 'C', type: 'customnode', position: { x: 250, y: 300 } },
]; ];
const isValidConnection = (connection) => connection.target === 'B';
const CustomInput = () => ( const CustomInput = () => (
<> <>
<div>Only connectable with B</div> <div>Only connectable with B</div>
<Handle <Handle type="source" position="right" isValidConnection={isValidConnection} />
type="source"
position="right"
isValidConnection={connection => connection.target === 'B'}
/>
</> </>
); );
const CustomNode = ({ id }) => ( const CustomNode = ({ id }) => (
<> <>
<Handle <Handle type="target" position="left" isValidConnection={isValidConnection} />
type="target"
position="left"
isValidConnection={_ => id === 'B'}
/>
<div>{id}</div> <div>{id}</div>
<Handle <Handle type="source" position="right" isValidConnection={isValidConnection} />
type="source"
position="right"
isValidConnection={_ => id === 'B'}
/>
</> </>
); );
const HorizontalFlow = () => { const HorizontalFlow = () => {
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));
return ( return (
<ReactFlow <ReactFlow
elements={elements} elements={elements}
onConnect={onConnect} onConnect={onConnect}
selectNodesOnDrag={false} selectNodesOnDrag={false}
onLoad={reactFlowInstance => reactFlowInstance.fitView()} onLoad={(reactFlowInstance) => reactFlowInstance.fitView()}
className="validationflow" className="validationflow"
nodeTypes={{ nodeTypes={{
custominput: CustomInput, custominput: CustomInput,
customnode: CustomNode customnode: CustomNode,
}} }}
/> />
); );
} };
export default HorizontalFlow; export default HorizontalFlow;